WordPress Custom Filter – apply_filters multiple

I’m building a way to extend a WordPress plugin I’m developing using the following filter for grabbing the html content from a different plugin:

$content = apply_filters('satl_render_view', array($view, $slides));

Read More

With just one plugin this works perfectly, but once I activate a second plugin utilizing this same filter it stops working, $content is null for either plugin:

I’m adding the filters on the plugins in the __construct() method:

add_filter('satl_render_view', array('SatellitePortraitPlugin','addRender'));

and

add_filter('satl_render_view', array('SatelliteAwesomePlugin', 'addRender'));

Anyone run into this before?

In case it helps, this is the addRender method as it currently stands:

public static function addRender($params)
{
    list($view, $slides) = $params;
    $plugin = new SatelliteAwesomePlugin();
    return $plugin->render($view, array('slides' => $slides, 'frompost' => 'false'), false);
}

For the record, I’ve tried remove_filter() if there is no content to return, but that didn’t solve the problem.

Related posts

Leave a Reply

1 comment

  1. Params get passed to callback function, in this case addRender() and contain all the HTML of what is wanted to display to the second plugin using that same filter. To utilize this bit of information one must change the method:

    public static function addRender($params)
    {
        if (is_array($params)) {
            list($view, $slides) = $params;
            $plugin = new SatelliteAwesomePlugin();
            return $plugin->render($view, array('slides' => $slides, 'frompost' => 'false'), false);
        } else {
            return $params;
        }
    }
    

    You also need to make sure to update how the render() method passed the proper information back to the apply_filters method so the next plugin would have the proper array to run the addRender()

    } else {
        return array($file,$params['slides']);
    }
    

    Main Learning: WordPress apply_filters is much dumber than you’d think. You can’t assume it merges everything for you, it just passes the information along and your code needs to make sense of it.