

Wordpress enqueue script on specific page how to#
Let’s say the filename for our template for masonry pages is page-masonry-gallery.php. How to enqueue different scripts & styles for a Custom Post Type in functions.php Hot Network Questions How do I echo a file path after interpreting characters like dots '. In our use case above, we might want to load our masonry library for pages using a special template for masonry pages. You can find many of them in WordPress’s Theme Developer Handbook. For example, if a script was enqueued using the loginenqueuescripts hook that script would not. Each of these areas uses a specific hook to enqueue scripts to use in that area. There are a whole host of useful conditional tags in WordPress. There are three different areas that a developer can enqueue scripts for the first is the client side, the second is the admin side and the third is the login pages. Within the action hook, you can use several functions and embed them in the functions. Let’s go through the basics of the wpenqueuescript hook that you’ll use to load your assets. When we enqueue our scripts, we can use a PHP if statement to load scripts only when needed. We now know what enqueueing is and how it works. Here is an example of loading a masonry library through enqueueing: wp_enqueue_script('masonry', get_stylesheet_directory_uri(). First, though, if you are not familiar with loading JavaScript using WordPress’s wp_enqueue_script function, you can learn more about that from the WordPress developer documentation. Otherwise wpenqueuescript function will not be executed. ishome() is for when the front blog page is being display. It works in tandem with a very similarly named bit of code, wpenqueuescripts, which is the WordPress action hook to which our individual calls to wpenqueuescript () will stick. isarchive() tells your if the current page is displaying a set of archives, whether for an author, a tag, a date range, a category, etc. Fortunately, conditionally loading a JavaScript in WordPress is pretty simple. In the above example, if the current webpage is single post, script will be enqueued. In short, wpenqueuescript () is the function that tells WordPress to add onenqueuea new JavaScript file for addition into WordPress. This should be used to enqueue scripts and styles only in the pages they. It seems like a waste to load something like a Masonry library on every page when it may never be needed. It provides a single parameter, hooksuffix, that informs the current admin page. I have often found myself building a website where only one or two pages need some JavaScript functionality.
