I’m trying to get the input value of a shortcode inside a function that is used by a filter, but there seems to be no success. Here is what i did:

function my_shortcode_function($atts){
    $value = $atts['id'];
    function filter_value(){
        echo $value;
    }
    add_filter('posts_where','filter_value');
}
add_shortcode('my-shortcode','my_shortcode_function');

Now i know using $value inside filter_value() won’t work because of variable scopes, but even using $GLOBALS['value'] doesn’t work.

I even tried using $value = $atts['id'] inside the filter_value(); but no success either.

How can i use my shortcode like [my-shortcode id='123'] and pass the 123 value to the filter?

Thanks.

4 s
4

Using a global variable will work. Here’s a demonstration:

function wpse_shortcode_function( $atts ){
    // User provided values are stored in $atts.
    // Default values are passed to shortcode_atts() below.
    // Merged values are stored in the $a array.
    $a = shortcode_atts( [
                'id'   => false,
    ], $atts );

    // Set global variable $value using value of shortcode's id attribute.
    $GLOBALS['value'] = $a['id'];

    // Add our filter and do a query.
    add_filter( 'posts_where', 'wpse_filter_value' );

    $my_query = new WP_Query( [
        'p' => $GLOBALS['value'],
    ] );

    if ( $my_query->have_posts() ) {
        while ( $my_query->have_posts() ) {
            $my_query->the_post();
            the_title( '<h1>', '</h1>');
        }
        wp_reset_postdata();
    }

    // Disable the filter.
    remove_filter( 'posts_where', 'wpse_filter_value' );
}
add_shortcode( 'my-shortcode', 'wpse_shortcode_function' );

function wpse_filter_value( $where ){
    // $GLOBALS['value'] is accessible here.

    // exit ( print_r( $GLOBALS['value'] ) );

    return $where;
}

Side note, declaring a function within another function is not a good practice.

Leave a Reply

Your email address will not be published. Required fields are marked *