Personalized Read More

Want to make the read more link in WordPress call out the user if they are logged in?  As part of my DC WordPress presentation, i showed that it’s easy as pie and only about 10 lines of code.

The plugin has two parts, the first is the filter that we are adding, and the second is the callback that will do the filtering. All together, it looks like:

[php]
add_filter(‘the_content_more_link’, ‘jorbin_the_content_more_link_filter’, 10, 2);

function jorbin_the_content_more_link_filter($original, $more_link_text){
global $post;

if( is_user_logged_in() ) {
global $current_user;
get_currentuserinfo();
$more_link_text = "Hey {$current_user->display_name}, why don’t you read more";
}

return ‘<a href="’ . get_permalink() . "#more-{$post->ID}\" class=’more-link’>$more_link_text</a>";

}
[/php]

The callback works by checking if our current user is logged in, if they are we use the $current_user global that we know will be fully populated by get_currentuserinfo() and change the $more_link_text. Otherwise the $more_link_text is the same as what it was originally. Either way, we return the full link. Pretty easy, eh?

One thought on “Personalized Read More”

Leave a Reply

Your email address will not be published. Required fields are marked *