Using WordPress ‘preview_theme_ob_filter()’ PHP function

The preview_theme_ob_filter() WordPress PHP function is a callback function for ob_start() that captures all links in a theme.


$content = 'Your HTML content with links';
$filtered_content = preview_theme_ob_filter($content);


  • $content (string) – The content to be filtered, containing the links in the theme.

More information

See WordPress Developer Resources: preview_theme_ob_filter()

This function is used internally by WordPress and is not intended for use in themes or plugins.


In this example, we use the preview_theme_ob_filter() function to capture and filter links in the theme.

$content = '<a href="">Example link</a>';
$filtered_content = preview_theme_ob_filter($content);
echo $filtered_content; // Output: <a href="">Example link</a>

In this example, we filter multiple links in the theme using the preview_theme_ob_filter() function.

$content = '<a href="">Example 1</a> <a href="">Example 2</a>';
$filtered_content = preview_theme_ob_filter($content);
echo $filtered_content; // Output: <a href="">Example 1</a> <a href="">Example 2</a>

In this example, we filter the links in a navigation menu using the preview_theme_ob_filter() function.

$menu = '<nav><a href="">Home</a> <a href="">About</a> <a href="">Contact</a></nav>';
$filtered_menu = preview_theme_ob_filter($menu);
echo $filtered_menu; // Output: <nav><a href="">Home</a> <a href="">About</a> <a href="">Contact</a></nav>

In this example, we filter the links in a list of blog posts using the preview_theme_ob_filter() function.

$posts = '<ul><li><a href="">Post 1</a></li> <li><a href="">Post 2</a></li></ul>';
$filtered_posts = preview_theme_ob_filter($posts);
echo $filtered_posts; // Output: <ul><li><a href="">Post 1</a></li> <li><a href="">Post 2</a></li></ul>