Name/Word Replacement Plugin

There may be a plugin or two out there that will do this (or that could be altered to), but it is solvable in two steps with three pretty simple php functions.

The quick approach may be to 1) add a user meta box for an author-alias applied to authors. Then 2) hook the the_content filter and preg_replace the author’s name for the alias from within the post content.

This can be added to the functions.php file of your template.

(Note: We have to hook two places for these, depending on if the user is accessing the page or an administrator).

Step One: Author-Alias Meta Box and Save Function

add_action( 'show_user_profile', 'adding_author_content_alias' );
add_action( 'edit_user_profile', 'adding_author_content_alias' );

function adding_author_content_alias( $user ) { 

    if( user_can( $user, 'edit_posts' ) ) { //hiding this box from users below author level
        ?>

        <h3>Author Alias</h3>

        <table class="form-table">

            <tr>
                <th><label for="author-alias">Author Alias</label></th>

                <td>
                    <input type="text" name="author-alias" id="author-alias" value="<?php echo esc_attr( get_the_author_meta( 'author-alias', $user->ID ) ); ?>" class="regular-text" /><br />
                    <span class="description">Add your Author Alias.</span>
                </td>
            </tr>

        </table>
        <?php 
    }//end if user_can
}//end adding_author_content_alias()

add_action( 'personal_options_update',  'save_author_content_alias' );
add_action( 'edit_user_profile_update', 'save_author_content_alias' );

function save_author_content_alias( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) ) {
        return false;
    }
    if ( isset( $_POST['author-alias'] ) ) {
        update_usermeta( $user_id, 'author-alias', sanitize_text_field( $_POST['author-alias'] ) );
    }
}//end save_author_content_alias()

quick note: in the adding_author_content_alias method above, we’re using [user_can][1] not current_user_can so we can pass the user object of the profile being viewed, rather than the object or id of the viewer, and check caps.

Step Two: Filtering the_content with author name and alias.

With the above new user meta in place, we can filter the_content rather easily:

add_filter( 'the_content', 'our_content_filter_function' );

function our_content_filter_function($content) {
    if( in_the_loop() && is_main_query() ) {

        $author = get_the_author();
        $author-alias = get_the_author_meta( 'author-alias' );

        $name_parts = explode(' ', $author); 
        $first_name = name_parts[0];
        $first_name = trim($first_name);

        $content = preg_replace( $first_name, $author-alias, $content );
        return $content;

    }//end if

    return $content;

}//end our_content_filter_function() 

The three functions and five hooks above – or some version of them – should do the trick if the methodology of that approach works for what you need.

The End 😉

– Everything below here is just further explanation for those looking for a starting point 😉

I will layout some more details for anybody else looking for a starting point for something similar:

1) The above assumes your Authors have “Display Name” set to First and Last Name, in that order. If Author’s Display Name is set to only First Name, just cut $name_parts/expode/$first_name stuff out. set $first_name = $author; or swap those variables in the preg_replace().

2) You could do all of the above name_parts/explode with php’s [list][1] if your php version isn’t going to change.

3) If you prefer to have the aliases assigned to authors further outside of their control, you could meta key/value them as options in the db perhaps. Then match the author key by the author name and set key and value to the variables for preg_replace()‘s pattern and replacement, respectively.

4) On the off chance you wish to replace all occurrences across the site of an author’s name (or to swap character’s names in serialized text, etc etc blah blah) you can pass arrays for both pattern and replacement. More on that below.

Notes: WordPress

We hook the the_content filter, since it is applied just before displaying the post, and exposes the post’s content as a string set to $content.
We aren’t changing any data in the db, but rather altering that data after the query and before it is displayed. It’s late enough that any theme or plugin should be able to jump in here, so the below could go in your theme’s functions.php without issue.

add_filter( 'the_content', 'our_content_filter_function' );

One thing to keep in mind with this filter is you may wish to check whether or not it is dealing with a main query. We used the recommended [is_main_query()][2] and [in_the_loop()][2] conditionals above for that reason, but if you need to alter things in secondary queries you may wish to exclude them.

 function our_content_filter_function($content) {
      if ( in_the_loop() && is_main_query() ) {
         //do stuff to $content here
           return $content;
     }
    return $content;
}

Notes: PHP

As mentioned above, php’s preg_replace() can accepted arrays for both the pattern and replacement, and it will match them if they are keyed and sorted. So $pattern[2] will be replaced by $replacement[2]. But you must [ksort()][2] these arrays, otherwise it will match them in the order they were enter in the code; see example 2 on php.net for more on that.

For your needs, swapping out James and replacing it with Thomas, (I’ll add a few more for the array):

 $pattern = array();
 $pattern[0] = 'James';
 $pattern[1] = 'Larry';
 $pattern[2] = 'Moe';
 $pattern[3] = 'Curly';

 $replacement = array();
 $replacement[1] = 'Groucho';
 $replacement[0] = 'Thomas';
 $replacement[3] = 'Chico';
 $replacement[2] = 'Harpo';

//makes sure our 1s match our 1s, and 2s match our 2s
ksort($pattern);
ksort($replacement);

//add in our $content string

preg_replace($pattern, $replacement, $content);

And now all together as a filter and callback:

add_filter( 'the_content', 'our_content_filter_function' );

function our_content_filter_function($content) {
    if( in_the_loop() && is_main_query() ) {
         $pattern = array();
         $pattern[0] = 'James';
         $pattern[1] = 'Larry';
         $pattern[2] = 'Moe';
         $pattern[3] = 'Curly';

         $replacement = array();
         $replacement[1] = 'Groucho';
         $replacement[0] = 'Thomas';
         $replacement[3] = 'Chico';
         $replacement[2] = 'Harpo';

         //makes sure our 1s match our 1s, and 2s match our 2s
         ksort($pattern);
         ksort($replacement);

        //add in our $content string

       preg_replace($pattern, $replacement, $content);
       return $content;
    }
    return $content;
}