WordPress Plugin: Getting Post Category ID During Output Buffering

I’m working on a WordPress plugin and running into an issue. I need to grab the category ID from a post page and use it to process some PHP code that’s stored in my database.

The tricky part is that I’m doing all this inside an output buffer that starts with a template_redirect hook. I want to modify the entire page before it gets sent to the browser.

Here’s my evaluation function:

function process_dynamic_content($content) {
    return preg_replace_callback("/(<\?php|<\?)(.*?)\?>/si", 'execute_code', $content);
}

And the execution function:

function execute_code($match) {
    ob_start();
    eval("$match[2];");
    $output = ob_get_contents();
    ob_end_clean();
    return $output;
}

The PHP code I’m trying to run looks like this:

<?php display_category_thumbnail([catID], 150, 150, 'thumb-class', '', '', ''); ?>

This works fine when I test it outside the output buffer, but inside the buffer it breaks everything and shows a blank page.

I tried getting the category ID before starting the output buffer and storing it in a global variable, but at that point the category data isn’t available yet:

if (strpos($_SERVER['REQUEST_URI'], 'wp-admin') === false) {
    global $stored_result;
    
    $content_template = get_option('my_plugin_template');
    $post_categories = get_the_category();
    $current_cat_id = $post_categories[0]->cat_ID;
    
    $content_template = str_replace("[catID]", $current_cat_id, $content_template);
    $stored_result = process_dynamic_content($content_template);
    
    add_action('template_redirect', 'start_page_buffering');
}

I need to either find the right WordPress hook that fires when category data is available, or figure out how to make the PHP evaluation work inside the output buffer. Any suggestions would be really helpful!

This happens because WordPress hasn’t set up the query context yet when template_redirect runs. I ran into the same thing building a content filter plugin. Switch to the wp_head action or use the the_posts filter instead - it fires after WordPress parses the query. For your case, try hooking into the wp action like others mentioned, but wrap your category stuff in is_single() or is_category() checks first. Also, ditch that eval() - it’s dangerous. Use do_shortcode() or custom shortcode handlers instead. I’ve seen eval() crash sites and create security holes in production.

try using the wp hook instead of template_redirect - that’s when query vars and post data are fully loaded. also check your error logs since eval() might be throwing fatal errors that kill the page silently.

Had the same nightmare with a custom plugin last year. Blank page = fatal error in your eval. Before switching hooks, throw error_reporting(E_ALL) and ini_set(‘display_errors’, 1) inside your execute_code function to see what’s breaking. Usually it’s WordPress functions not being available in eval context or variable scope issues. I built a custom function that creates an array of available data during the wp hook, then passes that context to the evaluation. Also try output buffering around just the eval part instead of the whole page - way easier to debug when stuff breaks.