Hot answers tagged

8

You can use wp_localize_script() to pass php variables to javascript. You create an array in php and then pass it to the function as the third parameter. It will come through as an object you name with the second parameter. First, register the script. wp_register_script( 'custom-name-js', plugins_url( 'assets/custom-js.php', __FILE__ ) ); Second, build ...


4

The first wp_enqueue_script() in your code should be wp_register_script(). The enqueue function does the job while the register function is getting ready to do the job. It is always better to rather create a new js file to keep things organised. As to calling them, you can do the following wp_register_script( 'abc', 'path_to' ); wp_register_script( 'xyz', ...


3

is_singular doesn't handle archives. Try is_post_type_archive( 'limitedrun' );


3

Here's another option which avoids having to de-register and re-register: // Move jQuery to the footer wp_scripts()->add_data( 'jquery', 'group', 1 ); wp_scripts()->add_data( 'jquery-core', 'group', 1 ); wp_scripts()->add_data( 'jquery-migrate', 'group', 1 );


2

You can try this function: wp_localize_script( $handle, $name, $data ); See https://codex.wordpress.org/Function_Reference/wp_localize_script for documentation. As the codex says: Though localization is the primary use, it can be used to make any data available to your script that you can normally only get from the server side of WordPress. I had to ...


2

I agree with @Mark Kaplun - what you're describing is an odd behavior though. Try this markup to override the $ver param. wp_enqueue_script( 'script', get_template_directory_uri() . '/js/script.js', array ( 'jquery' ), null, true); https://developer.wordpress.org/themes/basics/including-css-javascript/ Alternatively, to work around the version cache try ...


2

Just alter the script URL depending on the condition: function my_custom_js() { $file = some_condition() ? 'custom' : 'custom-2'; wp_enqueue_script( 'custom_js', get_template_directory_uri() . "/js/$file.js" /* array( 'jquery' ) */ /* dependencies, if any */ ); } add_action( 'wp_enqueue_scripts', 'my_custom_js' ); You ...


2

Since WordPress 4.5 you can use wp_add_inline_script(): add_action( 'wp_enqueue_scripts', 'cyb_enqueue_scripts' ); function cyb_enqueue_scripts() { wp_enqueue_script( 'myscript', 'url/to/myscript.js', array(), '1.0' ); wp_add_inline_script( 'myscript', 'Your inline javascript code gos here' ); }


2

You can prevent adding a script to a specific WordPress page by checking if whether we are on that page or not is_page($page_id): function add_js() { wp_deregister_script('jquery'); wp_register_script('jquery', "http" . ($_SERVER['SERVER_PORT'] == 443 ? "s" : "") . "://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js", false, null); ...


2

Use wp_enqueue_scripts instead on init. This will ensure you the template is already loaded. add_action('wp_enqueue_scripts', 'maps_scripts'); Or you try to check the template using get_page_template_slug() instead of is_page_template: $tmp = get_page_template_slug($post_id); // provide page/post ID if('page-about.php' == $tmp) { // enqueue scripts ...


1

You can save your time and server load by not using wp_register_script and wp_register_style when you don't need them definitely. wp_enqueue_style and wp_enqueue_script do the same job themselves when not involving excessive functions. Here is easier and more readable code up to date with the accepted answer by @vancoder: <?php function opby_theme() { ...


1

You are registering the script instead on enqueueing. Also, slimScroll depends on jQuery. So you have to point it out explicitly. function fullpagejs() { $slimscroll = get_stylesheet_directory_uri() . '/library/fullPage.js-master/vendors/jquery.slimscroll.min.js' ; wp_enqueue_script( // see here 'fullpage-slimscroll', $slimscroll, ...


1

I had 2 things wrong: I unregistered wordpress's jquery without properly registering my own (I concatenated my scripts ) I didnt have wp_footer() in my theme. the wp_enqueue_media() function loads the scripts to the footer.



Only top voted, non community-wiki answers of a minimum length are eligible