Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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.

like image 628
Daniel Harris Avatar asked Jan 21 '14 17:01

Daniel Harris


1 Answers

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');
like image 190
Clark T. Avatar answered Sep 19 '22 13:09

Clark T.