WordCamp Detroit 2010 Wordpress Theme Hacks

Preview:

DESCRIPTION

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

Citation preview

Wordpress Theme Hacks Nearly Anyone Can Do

Afraid you might "break something" 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.

How I Met Wordpress

THIS WAS MEFor

10 LONG YEARS

In 1999 I was hired as a webmaster

I was VERY loyal

Webmasters Did Everything

• HTML

• Javascript

• Graphics

• Technical Writing

• CGI/Perl Coding

• UNIX Shell Coding

• Create Online Tools

• Training

Open Source CMS

I tried to introduce multiple Open Source CMSSystems to Automate my Job

Big Money Always Won

I Met Wordpress in 2004

I Thought They Were Wrong...

So I kept using Wordpress on My Own...

Sometimes instead of doing my real work.

Little Did I Know...

In 2009 they outsourced the last of us...the last of the “webmasters” that is.

I Was Out of Work

No Jobs. Unemployment.Couldn't Move.

What Now?

Reinvention

Wordpress Theme Hacks Nearly Anyone Can Do

You Will Need to: Use the Wordpress

dashboard editor Know basic HTML Be able to Edit, Copy

and Paste small amounts of PHP Code

Why Hack a Wordpress Theme?To stand out or look different

create a look nobody else hasTo add functionality for the end user

“like”, “tweet”, disqusTo enhance the way content is displayed

post meta, text, images, “the_loop”, navigationTo customize specific pages

search results, 404 Pages, archives, category & tags pagesTo show content only when conditions are met

only on the home page, just singles posts, only tag pages, etc.

How Do I Edit My WP Theme?

In WP dashboard: Appearance → EditorClick on the page you want to edit:

What Are All Those Theme Pages?

At Bare Minimum, Most WP Themes Have:index.php (home page + a whole lot more)single.php (single posts)page.php (paged pages – like your “about” page)comments.php (the comments block after content)header.php (before the content on EVERY page)footer.php (after the content on EVERY page)sidebar.php (what's in the sidebar)functions.php (special theme specific PHP code)style.css (text formatting and layout rules)

Reference: http://codex.wordpress.org/Template_Hierarchy

Which file does what?

A page file (index, single, page) calls and loads other files for specific page content areas:

How Many Pages Could MyWP Theme Have?

The Bare Minimum index.php

single.php

page.php

comments.php

header.php

footer.php

functions.php

sidebar.php

style.css

Optional Theme Pages archive.php (date based archives)

search.php (search results)

category.php (category pages)

tag.php (tag pages)

404.php (not found errors)

attachment.php (attachments)

author.php (author page)

loop.php (Wordpress 3.0+)

Countless custom template files

Why Use Conditional Tags

To do something only whencertain conditions are met

Examples: Show specific text or images Create custom adverts for specific uses Designate alternate layouts for certain pages Using plugins to do this slows down your site

Where can I use Conditional Tags?

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:

index.php Header or Footer Sidebars the_loop Comments block

Very Specific Conditions

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'))

Specific category and tag pages Specific template pages Specific archive pages Specific post ID'sReference the Codex:

http://codex.wordpress.org/Conditional_Tags

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.

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.

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

<? } ?>

Using Multiple Conditions

Combine conditions on one statement to show the same thing in multiple places.

<?php if (is home() || is_single() || is_category() || is_page() || is_archive() { ?>

Show this text for all those conditions

<?php } ?>

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.

Show Everywhere, Except...

Use the concept in reverse to show everywhereexcept 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.

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

Hacking Your Comments

Plugins like “subscribe to comments” or “CommentLuv” are great enhancements

By modifying the text in comments.php, you can customize what your comments block says (or how it looks)

By modifying some of the code, you can add (or remove functionality)

Get rid of comment spam at the source

Add Delete and Spam Linksto Your Comment Sections

Add this code to your theme's functions.php file:

function delete_comment_link($id) { if (current_user_can('edit_post')) { echo '| <a href="'.admin_url("comment.php?action=cdc&c=$id").'">del</a> '; echo '| <a href="'.admin_url("comment.php?action=cdc&dt=spam&c=$id").'">spam</a>'; }}

Add this code to your comments.php where you want the links (usually add it in where you find edit_comment_link()):

delete_comment_link(get_comment_ID());

Source: http://www.wprecipes.com/how-to-add-del-and-spam-buttons-to-your-comments

Stop HTML Links in Comments

Add this to your functions.php file, all links will be stripped:

function plc_comment_post( $incoming_comment ) {$incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);$incoming_comment['comment_content'] = str_replace( "'", '&apos;', $incoming_comment['comment_content'] );return( $incoming_comment );}

function plc_comment_display( $comment_to_display ) {$comment_to_display = str_replace( '&apos;', "'", $comment_to_display );return $comment_to_display;}

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);

Reference: http://www.wprecipes.com/how-to-get-rid-of-links-in-your-comments

Prevent Most Comment Spam

Most Spammers use software or scripts that have no referrer. Require all comments come from a web browser to stop the spammers dead!

Add this to your functions.php file:

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');

Source: http://www.smashingmagazine.com/2009/07/23/10-wordpress-comments-hacks/

Hacking Navigation

Add things to your blog that will make it more usable and help readers find what they need

Breadcrumb Plugins Related Posts Plugins Most Viewed / Popular Post Plugins Category Icons Plugins

Remove Next / Previous Links

Replace with more common “pagination” links

WP-Page Numbers Plugin WP-PageNavi Plugin

Q&A

Find me:

facebook.com/jtprattmedia

www.jtprattmedia.com

www.jtpratt.com/blog

twitter.com/jtpratt

wp-dir.com