Even though the second script relies on jQuery, you do not need to specify this, as our first script will already load jQuery. Wp_enqueue_script( 'custom-search-spinner', plugins_url( '/js/search-spinner.js', _FILE_ ), array( 'custom-search' ) ) Īssume that the first script is a search function and the second is an extension to the search function, making a spinner spin while searching the database. Wp_enqueue_script( 'custom-search', plugins_url( '/js/search.js', _FILE_ ), array( 'jquery' ) ) Example: add_action( 'wp_enqueue_scripts', 'custom_search_assets' ) If you have dependencies, you have to register them otherwise, your scripts won’t load. Wp_enqueue_script( 'custom-searchpage', plugins_url( '/js/search.js', _FILE_ ), array('jquery')) Adding jQuery as a parameter to the enqueue can solve that. An example of this could be assets depending on jQuery. The third parameter is an array of registered scripts/styles that must finish loading before enqueuing the asset. WordPress’ enqueueing mechanism has built-in support for managing decencies, by using a third argument of both wp_register_style() and wp_register_script() functions. The following code goes where you want to output the assets. Wp_register_script('custom-searchpage', plugins_url( '/js/search.js', _FILE_ )) Īdd_shortcode('custom_search_assets', 'custom_search_assets') Wp_register_style('custom-searchpage', plugins_url( '/css/search.css', _FILE_ )) add_action('wp_enqueue_scripts', 'custom_search_assets') You can separate the two functions by using them in different “hooks.” For example, you can use the wp_enqueue_scripts hook to register the assets and then a shortcode function to enqueue the assets. Wp_enqueue_script( 'custom-searchpage', plugins_url( '/js/search.js', _FILE_ ) ) Wp_enqueue_style( 'custom-searchpage', plugins_url( '/css/search.css', _FILE_ ) ) You can use the enqueue functions to register and enqueue right away by using the same arguments as in the register functions: add_action( 'wp_enqueue_scripts', 'custom_search_assets' ) In the above example, We registered and enqueued assets within the same function, which is redundant. Wp_register_script('custom-searchpage', plugins_url('/js/search.js', _FILE_ )) Wp_register_style('custom-searchpage', plugins_url('/css/search.css', _FILE_ )) Within the hooked function you can use wp_register_script(), wp_enqueue_script(), wp_register_style() and wp_enqueue_style() functions. To enqueue assets (scripts and styles) in the WordPress front-end you can use the wp_enqueue_scripts hook. Enqueuing assets is a great way to structure assets and prevent unnecessary overhead in the network or the runtime/DOM i.e. Then you would register the file and its location, but only enqueue that file on the specific search page. An example of this could be a search page, where you would like to load some custom Javascript functions for searching – but you don’t need that extra JavaScript code on any other pages. For instance, sometimes, you want to let WordPress know about an asset, but you may not want to load that asset on every page. Enqueuing in WordPress instructs WordPress to register that a file exists and its physical location and then output it into the WordPress template’s header, body, or footer.Ī reason for having multiple steps has to do with modularity.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |