Renaming a Shortcode Programmatically

It does happen to change mind. But when for some reason we change mind on the name of a shortcode become almost ubiquitous amongst our Posts, Pages and Custom Posts, the only reasonable way of renaming all its occurrences is programmatically — writing scripts all the monotonous work can be delegated to is a habit that, trust me, pays off over time.

The snippet of code that follows is a generalisation of a simpler script I wrote for the migration of data from SiteTree to The Permalinks Cascade, it is basically an implementation of the find-and-replace task iterated over all the posts showing a glimpse of the shortcode we want to rename. Most of its magic is condensed in the regular expressions, used to spot the occurrences of the shortcode which have not been escaped with double square brackets. To accomplish this, the two regex use a negative lookaround, a zero-length assertion you can learn everything about on the website.

$old_shortcode_name = 'my-shortcode';
$new_shortcode_name = 'my-new-shortcode';

// We do a preliminary sorting of the posts by using
// the search feature built into the WP_Query class, 
// so as to retrieve from the database only the posts  
// that might contain the shortcode to rename.
$arguments = array(
    'posts_per_page' => -1,
    'post_type'      => 'any',
    's'              => "[{$old_shortcode_name}"

$query = new WP_Query( $arguments );

// The negative look-behind and the negative look-ahead in 
// the regular expressions ensure that shortcodes escaped 
// with double square brackets are not matched.
// Also, the capturing group in the first regex is 
// used to capture the list of attributes that 
// the shortcode might include. 
$patterns = array(
    "#(?<!\[)\[{$old_shortcode_name}( [^\]]+)?\]#",

// The match variable '$1' will be substituted either with 
// an empty string or a list of shortcode attributes.
$replacements = array(

while ( $query->have_posts() ) {

    $the_content = get_the_content();

    // If $the_content doesn't contain non-escaped occurrences of
    // the shortcode we want to rename, we just go on to analyse
    // the next post.
    if (! preg_match( $patterns[0], $the_content ) ) {
    // We get the current post as an associative array.
    $the_post = get_post( null, ARRAY_A );

    if (! $the_post ) {

    $the_post['post_content'] = preg_replace( $patterns,
                                              $the_content );

    // We let WordPress automatically update the post's modification date.
    unset( $the_post['post_modified'], $the_post['post_modified_gmt'] );

    // The last parameter hinders WordPress from triggering
    // the after-insert hooks.
    wp_update_post( $the_post, false, false );

// Mandatory when we create a secondary loop through WP_Query. 

The Edge Case

The presence of nested shortcodes in one or more of your posts is certainly not such an improbability that it should be tagged as edge case, but an escaped shortcode that wraps a very occurrence of the shortcode to rename is without doubt an edge case. So, if somewhere in your website you have written something like that...

    [my-shortcode arg="value"]
[/my-outer-shortcode]] would be better to preventively exclude from the query the posts containing such pseudo-code. You just have to pass to the constructor of WP_Query a list of IDs as the value of the post__not_in argument.

Running the code: Where and When

Given a generic context, you can simply put the code in the functions.php file of your active theme, and delete it once the renaming is complete. As a precaution however, I would suggest to let WordPress run it when the init hook fires, like so:

function rename_my_shortcode() {
    // The snippet goes here.
add_action( 'init', 'rename_my_shortcode' );

Running the code: A Final Note

Running the snippet only once is more than enough, however, in the event you let the snippet run more than once, you don't have to worry, because the conditional statements it comes with will act as a deterrent to unexpected results.