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:

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

}

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?

About Aaron Jorbin

Aaron Jorbin is an engineer with Clearspring where he works on AddThis and a WordPress Core Contributor. He has spoken to multiple User Groups and at WordCamps in four time zones. When he's not busy creating and fixing bugs, Aaron helps run an educational simulation conference for over 1500 college students. He'll gladly toast to the GPL any day of the week and happily will discuss whisky, quality beer, or the upper peninsula of Michigan with anyone that wants.
This entry was posted in Code, Programming, WordPress and tagged , , . Bookmark the permalink.

One Response to Personalized Read More

  1. Ozh says:

    Cool idea :) That would be neat to apply this trick not only to logged in users but also if there’s a commenter name cookie

Leave a Reply

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

* * . Now that I have read Personalized Read More, I wanted to say:

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">