Call to undefined function is_blog()

Inside a post title filter I check whether the current page is the blog page.
However, PHP complains that the function is_blog doesn’t exist:

NOTICE: PHP message: PHP Fatal error:  Uncaught Error: Call to undefined function is_blog() in /srv/www/web/app/themes/some-theme/app/setup.php:339
php_1    | Stack trace:
php_1    | #0 /srv/www/web/wp/wp-includes/class-wp-hook.php(288): App\post_button_title('Some title', 6)
php_1    | #1 /srv/www/web/wp/wp-includes/plugin.php(206): WP_Hook->apply_filters('Some title', Array)
php_1    | #2 /srv/www/web/wp/wp-includes/post-template.php(171): apply_filters('the_title', 'Some title', 6)
php_1    | #3 /srv/www/web/wp/wp-includes/post-template.php(89): get_the_title(Object(WP_Post))
php_1    | #4 /srv/www/web/app/plugins/dublin-core-title/dublin-core-title.php(26): the_title_attribute(Array)
php_1    | #5 /srv/www/web/wp/wp-includes/class-wp-hook.php(288): add_dctitle('')
php_1    | #6 /srv/www/web/wp/wp-includes/class-wp-hook.php(312): WP_Hook->apply_filters(NULL, Array)
php_1    | #7 /srv/www/web/wp/wp-includes/plugin.php(478): WP_Hook->do_action(Array)
php_1    | #8 /srv/www/web/app/themes/some-theme/app/filters.php(52): do_action('wp_head')
php_1    | #9 /srv/www/web/app/themes/some-theme/vendor/illuminate/support/Collection.php(397): App\{closu in /srv/www/web/app/themes/some-theme/app/setup.php on line 339

In setup.php:

function post_button_title( $title, $post_id ) {
    if(!\is_blog()) return $title; // skip
    if(!$post_id) return $title; // skip

    $button_title = get_field('button', $post_id);
    if(!isset($button_title) or empty($button_title)) return $title; // skip

    return $button_title;
}
add_filter( 'the_title', __NAMESPACE__ . '\\post_button_title', 10, 2 );

I already removed the wp/ folder for the WordPress installation and reinstalled using composer install.

WP core doesn’t come with a function named is_blog() so unless you have created one yourself, the error message is correct.

https://gist.github.com/wesbos/1189639 has a discussion on how to implement it.

1 Like

Somehow the search results made the impression for me that is_blog is an existing core function in WordPress.

This topic was automatically closed after 42 days. New replies are no longer allowed.