WordPress – Overwriting a Shortcode

I have a theme that extends the Visual Composer plugin with a slider on the front page. The slider will show five testimonials from five different customers. I want to add the featured image of each testimonial as the thumbnail in the slider.

Here’s the shortened code from the parent theme:

Read More
function jo_customers_testimonials_slider( $atts ) {
    extract( shortcode_atts( array( 'limit' => 5, "widget_title" => __('What Are People Saying', 'jo'), 'text_color' => "#000" ), $atts ) );
    $content = "";
    $loopArgs = array( "post_type" => "customers", "posts_per_page" => $limit, 'ignore_sticky_posts' => 1 );

    $postsLoop = new WP_Query( $loopArgs );
    $content = "";

    $content .= '...';
    $content .= '...';
    $content .= '...';

    wp_reset_query();
    return $content;
}
add_shortcode( 'jo_customers_testimonials_slider', 'jo_customers_testimonials_slider' ); 

My functions.php file:

function jo_customers_testimonials_slider_with_thumbnail( $atts ) {
    extract( shortcode_atts( array( 'limit' => 5, "widget_title" => __('What Are People Saying', 'jo'), 'text_color' => "#000" ), $atts ) );
    $content = "";
    $loopArgs = array( "post_type" => "customers", "posts_per_page" => $limit, 'ignore_sticky_posts' => 1 );

    $postsLoop = new WP_Query( $loopArgs );
    $content = "";

    $content .= '...';
    $content .= get_the_post_thumbnail( get_the_ID(), 'thumbnail' );
    $content .= '...';
    $content .= '...';

    wp_reset_query();
    return $content;
}
add_shortcode( 'jo_customers_testimonials_slider', 'jo_customers_testimonials_slider_with_thumbnail' );

In theory, the function from my functions.php file should overwrite the shortcode from the parent theme. But nothing seems to happen when I use this code. What am I doing wrong?

Edit:

Tried this code, but it still won’t work.

function wpa_add_child_shortcodes(){
remove_shortcode('jo_customers_testimonials_slider');
    add_shortcode( 'jo_customers_testimonials_slider', 'jo_customers_testimonials_slider_with_thumbnail' );
}
add_action( 'after_setup_theme', 'wpa_add_child_shortcodes' );

Also changed

add_action( 'after_setup_theme', 'wpa_add_child_shortcodes' ); to

add_action( 'init', 'wpa_add_child_shortcodes' );

, but no difference in the outcome.

Edit 2 (With Solution):

Changing add_action( 'after_setup_theme', 'wpa_add_child_shortcodes' ); to add_action( 'wp_loaded', 'wpa_add_child_shortcodes' ); solved it.

Related posts

Leave a Reply

2 comments

  1. you need to call remove_shortcode(); like this:

    remove_shortcode('jo_customers_testimonials_slider');` 
    

    Before you add your new shortcode with the same name to “overwrite” it.

    You’ll also need to call it after the parent theme has run so we fire on an action hook called wp_loaded.

    function overwrite_shortcode() {
    
        function jo_customers_testimonials_slider_with_thumbnail($atts) {
            extract(shortcode_atts(array('limit' => 5, "widget_title" => __('What Are People Saying', 'jo'), 'text_color' => "#000"), $atts));
            $content = "";
            $loopArgs = array("post_type" => "customers", "posts_per_page" => $limit, 'ignore_sticky_posts' => 1);
    
            $postsLoop = new WP_Query($loopArgs);
            $content = "";
    
            $content .= '...';
            $content .= get_the_post_thumbnail(get_the_ID(), 'thumbnail');
            $content .= '...';
            $content .= '...';
    
            wp_reset_query();
            return $content;
        }
    
        remove_shortcode('jo_customers_testimonials_slider');
        add_shortcode('jo_customers_testimonials_slider', 'jo_customers_testimonials_slider_with_thumbnail');
    }
    
    add_action('wp_loaded', 'overwrite_shortcode');
    
  2. You have to write this code in your Child Theme’s functions.php

    add_action( 'after_setup_theme', 'calling_child_theme_setup' );
    
    function calling_child_theme_setup() {
       remove_shortcode( 'parent_shortcode_function' );
       add_shortcode( 'shortcode_name', 'child_shortcode_function' );
    }
    
    function child_shortcode_function( $atts) {
        $atts = shortcode_atts( array(
            'img'  => '',
            'cat'  => '',
            'capt' => '',
            'link' => ''
        ), $atts );
    
    //YOUR OWN CODE HERE
    
        $imgSrc = wp_get_attachment_image_src( $atts['img'], 'delicious-gallery' );
    
        $imgFull = wp_get_attachment_image_src( $atts['img'], 'full' );
    
    
    
        $b = '<div class="screen-item" data-groups='["'.strtolower(str_replace(' ', '_', $atts["cat"])).'", "all"]'>'.
    
            '<a href="'.$atts["link"].'" data-title="'.$atts["capt"].'" target="_blank"><img src="'.$imgSrc[0].'" alt="SCREEN" class="screenImg" /></a>'.
    
            '<span>'.$atts["capt"].'</span>'.
    
        '</div>';
    
    //YOUR OWN CODE HERE
    
        return $b;
    }