Support » Plugins » Multiple ob_start ‘s in WordPress

  • Hi,

    I’m trying to write some custom plugin code, the ultimate end result will be that I can “program” in php, and my “author” can then call the code from within posts with {key_words}. I get that in the following example plugins are avilable to list_pages etc, but this will devlop into “odd” requests from my author.

    Please consider the following code which executes fine outside of wordpress….

    function wp_recent_posts() {
    
                    ?>recent post<?php
    }
    
    function wp_page_list() {
    
                    ?>page list<?php
    }
    
    function wp_recent_posts_inline( $content ) {
    
            ob_start(); // Enable output buffering to supress echos
    
            wp_recent_posts(); // run function
    
            $result = ob_get_contents();
    
            ob_end_clean(); // Empty the Buffer
    
            echo str_replace("{wp_recent_posts}", $result, $content);
    
    }
    
    function wp_page_list_inline( $content ) {
    
            ob_start(); // Enable output buffering to supress echos
    
            wp_page_list(); // run function
    
            $result = ob_get_contents();
    
            ob_end_clean(); // Empty the Buffer
    
            echo str_replace("{page_list}", $result, $content);
    
    }
    
    $content = " abc {page_list} cde " ;
    wp_page_list_inline($content);
    $content = " 123 {wp_recent_posts} 456";
    wp_recent_posts_inline($content);

    Now replace

    $content = " abc {page_list} cde " ;
    wp_page_list_inline($content);
    $content = " 123 {wp_recent_posts} 456";
    wp_recent_posts_inline($content);

    with

    add_filter('the_content', 'wp_page_list_inline');
    add_filter('the_content', 'wp_recent_posts_inline');

    upload the code & activate the plugin, write a page / post with the words {page_list} and {wp_recent_posts} in .

    In my experience {page_list} will work but {wp_recent_posts} will not, if you change

    add_filter('the_content', 'wp_page_list_inline');
    add_filter('the_content', 'wp_recent_posts_inline');

    for

    add_filter('the_content', 'wp_recent_posts_inline');
    add_filter('the_content', 'wp_page_list_inline');

    i.e. swap the add_filter statements over, the opposite will happen… can anyone offer any advise ? ( as I’d like both to work) 😉

    Cheers,
    Nick

  • The topic ‘Multiple ob_start ‘s in WordPress’ is closed to new replies.