Skip to main content

Using Hooks to Replace WordPress Content Inline

By April 19, 2020WordPress
Male hands typing programming code or text on laptop

Something I did recently when I updated the Better AdSense Targeting plugin, was create the ability to allow users to specify certain sections of each post that Google should ignore when determining what ads to display. I did this by using the str_replace() function in PHP and a little thing called ‘add_filter’ from the WordPress hooks. It’s rather simple and looks a little like the following. First let’s think of something we might want to replace. Let’s say we want to make every instance of the string ‘[my other site]’ turn into a link to your another website you run. Typically you’d need to code that out or use all the tools in the editing screen to do this, but instead, we cause use the power of str_replace() to do that for us. We’ll need to include this fix in a plugin or the functions.php of your theme.

First let’s make sure we’re adding the ‘filter’ on the content that we are taking from the post loop.

[php] add_filter( ‘the_content’, ‘url_replace’ );
[/php]

This line simply runs the function url_replace() whenever the_content() is run. Next we’ll need to create the actual function that does the replacing:

[php] function url_replace($content=”) {
return str_replace( ‘[my other site]’, ‘My Other Site’, $content);
}
[/php]

A little explanation here, we are running the function and are looking for instances of ‘[my other site]’ and replacing it with the full HTML anchor tag and text in the string $content. If you are wanting to replace multiple words or phrases you’ll need to do something like the following:

[php] function multi_replace($content=”) {
$content = str_replace( ‘[my other site]’, ‘My Other Site’, $content);
$content = str_replace( ‘[WordPress]’, ‘WordPress.org’, $content);

return $content;
}
[/php]

I don’t think I have to explain much here now, but we’re simply replacing any instance of ‘[my other site]’ with the full HTML link and we are also looking for ‘[WordPress]’ and making it link to WordPress.org. Easy enough, it creates links for you quickly, and allows you to focus more on writing. I find it common practice to put things like [WordPress] in and then later look for these strings in my drafts and replace them with links. Anytime I can avoid breaking my writing flow, I do, and shorthand notes are the way to go.

There you have it, you’ve successfully replaced content. Where this is useful is if you wish to include specific code wherever need be. You don’t have to replace this text with visible text. For instance, my AdSense Plugin replaces the string ‘[ignore]’ and ‘[/ignore]’ with the proper HTML that I need to use to have Google AdSense ignore the content between these tags when selecting ads.

In this practice I used the str_replace() function, which is great for unique strings like [my other site] and [WordPress] however it can cause some issues when simply replacing normal text by including parts of other words as long as they meet the requirements of the replacement. For example, the word ‘and’ would be replaced in the words ‘brand’, ‘sand’, or ‘stand’. If you are looking for a more indepth and intensive replace you might want to try using preg_replace() which uses Regular Expressions for it’s replacement rules