WordPress Category ID vs Eval Issue

Ok this is a little complex. I am creating a plugin, and want to find the category ID from the Post page.

That’s the easy part.

Read More

What makes it complex is I am doing it within an ob_start (started in a ‘template_redirect’ action) as I want to edit the full page before it is returned to the browser. Again that is easy enough from the ob_start function.

With the ID returned I want to evaluate some php stored in a sql field. I am trying to do this from within the ob_start function

$tui_cifp_insertvalue = tui_cifp_evaluate_html($tui_cifp_insertvalue);

This calls this

function tui_cifp_evaluate_html($string) {
return preg_replace_callback("/(<?php|<?|< ?php)(.*?)?>/si",'EvalBuffer', $string);
}

Which in turn calls

function EvalBuffer($string) {
ob_start();
eval("$string[2];");
$ret = ob_get_contents();
ob_end_clean();
return $ret;
}

And the php I am trying to evaluate is.

<?php tui_findPostThumbIMG([categoryID],100,100,'categoryintro-thumbnail','','',''); ?>

This all works outside the ob_start routine, but here even simple php doesn’t work. From within the ob_start routine the plugin breaks and a blank page returns.

So I thought I could evaluate the php before the start of the ob_start and pass the result through a global variable. That works, but at the point this starts using the following, the category ID is not available.

if ( strpos($_SERVER['REQUEST_URI'], 'wp-admin') === false ) {

global $holdvalue;

$tui_cifp_insertvalue = get_option('tui_cifp_insertvalue');

$categories = get_the_category();
$categoryID = $categories[0]->cat_ID;

$tui_cifp_insertvalue = str_replace("[categoryID]", $categoryID, $tui_cifp_insertvalue);

$holdvalue = tui_cifp_evaluate_html($tui_cifp_insertvalue);

add_action('template_redirect','tui_cifp_ob_start'); //

}

The ob_start function

function tui_cifp_ob_start()
{

ob_start('tui_cifp_templatefilter');

}

Ok I am stumped … any ideas?

I either need to find a hook that executes at the right time so that I have access to the category ID, or I need to work out how to evaluate the php during the ob_start.

Oh … I guess I should say. What I want to do is replace a tag on a wordpress page with some other information saved in a string, but need to be able to do this once the full page if drawn.

Thanks
Stephen

PS I have asked this on the wordpress forums without a response. Sorry for the cross posting but I am a little desperate.

Related posts

Leave a Reply

2 comments

  1. Im not partial to eval, but this seems to work, with or without the output buffering at the end …

    function tui_findPostThumbIMG()
    {
     echo "hey heyn";
    }
    
    ob_start();
    $categoryID = 10;
    $tui_cifp_insertvalue = "<?php tui_findPostThumbIMG([categoryID],100,100,'categoryintro-thumbnail','','',''); ?>";
    $tui_cifp_insertvalue = str_replace("[categoryID]", $categoryID, $tui_cifp_insertvalue);
    $tui_cifp_insertvalue = tui_cifp_evaluate_html($tui_cifp_insertvalue);
    echo $tui_cifp_insertvalue;
    ob_end_flush();
    
  2. Thanks OIS, I appreciate your proposing that solution. It is however doing the same thing as I have been doing. I guess structured differently.

    It did however cause me to look at the problem from a different prospective.

    What I realised was that get_the_category() needs a parameter and wasn’t getting a category because it was the Post ID that wasn’t available. I solved the problem by doing the setup up front like so.

    function tui_cifp_ob_start()
    {
    
        global $tui_cifp_message, $tui_cifp_div, $wp_query;
    
        if (is_single()) 
        {
    
            $tui_cifp_div = get_option('tui_cifp_div');
    
            if ($tui_cifp_div !== '') 
            {
    
            $thePostID = $wp_query->post->ID;
            $categories = get_the_category($thePostID); 
            $categoryID = $categories[0]->cat_ID;
    
            $tui_cifp_message = get_option('tui_cifp_message');
    
            $categoryTitle = $categories[0]->cat_name;
            $categoryDescription = $categories[0]->category_description;
    
            $tui_cifp_message = str_replace("[categoryID]", $categoryID, $tui_cifp_message);
            $tui_cifp_message = str_replace("[categoryTitle]", $categoryTitle, $tui_cifp_message);
            $tui_cifp_message = str_replace("[categoryDescription]", $categoryDescription, $tui_cifp_message);
            $tui_cifp_message = $tui_cifp_div.$tui_cifp_message;
    
            $tui_cifp_message = tui_cifp_evaluate_html($tui_cifp_message);
    
            }
    
            ob_start('tui_cifp_templatefilter');
    
        }
    
    }
    

    Thanks again.