• in my main plugin I filter the_content to add rich snippet, something like this:

    favorite
    
    in my main plugin I filter the_content to add rich snippet, something like this:
    
    add_filter('the_content', 'add_schema');
    
    function add_schema($content) {
            $schema=NULL; //To avoid undefined variable notice outside the loop
    
            $review_choosen = get_snippet_type();
    
            //Use apply filter to allow users to customize the rich snippet
            $schema = apply_filters( 'filter_schema_microdata', $review_choosen )
    
            //If $schema is not false mean the user added his filter and return this one
            if ($schema) {
    
                return $content . $schema;
    
            }
    
           //DEFAULT CODE HERE
    
    }

    At the begin of the function, I use apply_filter to let my user or other developer to customize the rich snippet instead use the defaults that my plugin adds.

    If the add_filter is put on functions.php, everything is ok.

    But now I’m working on an extension that run on plugins_loaded. So when I try to use add_filter here this is what happens:

    If I use

    add_filter ( 'filter_schema_microdata', 'callback_function' )
        function callback_function ($review_choosen) {

    the $review_choosen var is empty.

    If instead, I try to put an higher priority, e.g. 9

    add_filter ( 'filter_schema_microdata', 'callback_function', 9 )
       function callback_function ($review_choosen) {

    The $review_choosen var is ok, but the functions doesn’t return nothing.

    So in which action should I run the add_filter?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    You can try ‘init’, though ‘plugins_loaded’ should be fine. Changing the priority should make no difference if your callback_function() is the only callback added to ‘filter_schema_microdata’.

    The priority only affects which order callbacks are called in. With only one callback, the parameter does nothing. At least that’s how it’s supposed to work. The behavior you describe makes no sense in that respect. I don’t doubt your observation, I just cannot explain it. I have to think something else is at play here. Is there any chance the filter tag name is used by another plugin? Or something else you’ve coded? It’s always a good idea to prepend some unique characters to function and tag names to avoid name collisions.

    Thread Starter dudo

    (@dudo)

    Hi bcworks, thank you for your answer.

    I’m felling an idiot today, after a good sleep I just remember that in another part of the plugin, same filter were running and the callback function was simply “return FALSE”.
    I put a lower priority on this function and everything is fine.

    Of course, on my code all the function has an unique characters, (prefix_function_name) here I put just a more generic code 🙂

    Best,
    Dario

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Question about filter / action priority’ is closed to new replies.