I’d like to replace all {POST_NAME}
‘s from my text-widgets on single.php
sidebar. I’m thinking of something like this
global $post;
$POST_NAME= $post->post_name;
$sidebar = preg_replace('/\{(POST_NAME)\}/e', "$$1", $sidebar);
How can I get sidebar HTML into \$sidebar
2 Answers
Your code looks well and considerations are correct. In order to achieve your goal, I’d collect HTML and wrap it up like this:
Instead of calling <?php dynamic_sidebar($sidebar_id); ?>
use <?php dynamic_sidebar_replaced($sidebar_id); ?>
with a function (add it to functions.php) below:
function dynamic_sidebar_replaced($sidebar_id) {
if (is_single()) { // Optionally, to ensure you're at the single post template.
ob_start(); // Start gathering output
dynamic_sidebar($sidebar_id);
global $post;
$POST_NAME = $post->post_name;
$sidebar = preg_replace('/\{(POST_NAME)\}/e', "$$1", $sidebar);
ob_end_clean();
echo $sidebar;
return '';
}
dynamic_sidebar($sidebar_id);
}