Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sumonst21/f319319c8c61407e42f3b74c99ff6d0f to your computer and use it in GitHub Desktop.
Save sumonst21/f319319c8c61407e42f3b74c99ff6d0f to your computer and use it in GitHub Desktop.
How to solve the PHP Notice: is_page was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. By using the Template Redirect hook the action can be called before any templates are loaded.
// Currently there is an issue in our PHP server error logs.
// PHP Notice: is_page was called incorrectly.
// Conditional query tags do not work before the query is run. Before then, they always return false.
// This is the wrong way to call is_page.
// is_page() only work within template files.
// To use it within plugin files, you need to use it with the combination of template_redirect action hook.
// function wordpress_theme_enqueue_styles() {
// if (is_page(2072)) {
// wp_enqueue_style('accountdetails-style', get_stylesheet_directory_uri() . '/accountdetails.css?v=5', array('parent-style'));
// }
// }
// This is the correct way of re-writing the function.
// This action hook executes just before WordPress determines which template page to load.
// Source: https://stackoverflow.com/questions/22070223/how-can-i-use-is-page-inside-a-plugin
add_action( 'template_redirect', 'footer_css' );
function footer_css() {
if (is_page() || is_product() || is_singular('post') || is_woocommerce() || is_cart() || is_checkout()) {
wp_enqueue_style('Footer_Links-style', get_stylesheet_directory_uri() . '/CSS_Components/Footer_Links.css?v=2.5', array( 'parent-style'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment