The init
hook is too early — WordPress hasn’t yet identified the queried object (post, category, etc.); hence the is_page()
doesn’t work there:
add_action('init', 'register_script');
So for conditional tags/functions like is_page()
, is_single()
, etc. to work (i.e. return the proper result), you should use wp
or a later hook:
add_action('wp', 'whatever_function');
But for registering/enqueueing scripts and styles, you should always use the wp_enqueue_scripts
hook; so you would use this (and not the above):
add_action('wp_enqueue_scripts', 'register_script');
PS: Credits to @huraji for his helpful comment.
Related Posts:
- Load js/css files only on specific admin UI pages
- Get list of scripts / styles and show file which enqueued them
- Enqueue script only when shortcode is used, with WP Plugin Boilerplate
- Use js script from one plugin in another plugin
- Can’t get JS code to work with shortcode
- How to avoid loading same script twice?
- Loading scripts with wp_register_script and wp_enqueue_script
- Plugin Development – Class Constructor Not Firing wp_enqueue_style action hook
- Custom Plugin – CSS works, JS doesn’t
- Enqueuing scripts and styles in custom plugins
- How to enqueue scripts in right way in a plugin?
- How to make my shortcode load scripts and styles, from within the plugin?
- Enqueue a file that’s not js or css
- Enqueue ONLY Plugin Styles and Scripts
- plugin css is not being applied to the page
- How do I ensure I can loop through every enqueued script and CSS?
- How to avoid conflict if a plugin already have included open library?
- How to only Load scripts on variable products page
- Where is the right place to register/enqueue scripts & styles
- Check if a script/style was enqueued/registered
- How to add a javascript snippet to the footer that requires jQuery
- Is it ever okay to include inline CSS in plugins?
- How to enqueue JavaScripts in a plugin
- How to include jQuery and JavaScript files correctly?
- How to dequeue / deregister any theme styles and scripts
- jQuery in header or footer
- Hyphens vs. periods in the script slug in wp_register_script?
- How do I conditionally enqueue script for CPT single post type with plugin?
- Where to enqueue stylesheets for plugin?
- When is it too late to call the action wp_enqueue_scripts?
- Pass $this to function nested in another public function of the same class
- Script won’t load via plugin class
- Plugin Loading Scripts and Styles on Every Page – Even when not being used
- Dequeue / Deregister script and replace it with a new plugin
- Best spot for wp_register_script() and wp_register_style()
- Inserting JS snippet in the footer if there is no wp_footer?
- Register script/style: Is it possible to customize the version query string via plugin?
- How do I add a javascript file to all admin pages via a plugin?
- Multisite plugin development and wp_enqueue_script
- Dequeue theme stylesheets but keep widget styling on custom page template
- Using multiple versions of jQuery while still calling it like WP likes
- How to run a external JavaScript file on wp-admin if admin, and other if normal user?
- Deregistering a script in WordPress seems impossible
- Adding Dropbox dropin js using wp_enqueue_script
- Where to call wp_enqueue_script in a plugin with custom template?
- Replacing Scripts in Admin Load_Scripts
- Is there a way to ensure plugin script loads before another script?
- React JSX in WordPress Plugin Development
- Gutenberg blocks not getting styled on back end
- What is the proper way to include Bootstrap when executing a shortcode
- conditionally load javascript & CSS for do_action() calls from plugin
- What stylesheets are available in core?
- Making a plugin only available on the front-end for the logged in super admin
- How to enqueue JavaScript file that depends on CSS stylesheet
- How to enqueue style in WordPress plugin from theme files?
- How to force that styles are enqueued in the header?
- How to enqueue or include json file to a plugin
- How to load library scripts in admin from plugins in noConflict wrapper?
- Where is the right place to register/enqueue scripts & styles
- jQuery does not enqueue for my purpose… (before an inline script in the footer)
- CSS not affecting widget output
- Enqueue script globally
- How should I go about registering JavaScript that isn’t a file? [duplicate]
- WP Plugin Running before jQuery
- wp_localize_script is not adding a global variable for javascript
- Loading Modernizr (or other JavaScript libraries) for use in a plugin
- should i be checking for jquery before enqueing it in a plugin
- WordPress plugin enqueue scripts working for js, but not css files
- How to import the css in the plugin admin area?
- My stylesheets are not enqueuing
- Why enqueue styles on hook?
- unable to wp_enqueue_script(‘suggest’);
- How to check via conditional tags for a single plugin page?
- How do I get rid of my inclusion race-condition on wp_enqueue_script
- Using function from enqueued .js file in theme in plugin?
- Enqueue scripts inside a class in a plugin
- CSS not being applied using wp_enqueue_style
- wp_enqueue_scripts in a plugin’s class
- WordPress Plugin Authoring question about jquery and css
- Loading Images from Javascript on the Front End
- Creating a WP plugin and having issues linking CSS stylesheet within functions
- wordpress plugin add page when activate
- Add default value from selection
- Is “document loaded” different on admin side than public side?
- Adding External Gmap JS to WordPress
- Load script and styles using an array of resources
- Append php file to footer
- AJAX call of function containing javascript which is not loaded (Plugin development)
- Javascript not being loaded only in custom taxonomy template
- Ajax Response Error | just getting error as the response
- Add a Script button in W3 Total Cache plugin not working due to conflicting jQuery version
- Enqueue scripts based on site.
- How to enqueue an external Javascript file to Frontpage footer
- Chosen Select jquery Not Working in Plugin
- Enqueue script not working
- WordPress-child problem with many css files in parent theme
- wp_enqueue doesn’t load dependencies
- Why does this fail: Disabling plugins enqueue_script() in functions.php
- Enqueue Script in custom plugin before other
- Why do WordPress adds the id=”handle-{js|css}” attribute to scripts and stylesheet?