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.

Read more here: How to get shorcode’s input values inside a filter?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: