Remove Words from WordPress Post Titles Function Using PHP Regex and Shortcodes

by | February 28, 2023 | PHP, Website Development

This page may contain affiliate links to recommended products or services. If you choose to purchase after clicking a link, I may receive a commission at no additional cost to you.

In this article, I will explain how to remove a word from the end of WordPress post titles using regex inside of a PHP function that can be added to your WordPress theme’s functions.php file. Here, the method’s action runs on the init hook, which will happen as soon as you load a new page. Alternatively, you could skip declaring the action and tie it to a shortcode that runs when and where you tell it to, but that’s up to you.

Code

Here’s the PHP function. Add it to your WordPress theme’s functions.php file.

function remove_word_from_post_titles() {
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => -1,
    );
    $posts = get_posts($args);

    foreach ($posts as $post) {
        $title = $post->post_title;
        // Use regex to check if the title ends with "Word"
        if (preg_match('/Word$/', $title)) {
            // Remove "Word" from the title and trim any trailing whitespace
            $new_title = rtrim(preg_replace('/Word$/', '', $title));
            // Update the post title
            wp_update_post(array(
                'ID' => $post->ID,
                'post_title' => $new_title,
            ));
        }
    }
}
add_action('init', 'remove_word_from_post_titles');

Explanation

The function uses the WordPress get_posts() function to retrieve all posts of the “post” post type and then iterates over each post’s title to check if it ends with “Word”. If it does, it removes “Word” from the title and updates the post using the wp_update_post() function.

The function is hooked to the init action, which means it will be executed when WordPress initializes. Once you add this function to your site, it will automatically remove the word “Word” from all post titles of the “post” post type.

Bonus Shortcode

Here’s an example of how you could create a WordPress shortcode to run the function instead of executing it on init. Shortcodes are a useful method of running PHP code inside WordPress content without actually adding PHP inside of the content (because WordPress doesn’t allow you to do that). Same as before, append this update to your WordPress theme’s functions.php file.

// Comment out or remove the add_action() to prevent automatic execution.
// add_action('init', 'remove_word_from_post_titles');

/*
* Custom shortcode to run the remove_word_from_post_titles() function.
*/
function remove_text_shortcode( $atts ) {
    ob_start();
    remove_word_from_post_titles();
    return ob_get_clean();
}
add_shortcode( 'remove_text', 'remove_text_shortcode' );

To use the shortcode, simply add [remove_text] to any post or page. You can add it in a normal text block or in a short code block. When the page is loaded, the shortcode will execute the remove_word_from_post_titles() function.

Case Study

As a real-world example of its utility, I used a variation of this function to remove an unwanted word from the trailing end of product titles for a small business’s WordPress website. The post-type was products. The trailing text was an unnecessary word that was preventing the product titles from being re-purposed inside of dynamically generated content. And, the desire was to remove the word from whichever products were using it. As a result of the function, I was able to achieve the business owner’s objectives.

In practice, there were minimal changes to the code. I changed the post type from post to product because that’s how the website’s product content was structured and I changed the reference from “Word” to the actual word that needed to be removed. These small edits allowed me to remove the trailing text, update the titles without affecting the permalink URLs, and provide a grammatically sound version of the titles which could be re-used throughout the additional page content as desired.

In my opinion, this solution created an elegant way of streamlining the website’s copywriting by programmatically creating unique descriptions for each product without requiring the actual writing of unique descriptions for each product. It saved time, money, and brain power by working smarter instead of harder. Success.

Floyd Hartford is a website developer from southern Maine. He's focused on creating and building WordPress websites and loves spending time digging into code like HTML, CSS, scss, jQuery, PHP, and MySQL.

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

thirteen − 6 =

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Looking for WordPress Help?

I’m a freelance web developer specializing in small business and corporate marketing websites.

Pin It on Pinterest