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$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.

How to Fix WordPress Character Encoding

November 19, 2018

After a recent update to my About page, I saw that Japanese characters were being rendered as question marks. Since Japanese characters were immediately changed to question marks after hitting the “Update” button in the WordPress editor, I quickly ruled out a font-related bug as the cause of the issue. After some Googling and subsequent database exploration, I discovered that my web host’s default database character set is latin1 (ISO-8859-1), which doesn’t support Japanese characters.

Continue Reading →


Find me on Twitter, or send me an email.