WordCamp Detroit 2010 Wordpress Theme Hacks


Published on

WordCamp Detroit 2010 Wordpress Theme Hacks presentation slides by JTPratt Media. #WCDET

Published in: Technology
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

WordCamp Detroit 2010 Wordpress Theme Hacks

  1. 1. Wordpress Theme Hacks Nearly Anyone Can Do <ul><li>Afraid you might &quot;break something&quot; by mucking around in your theme? With a little determination, and just a few keystrokes - you'd be surprised how many things you can change on your own. </li></ul>
  2. 2. How I Met Wordpress THIS WAS ME For 10 LONG YEARS
  3. 3. In 1999 I was hired as a webmaster I was VERY loyal
  4. 4. Webmasters Did Everything <ul><li>HTML </li></ul><ul><li>Javascript </li></ul><ul><li>Graphics </li></ul><ul><li>Technical Writing </li></ul><ul><li>CGI/Perl Coding </li></ul><ul><li>UNIX Shell Coding </li></ul><ul><li>Create Online Tools </li></ul><ul><li>Training </li></ul>
  5. 5. Open Source CMS I tried to introduce multiple Open Source CMS Systems to Automate my Job
  6. 6. Big Money Always Won
  7. 7. I Met Wordpress in 2004
  8. 8. I Thought They Were Wrong... So I kept using Wordpress on My Own... Sometimes instead of doing my real work.
  9. 9. Little Did I Know... In 2009 they outsourced the last of us... the last of the “webmasters” that is.
  10. 10. I Was Out of Work No Jobs. Unemployment. Couldn't Move. What Now?
  11. 11. Reinvention
  12. 12. Wordpress Theme Hacks Nearly Anyone Can Do <ul><li>You Will Need to: </li></ul><ul><li>Use the Wordpress dashboard editor </li></ul><ul><li>Know basic HTML </li></ul><ul><li>Be able to Edit, Copy and Paste small amounts of PHP Code </li></ul>
  13. 13. Why Hack a Wordpress Theme? <ul><li>To stand out or look different create a look nobody else has </li></ul><ul><li>To add functionality for the end user “like”, “tweet”, disqus </li></ul><ul><li>To enhance the way content is displayed post meta, text, images, “the_loop”, navigation </li></ul><ul><li>To customize specific pages search results, 404 Pages, archives, category & tags pages </li></ul><ul><li>To show content only when conditions are met only on the home page, just singles posts, only tag pages, etc. </li></ul>
  14. 14. How Do I Edit My WP Theme? In WP dashboard: Appearance -> Editor Click on the page you want to edit:
  15. 15. What Are All Those Theme Pages? <ul><li>At Bare Minimum, Most WP Themes Have: </li></ul><ul><li>index.php (home page + a whole lot more) </li></ul><ul><li>single.php (single posts) </li></ul><ul><li>page.php (paged pages – like your “about” page) </li></ul><ul><li>comments.php (the comments block after content) </li></ul><ul><li>header.php (before the content on EVERY page) </li></ul><ul><li>footer.php (after the content on EVERY page) </li></ul><ul><li>sidebar.php (what's in the sidebar) </li></ul><ul><li>functions.php (special theme specific PHP code) </li></ul><ul><li>style.css (text formatting and layout rules) </li></ul><ul><li>Reference: http://codex.wordpress.org/Template_Hierarchy </li></ul>
  16. 16. Which file does what? A page file (index, single, page) calls and loads other files for specific page content areas:
  17. 17. How Many Pages Could My WP Theme Have? <ul><li>The Bare Minimum </li></ul><ul><li>index.php </li></ul><ul><li>single.php </li></ul><ul><li>page.php </li></ul><ul><li>comments.php </li></ul><ul><li>header.php </li></ul><ul><li>footer.php </li></ul><ul><li>functions.php </li></ul><ul><li>sidebar.php </li></ul><ul><li>style.css </li></ul><ul><li>Optional Theme Pages </li></ul><ul><li>archive.php (date based archives) </li></ul><ul><li>search.php (search results) </li></ul><ul><li>category.php (category pages) </li></ul><ul><li>tag.php (tag pages) </li></ul><ul><li>404.php (not found errors) </li></ul><ul><li>attachment.php (attachments) </li></ul><ul><li>author.php (author page) </li></ul><ul><li>loop.php (Wordpress 3.0+) </li></ul><ul><li>Countless custom template files </li></ul>
  18. 18. Why Use Conditional Tags <ul><li>To do something only when certain conditions are met </li></ul><ul><li>Examples: </li></ul><ul><li>Show specific text or images </li></ul><ul><li>Create custom adverts for specific uses </li></ul><ul><li>Designate alternate layouts for certain pages </li></ul><ul><li>Using plugins to do this slows down your site </li></ul>
  19. 19. Where can I use Conditional Tags? <ul><li>Conditions such as “is_home” with “is_front_page”, “is_single”, “is_sticky”, “is_page”, “is_page_template”, “is_category”, “is_tag”, “is_author”, work great in sections such as: </li></ul><ul><li>index.php </li></ul><ul><li>Header or Footer </li></ul><ul><li>Sidebars </li></ul><ul><li>the_loop </li></ul><ul><li>Comments block </li></ul>
  20. 20. Very Specific Conditions <ul><li>Specify individual pages like is_single('hello') or in_category('5'), is_page_template('contact.php'), is_tag('ipod') or has_tag('ipod'), is_author('john-pratt'), is_date(), is_single(array('my-best-post', 'my-worst-post')) </li></ul><ul><li>Specific category and tag pages </li></ul><ul><li>Specific template pages </li></ul><ul><li>Specific archive pages </li></ul><ul><li>Specific post ID's </li></ul><ul><li>Reference the Codex: http://codex.wordpress.org/Conditional_Tags </li></ul>
  21. 21. Hacking with Conditional Tags Do “ABC” when “XYZ” Condition is met: <?php if (is_home()) { ?> Show my big block of SEO keyword-laden text <? } ?> By replacing “is_home” with “is_front_page”, “is_single”, “is_sticky”, “is_page”, “is_page_template”, “is_category”, “is_tag”, “is_author”, “is_date”, “is_archive”, or “is_attachment” to make your message show up on nearly any WordPress page.
  22. 22. Having Multiple Conditions Do “ABC” when “XYZ” Condition is met, elseif single post page do “DEF” <?php if (is_home()) { ?> Show my home page text block <?php } elseif (is_single()) { ?> Show my single post text block instead <? } ?> In this example we show custom text for the home page, or on single pages. If neither condition is met – we don't show any custom text.
  23. 23. Multiple Conditions Else Default Do “ABC” when “XYZ” Condition is met, elseif single post page do “DEF”, else show do default for all other instances <?php if (is_home()) { ?> Show my home page text block <?php } elseif (is_single()) { ?> Show my single post text block instead <?php else { ?> Show my default text for all other conditions <? } ?>
  24. 24. Using Multiple Conditions <ul><li>Combine conditions on one statement to show the same thing in multiple places. </li></ul><ul><li><?php if (is home() || is_single() || is_category() || is_page() || is_archive() { ?> </li></ul><ul><li>Show this text for all those conditions </li></ul><ul><li><?php } ?> </li></ul><ul><li>The double-pipe or || in the code signifies “OR”, so WordPress knows, if this is home, or a single page, or a category page, or a “page” page, or an archive page – show the same thing for all. </li></ul>
  25. 25. Show Everywhere, Except... Use the concept in reverse to show everywhere except where you don't want it... <?php if (is_home()) { } Else { ?> <p>Show this everywhere!</p> <?php } ?> In this example we show nothing when the home page is shown, “ else” show it every other time.
  26. 26. Conditionally Use WP Functions Use a Conditional tag to force a WP Function <? php if (is_home()) { wp_list_bookmarks(); } ?> Use conditional statements to do things like get_tags, get_category_parents, get_page_children, get_user_meta, and many more... Reference: http://codex.wordpress.org/Function_Reference
  27. 27. Hacking Your Comments <ul><li>Plugins like “subscribe to comments” or “CommentLuv” are great enhancements </li></ul><ul><li>By modifying the text in comments.php, you can customize what your comments block says (or how it looks) </li></ul><ul><li>By modifying some of the code, you can add (or remove functionality) </li></ul><ul><li>Get rid of comment spam at the source </li></ul>
  28. 28. Add Delete and Spam Links to Your Comment Sections <ul><li>Add this code to your theme's functions.php file: </li></ul><ul><li>function delete_comment_link($id) { if (current_user_can('edit_post')) { echo '| <a href=&quot;'.admin_url(&quot;comment.php?action=cdc&c=$id&quot;).'&quot;>del</a> '; echo '| <a href=&quot;'.admin_url(&quot;comment.php? action=cdc&dt=spam&c=$id&quot;).'&quot;>spam</a>'; } } </li></ul><ul><li>Add this code to your comments.php where you want the links ( usually add it in where you find edit_comment_link() ) : </li></ul><ul><li>delete_comment_link(get_comment_ID()); Source: http://www.wprecipes.com/how-to-add-del-and-spam-buttons-to-your-comments </li></ul>
  29. 29. Stop HTML Links in Comments <ul><li>Add this to your functions.php file, all links will be stripped: </li></ul><ul><li>function plc_comment_post( $incoming_comment ) { $incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']); $incoming_comment['comment_content'] = str_replace( &quot;'&quot;, '&apos;', $incoming_comment['comment_content'] ); return( $incoming_comment ); } </li></ul><ul><li>function plc_comment_display( $comment_to_display ) { $comment_to_display = str_replace( '&apos;', &quot;'&quot;, $comment_to_display ); return $comment_to_display; } </li></ul><ul><li>add_filter('preprocess_comment', 'plc_comment_post', '', 1); add_filter('comment_text', 'plc_comment_display', '', 1); add_filter('comment_text_rss', 'plc_comment_display', '', 1); add_filter('comment_excerpt', 'plc_comment_display', '', 1); </li></ul><ul><li>Reference: http://www.wprecipes.com/how-to-get-rid-of-links-in-your-comments </li></ul>
  30. 30. Prevent Most Comment Spam <ul><li>Most Spammers use software or scripts that have no referrer. Require all comments come from a web browser to stop the spammers dead! </li></ul><ul><li>Add this to your functions.php file: </li></ul><ul><li>function check_referrer() { if (!isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] == “”) { wp_die( __('enable referrers or go away') ); } } add_action('check_comment_flood', 'check_referrer'); </li></ul><ul><li>Source: http://www.smashingmagazine.com/2009/07/23/10-wordpress-comments-hacks/ </li></ul>
  31. 31. Hacking Navigation <ul><li>Add things to your blog that will make it more usable and help readers find what they need </li></ul><ul><li>Breadcrumb Plugins </li></ul><ul><li>Related Posts Plugins </li></ul><ul><li>Most Viewed / Popular Post Plugins </li></ul><ul><li>Category Icons Plugins </li></ul>
  32. 32. Remove Next / Previous Links <ul><li>Replace with more common “pagination” links </li></ul><ul><li>WP-Page Numbers Plugin </li></ul><ul><li>WP-PageNavi Plugin </li></ul>
  33. 33. Q&A Find me: facebook.com/jtprattmedia www.jtprattmedia.com www.jtpratt.com/blog twitter.com/jtpratt wp-dir.com