A Problem with Read More Link URLs and Webtreats inFocus WordPress Theme: Solved

A

Just last week I posted my solution for allowing users of the WebTreats inFocus theme for WordPress to choose how post excerpts are displayed. I am of course using this solution on my site. Yesterday I noticed some rather odd-looking URLs associated with a couple of the Read More links on my blog.

These instructions are only applicable for versions of inFocus up to and including v1.6

For example, I have a post entitled Outbrain and Blogger Read More Links: A Better Solution. The post’s permalink is;

http://www.tech-otaku.com/blogging/outbrain-blogger-readmore-links-solution/

However, the URL associated with this post’s Read More link was;

http://www.tech-otaku.com/blogging/outbrain-blogger-readmore-link button_links-solution/#more-252

The text readmore-links in the URL had been replaced with readmore-link button_links. Clicking the post’s Read More link resulted in a 404 – Not Found as that URL doesn’t exist.

The reason why WordPress seemed to be arbitrarily changing URLs was not immediately apparent, but after a fair bit of digging I came up with the answer.

When posts are displayed one of the functions that is executed is get_the_content() located in wp-includes/post-template.php. This function handles Read More links and makes this call;

$output .= apply_filters(
	'the_content_more_link', ' <a href="' . get_permalink() . "#more-$id\" class=\"more-link\">$more_link_text</a>", $more_link_text
	);

 

The first argument to apply_filters() is a filter named the_content_more_link. This filter is applied to the Read More link before being displayed to the browser. Exactly what this filter was doing though was still a mystery.

However, one of the inFocus theme files, lib/functions/theme-functions.php, includes this code;

function new_more_link( $more_link, $more_link_text ) {
	return str_replace('more-link', 'more-link button_link', $more_link );
}
add_filter('the_content_more_link', 'new_more_link', 10, 2);

 

Here, a new function named new_more_link() is declared. More about what this function does shortly. Below this function declaration is a call to the add_filter() function which associates the new_more_link() function with the the_content_more_link filter. In short, the new_more_link() function will be executed with the call to apply_filters() in post-template.php.

Before apply_filters() and subsequently new_more_link() are executed the HTML for the Read More link looks like this;

<a href="http://www.tech-otaku.com/blogging/outbrain-blogger-readmore-links-solution/#more-252" class="more-link">
	<span class='button_link'>More</span>
</a>

 

The <a> tag currently has a class of more-link. The new_more_link() function’s purpose is to add the button_link class to the <a> tag by replacing every occurrence of more-link with more-link button_link using str_replace(). The result is;

<a href="http://www.tech-otaku.com/blogging/outbrain-blogger-readmore-link button_links-solution/#more-252" class="more-link button_link">
	<span class='button_link'>More</span>
</a>

 

Unfortunately, The text readmore-links in the URL is also replaced with readmore-link button_links. Hence the rather odd URL.

The solution then was to find a better way of matching the appropriate text in the Read More link’s URL. The rather limited str_replace() wasn’t up to the job so I thought I’d give preg_replace() a try. This is the amended code for the new_more_link() function;

function new_more_link( $more_link, $more_link_text ) {
	return preg_replace("/[\"\']more-link(?!s)[\"\']/", "\"more-link button_link\"", $more_link);
}
add_filter('the_content_more_link', 'new_more_link', 10, 2);

 

The first argument in preg_replace() is a regular expression that will match any occurrence of more-link providing it is surrounded by double or single quotes. So it will match more-link in class=“more-link” or class=‘more-link’ but not /outbrain-blogger-readmore-links-solution/#more-252.

While my regular expressions are not up to much, this certainly solves the problem.

Admittedly, this issue will have very little impact for most people, but if you’re experiencing the same problems with URLs as I was then this may well be the answer.

About the author

A native Brit exiled in Japan, Steve spends too much of his time struggling with the Japanese language, dreaming of fish & chips and writing the occasional blog post he hopes others will find helpful.

2 responses

2 Comments

Steve

Recent Comments

Recent Posts

Archives