conditional tags - Using is_page() in functions.php not working at all

Trying to run a custom script on a single page using is_page in functions.php not working at all. I have a function call

Trying to run a custom script on a single page using is_page in functions.php not working at all. I have a function called load_gh_boards that would enquque a script, only on a certain page (79):

function load_gh_boards() {
if( is_page(79) ){
    wp_register_script( 'gh-jobs-board', get_bloginfo( 'template_directory' ) . '/js/gh-jobs-board.js', array(), '1.0', true );
}}
add_action('wp_enqueue_script', 'load_gh_boards');

This is inside a theme enqueue function that is loading all of my styles and scripts:

function theme_enqueue() {
if ( ! is_admin() ) {

    wp_register_style( 'main-style', get_bloginfo( 'template_directory' ) . '/css/style.css?' . time() );
    wp_enqueue_style( 'main-style' );
    wp_enqueue_script( 'jquery' );
    wp_register_script( 'main-vendor', get_bloginfo( 'template_directory' ) . '/js/vendor.js', array(), '1.0', true );
    ///etc...

    wp_enqueue_script( 'main-vendor' );
    wp_enqueue_script( 'main-script' );

    ///load_gh_boards
}

No result. Any help or insight at all would be appreciated.

Trying to run a custom script on a single page using is_page in functions.php not working at all. I have a function called load_gh_boards that would enquque a script, only on a certain page (79):

function load_gh_boards() {
if( is_page(79) ){
    wp_register_script( 'gh-jobs-board', get_bloginfo( 'template_directory' ) . '/js/gh-jobs-board.js', array(), '1.0', true );
}}
add_action('wp_enqueue_script', 'load_gh_boards');

This is inside a theme enqueue function that is loading all of my styles and scripts:

function theme_enqueue() {
if ( ! is_admin() ) {

    wp_register_style( 'main-style', get_bloginfo( 'template_directory' ) . '/css/style.css?' . time() );
    wp_enqueue_style( 'main-style' );
    wp_enqueue_script( 'jquery' );
    wp_register_script( 'main-vendor', get_bloginfo( 'template_directory' ) . '/js/vendor.js', array(), '1.0', true );
    ///etc...

    wp_enqueue_script( 'main-vendor' );
    wp_enqueue_script( 'main-script' );

    ///load_gh_boards
}

No result. Any help or insight at all would be appreciated.

Share Improve this question edited Aug 29, 2017 at 22:24 Johansson 15.4k11 gold badges43 silver badges79 bronze badges asked Aug 29, 2017 at 19:19 pjldesignpjldesign 1352 silver badges6 bronze badges 1
  • 1 Please don't replace the wrong code in your question with the correct code from the answer. If you do this, future visitors won't have a chance to understand the problem. – Johansson Commented Aug 29, 2017 at 22:24
Add a comment  | 

5 Answers 5

Reset to default 2

is_page(79) is asking if the current main query is a page, specifically page 79

But the main query hasn't happened yet

This function has to be called inside the main loop or at the very least inside the template, but the init hook is way too early for this.

Instead, make sure you use the body class, and load the script on all pages. Then, check if the body tag has the css class page-id-79.

Other Notes

  • You don't enqueue on the init hook, you're meant to enqueue on the wp_enqueue_scripts hook
  • It would be simpler to return early e.g. if ( is_admin() ) return;
  • Don't enqueue jQuery, instead add it as a dependency so WP knows how to output the scripts in the correct order
  • main-style and main-vendor are very generic names, you might encounter clashes, prefix them, e.g. pji-main-vendor
  • theme_enqueue is also a very generic function name, you should prefix that too
  • You can register and enqueue at the same time using wp_enqueue_script. If you pass everything to that function there's no need to call wp_register_script
  • Indent your code, modern code editors will do this automatically for free, zero effort. Sublime or Atom are examples of free software that will do this for you, there really are no excuses

This is absolutely working for me right now:

add_action('get_header', function() {
    if(is_page('566')) {

        function sp_enqueue_script() {
            wp_enqueue_script(
                'sp-custom-script',
                get_stylesheet_directory_uri() . '/assets/js/sp_script.js',
                array( 'jquery' ), '1.0', true
            );
        }

        add_action( 'wp_enqueue_scripts', 'sp_enqueue_script' );
    }
});

is_page doesn't work in functions.php because the main query hasn't loaded yet, so wait for the header to load.

Last boolean in the wp_enqueue_script hook is for the script to be enqueued in the footer, so it runs faster.

function theme_enqueue() {
if ( ! is_admin() ) {
wp_register_script( 'main-script', get_bloginfo( 'template_directory' ) . '/js/script.js', array(), '1.0', true );
wp_register_script( 'gh-jobs-board', get_bloginfo( 'template_directory' ) . '/js/gh-jobs-board.js', array(), '1.0', true );
///etc...
wp_enqueue_script( 'main-vendor' );
wp_enqueue_script( 'main-script' );
///etc...

if( is_page(79) ) {
    wp_enqueue_script( 'gh-jobs-board' );
}
}
}
add_action( 'wp_enqueue_scripts', 'theme_enqueue' );

First of all, there is a mistype in your code. It's wp_enqueue_scripts, not wp_enqueue_script. That might be the issue.

Also, you shouldn't use wp_enqueue_scripts inside another wp_enqueue_scripts. Simply add your conditional in the theme_enqueue() function like this:

function theme_enqueue() {
if ( ! is_admin() ) {

    wp_register_style( 'main-style', get_bloginfo( 'template_directory' ) . '/css/style.css?' . time() );
    wp_enqueue_style( 'main-style' );
    wp_enqueue_script( 'jquery' );
    wp_register_script( 'main-vendor', get_bloginfo( 'template_directory' ) . '/js/vendor.js', array(), '1.0', true );
    ///etc...

    wp_enqueue_script( 'main-vendor' );
    wp_enqueue_script( 'main-script' );

    if( is_page(79) ){
        wp_register_script( 'gh-jobs-board', get_bloginfo( 'template_directory' ) . '/js/gh-jobs-board.js', array(), '1.0', true );
    }
}
add_action('wp_enqueue_scripts', 'theme_enqueue');

I assume that the theme_enqueue() function is hooked to wp_enqueue_scripts itself.

You Register the Script, but you don't enqueue it. Add wp_enqueue_script('gh-jobs-board'); to your conditional function.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745261260a4619222.html

相关推荐

  • conditional tags - Using is_page() in functions.php not working at all

    Trying to run a custom script on a single page using is_page in functions.php not working at all. I have a function call

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信