WordPress.org

Support

Support » Plugins and Hacks » How to apply a filter to the entire page?

How to apply a filter to the entire page?

  • I’m having the following filters in a plugin:
    add_filter(‘the_content’, ‘myfunction’, 10);
    add_filter(‘the_excerpt’, ‘myfunction’, 10);

    Now I want to add an option in the plugin to apply a filter to the entire page, so that the content of the sidebar, header, footer, etc. can be modified by the plugin as well.
    Any idea how to implement?

    Thanks in advance,
    Michael

Viewing 11 replies - 1 through 11 (of 11 total)
  • You can do some output bufferring. But before going to further details, what is it precisely that you want to filter out?

    alphaoide, please see Link Indication Plugin.
    Here I want to add an option to apply the plugin to the entire page. So long, I only apply filters as aforementioned to the_content, the_excerpt and optionally to comment_text.

    Thanks.

    So, here is the basic concept how you could do it.

    if (/* we're in one of intended pages */)
    {
    ob_start('alphaoide_filter_content');
    }

    Then somewhere you’d have the filter function definition.

    function alphaoide_filter_content($content)
    {
    ...
    $content = /* filtered_content */
    ...
    return $content;
    }

    Make sure you measure how this will affect the page load time.

    Great, many thanks!

    Michael

    I have implemented ob_start() for applying the plugin to the entire site as suggested by alphaoide.
    But if gzip compression is used (WP Admin / Options / Reading / ‘WordPress should compress articles (gzip) if browsers ask for them’), the plugin is not being applied.

    Any other ideas?

    Thanks,
    Michael

    Im offering ob_start() as an option in my plugin Link indication.
    In the meantime there are 2 known issues when using this option:
    – gzip issue (see above)
    – some users reported that TinyMCE didn’t work anymore. I could reproduce this only when using IE but not with Firefox, but they did have this problem with FF as well.

    Hope that we will have such plugin filter in future WP release…

    What does the content filter actually get passed when you have gzip compression on? It should work, near as I can see, because ob_starts can be nested fith no troubles.

    This is the code I am using for applying the plugin:

    } else {
    // Apply to entire blog but not to WP admin
    if ( strpos($_SERVER['REQUEST_URI'], 'wp-admin') === false ) {
    ob_start('wp_link_indication');
    }
    }

    The plugin is not applied at all when gzip is on.

    The following function does some replacements in anchor tags.
    function wp_link_indication($content) {

    // perform the replacement of each URL in content
    $pattern = '/(.*?)<\/a>/i';
    $result = preg_replace_callback($pattern,'mwli_parse_links',$content);
    return $result;
    }

    “If $wmli_opt[‘opt_entireblog’] == ‘1’, the plugin is not applied at all when gzip is on.”

    Yes, I get that, but you’re going to have to do a little debugging here in order to figure out where the problem is and figure out how to work around it.

    The important thing is this: If you have opt_entireblog on and also turn on gzip, then does wp_link_indication() get called at all, ever, and if so, what is in $content when it is called? You may need to change wp_link_indication to have it spit $content out to a file or something in order to figure out what is going on.

    What I think is happening is that the gzip_compression itself uses an ob_start(). However, plugins execute first, and so your output buffer is on the outside of the gzip output buffer. Therefore, when the page ends, the gzip output buffer is executed first, and your buffer gets back the already compressed data. So you match nothing, and voila.

    Additional: Yes, I’ve traced the startup sequence, and this is definitely the case. wp-config.php calls wp-settings.php, which loads the plugins. gzip_compression() isn’t called until after that, in wp-blog-header.php. But since you want it to apply after the compression is turned on, you just need to hook the earliest thing after that that you can. Which is probably the template_redirect hook.

    So, change your ob_start() line to this:
    add_action('template_redirect','do_my_ob_start');

    and create a new function like this:
    function do_my_ob_start() {
    ob_start('wp_link_indication');
    }

    That should fix it for gzip compression.

    Perfect! Many thanks, Otto, I just have tested it and it works like a charm.
    So gzip issue is solved now 😉

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘How to apply a filter to the entire page?’ is closed to new replies.
Skip to toolbar