Search and replace in MySQL

Currently, I want to wrap all acronyms – »PHP«, for example – in a span element to display them as small capitals. So I searched for a way to search and replace in MySQL, and there it is: REGEXP_REPLACE.

For example, the following command wraps WOFF in the post titles in the wanted markup (it is a good idea to create a backup before):

UPDATE `wp_posts` SET `post_title` = REGEXP_REPLACE(post_title COLLATE utf8mb4_bin, 'WOFF', '<span class="smcp">WOFF</span>' ) WHERE `ID` = 4691
Code language: SQL (Structured Query Language) (sql)

The first param of REGEXP_REPLACE is the string to search in – we set the column’s name we want to search in. The second parameter is the searched string and the third the string to replace the searched string with. Setting COLLATE utf8mb4_bin (you may need to adjust the collation if you use another one) is important because otherwise, the search would be case insensitive.

Like written in the MariaDB documentation (and as the name suggests) you can also use regular expressions with REGEXP_REPLACE. For my German version of the weekly recap, I used the following:

UPDATE `wp_posts` SET `post_title` = REGEXP_REPLACE(post_title COLLATE utf8mb4_bin, 'KW([0-9]+)', '<span class="smcp">KW</span>\\1' ) WHERE `ID` = 4691
Code language: SQL (Structured Query Language) (sql)

\\1 inserts the part inside the brackets from the searched string into the replace string: the week number.

WordPress weekly recap #22: no auto updates for 4.9.6 and more

Because of the larger amount of new code in 4.9.6, the team decided to disable the automatic updates for a day to wait for incoming issues. There were some issues with plugins that use the wrong hook for the new privacy features that caused a white screen, so the team kept the feature disabled.

Continue reading "WordPress weekly recap #22: no auto updates for 4.9.6 and more"