Skip to content

Instantly share code, notes, and snippets.

@keiraarts
Last active May 12, 2022 10:23
Show Gist options
  • Save keiraarts/b9c0e635ac1364fd8464e0f4e332408a to your computer and use it in GitHub Desktop.
Save keiraarts/b9c0e635ac1364fd8464e0f4e332408a 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'));
}
}
@phg124214
Copy link

phg124214 commented May 12, 2022

To solve this problem, I am suppose to copy/paste this PHP file into my FTP? If so, into which folder? Do I then need to use the "embed" script? If so, where should I add that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment