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.

Usage

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

Parameters

  • $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.

Examples

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

$content = '<a href="https://example.com">Example link</a>';
$filtered_content = preview_theme_ob_filter($content);
echo $filtered_content; // Output: <a href="https://example.com?preview=true">Example link</a>

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

$content = '<a href="https://example1.com">Example 1</a> <a href="https://example2.com">Example 2</a>';
$filtered_content = preview_theme_ob_filter($content);
echo $filtered_content; // Output: <a href="https://example1.com?preview=true">Example 1</a> <a href="https://example2.com?preview=true">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="https://home.com">Home</a> <a href="https://about.com">About</a> <a href="https://contact.com">Contact</a></nav>';
$filtered_menu = preview_theme_ob_filter($menu);
echo $filtered_menu; // Output: <nav><a href="https://home.com?preview=true">Home</a> <a href="https://about.com?preview=true">About</a> <a href="https://contact.com?preview=true">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="https://post1.com">Post 1</a></li> <li><a href="https://post2.com">Post 2</a></li></ul>';
$filtered_posts = preview_theme_ob_filter($posts);
echo $filtered_posts; // Output: <ul><li><a href="https://post1.com?preview=true">Post 1</a></li> <li><a href="https://post2.com?preview=true">Post 2</a></li></ul>