Delete specific shortcode maintaining content

There is a way to delete a specific shortcode, maintaining the text inside?

For example: in this case [dropcap]A[/dropcap] I would like to eliminate the shortcode maintaining the “A”, or any other letter inside.

Read More

Thanks!

Related posts

Leave a Reply

2 comments

  1. Use this JS regex to strip out any characters between square brackets from the text, while leaving any text that might’ve come beween shortcode tags, like [dropcap]A[/dropcap].

    var myReg = /[.+]/g;
    paragraphText = paragraphText.replace(myReg, '');
    

    or to remove a shortcode like:

    [media-credit param=value param2="value2"]text you actually want goes here[/media-credit]
    

    You can use the following to your functions.php file:

    add_filter( 'the_excerpt', 'remove_media_credit_from_excerpt' );
    function remove_media_credit_from_excerpt( $excerpt ) {
        return preg_replace ('/[media-credit[^]]*](.*)[/media-credit]/', '$1', $excerpt);
    }
    
  2. Here’s a plugin that will launch one time and parse ALL POSTS’ content, stripping the shortcodes (and leaving the content) of any desired shortcodes. Simply enter the shortcodes you want to strip in the $shortcode_tags array and the post type you want to perform on in the $posts array.

    NOTE: This will impact your database and can NOT be undone. It is highly recommended that you backup your database first.

    <?php
    /*
    Plugin Name: Strip Shortcodes Example
    */
    
    add_action( 'init', '_replace_shortcodes' );
    
    function _replace_shortcodes() {
    
        global $shortcode_tags;
    
        // Make sure this only happens ONE time
        if ( get_option( '_replace_shortcodes_did_once' ) !== false ) {
            return;
        }
    
        update_option( '_replace_shortcodes_did_once', true );
    
        add_action( 'admin_notices', '_replace_shortcodes_notice' );
    
        // Get all of our posts
        $posts = get_posts( array(
            'numberposts' => -1,
            'post_type' => 'post', // Change this for other post types (can be "any")
        ));
    
        // Make WP think this is the only shortcode when getting the regex (put in all shortcodes to perform on here)
        $orig_shortcode_tags = $shortcode_tags;
        $shortcode_tags = array(
            'dropcap' => null,
        );
        $shortcode_regex = get_shortcode_regex();
        $shortcode_tags = $orig_shortcode_tags;
    
        // Loop through the posts
        if ( ! empty( $posts ) ) {
            foreach ( $posts as $post ) {
    
                // Perform Regex to strip shortcodes for given shortcodes
                $post_content = preg_replace_callback( "/$shortcode_regex/s", '_replace_shortcodes_callback', $post->post_content );
    
                // Update our post in the database
                wp_update_post( array(
                    'ID' => $post->ID,
                    'post_content' => $post_content,
                ) );
            }    
        }
    }
    
    function _replace_shortcodes_callback( $matches ) {
    
        // This is the shortcode content
        $content = $matches[5];
    
        // No content, so just return the whole thing unmodified
        if ( empty( $content ) ) {
            return $matches[0];
        }
    
        // Otherwise, just return the content (with no shortcodes)
        return $content;
    }
    
    function _replace_shortcodes_notice() {
        ?>
        <div class="updated">
            <p>
                All posts' content updated.
            </p>
        </div>
    <?php
    }