Follow Brian Li (@DecryptoBL) on Twitter.
Follow Brian Li (@bwhli) on Instagram.

How to Replace Text in WordPress with functions.php

January 8, 2019

This snippet can be used in a WordPress theme’s functions.php file to dynamically replace text on a specific part of the page. This example replaces text strings in the_content and nowhere else.

function replace_text($text) {
	$text = str_replace('<hr>', '<hr class="hr-small">', $text);
	$text = str_replace('<hr />', '<hr class="hr-small">', $text);
	$text = str_replace('>^<', 'class="rs">^<', $text);
	$text = str_replace('<sup>', '<sup class="rs-sup">', $text);
	return $text;
}
add_filter('the_content', 'replace_text');

Redirect 7xx px wide images to full-size images in WordPress with NGINX

December 30, 2018

Recently. I noticed a bunch of 404 errors involving 700px and 768px wide images. These were “large” image sizes I used in previous WordPress themes. After doing a regex search and replace on my database to get rid of all instances of -7\d{2}x\d{3}, I added the following NGINX rule to redirect any potential external direct links to the images.

location ~* "^\/wp-content\/uploads\/(.*)(-7\d{2}x\d{3})(..{3})$"  {
   return 301 https://decrypto.net/wp-content/uploads/$1$3;
}


How to Disable Gutenberg in WordPress 5.0

December 8, 2018

Add the line below to your theme’s functions.php file to disable the new Gutenberg editor in WordPress 5.0.

/* Disable the Gutenberg editor. */
add_filter('use_block_editor_for_post', '__return_false');

There’s also an official Classic Editor plugin if you want to go that route instead.




Questions?

Find me on Twitter, or send me an email.