New answers tagged

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() { ...


3

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


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); ...


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, ...


0

You must not only register the script, but also enqueue it after that, like this: wp_enqueue_script('fullpage-slimscroll');


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

One simple way of doing this would be by creating new page template for those pages. Follow the procedure copy your page.php file and give it a name i.e. no_script.php Then in the top line replace function name as get_header(); with you actual header.php code (Mean simply paste you whole header.php code by replacing get_header();). Remove header script ...


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 );


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 ...


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 ...


0

You should always specify a version number for you scripts/css when you enqueue them, otherwise when you change them on the server they might not be changed for the user because some caching proxy in the middle cached it and keep serving it. If you don't specify one, wordpress will attach its own version number to the url as the ver parameter, but frankly ...


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 ...


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', ...


0

As mentioned in the comments, your code snippet is working fine. Nevertheless the font kit has to be published within the Kit Editor: Login to your Typekit account Locate the Kit Editor (see: https://typekit.com/kit_editor/kits/xxxxxxx) Click the Publish button (bottom right) You may also consider using the new embed code provided by Typekit: ...


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.


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 ...



Top 50 recent answers are included