wineqert.blogg.se

Wordpress enqueue script on specific admin page
Wordpress enqueue script on specific admin page









We can use WordPress’s is_page_template conditional to enqueue our script just in these cases: if( is_page_template( 'page-complete-gallery. We simply create a PHP script with a specific block comment at the top of the page, more details on this can be found on the WP Plugins handbook page. How do I Enqueue styles/scripts on Certain /wp-admin Pages I have two simple functions that load stuff using wpenqueuestyle() and wpenqueuescript().

wordpress enqueue script on specific admin page

Let’s say the filename for our template for masonry pages is page-masonry-gallery.php. 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. There are a whole host of useful conditional tags in WordPress. Within the action hook, you can use several functions and embed them in the functions.php file: wpregisterscript() wpenqueuescript() wpregisterstyle() wpenqueue. Let’s go through the basics of the wpenqueuescript hook that you’ll use to load your assets. Don’t forget to change the script and style handle name and path to the file.

WORDPRESS ENQUEUE SCRIPT ON SPECIFIC ADMIN PAGE CODE

Add this code to functions.php file to add scripts and styles in WordPress admin for all pages. This hook will be the same for both scripts and style. 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. To enqueue any scripts and styles in admin, WordPress provides a hook adminenequeuescripts.

wordpress enqueue script on specific admin page

Here is an example of loading a masonry library through enqueueing: wp_enqueue_script('masonry', get_stylesheet_directory_uri().

wordpress enqueue script on specific admin page

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. It provides a single parameter, hooksuffix, that informs the current admin page. Fortunately, conditionally loading a JavaScript in WordPress is pretty simple. Despite the name, it is used for enqueuing both scripts and styles. It seems like a waste to load something like a Masonry library on every page when it may never be needed. I have often found myself building a website where only one or two pages need some JavaScript functionality.









Wordpress enqueue script on specific admin page