![]() ![]() Then, place the file either inside your theme or plugin directory. wpenqueuescripts is the proper hook to use when enqueuing scripts and styles that are meant to appear on the front end. WordPress has a suitable function to achieve this, is_page(). First, you need to create a separate file for your code. ishome() is for when the front blog page is being display. Conditionally loading assetsįor this particular scenario the assets only need to be loaded on the contact page. 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. Our next task is to specify the pages we’d like to load them on. IE10 and above does not support conditional comments in standards mode. Load scripts (or stylesheets) specifically for IE. Now the assets will not load on any pages. Enqueue scripts for IE only addaction( wpenqueuescripts, enqueuemystylesandscripts ) / Enqueue scripts (or styles) conditionally. ![]() Only enqueue scripts on a single page/post. However, CF7 provides a filter that can be accessed, for which we can just return false, thus not enqueuing anything: /**Īdd_filter('wpcf7_load_js', '_return_false') Īdd_filter('wpcf7_load_css', '_return_false') Quform WordPress Guides Advanced Loading scripts on specific pages only. A generic way of achieving this would be to find the assets in the $wp_styles and $wp_scripts variable. 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. as the other scripts your site may crash and you may not have access to the website. The first thing we need to do is stop the default action of the assets being loaded on all pages. We now know what enqueueing is and how it works. There are a few ways to add your custom js to your WordPress theme. NB: For the rest of this article I’ll refer to scripts and styles collectively as assets. Preventing/dequeuing the script and styles #Wordpress enqueue script on specific page how to#This article will demonstrate how to conditionally load the JS and CSS for CF7, but the concept could be adapted to apply to other plugins. One such example would be to only load the JavaScript and CSS stylesheets for Contact Form 7 ( a lightweight contact form plugin) on the contact page. There are many use-cases where scripts and stylesheets only need to be loaded on certain pages. Updated on August 28th, 2020 Conditionally enqueue scripts and stylesheets on specific pages #Wordpress enqueue script on specific page code#Originally published on November 3rd, 2018 All Languages > CSS > enqueue script for specific page wordpress enqueue script for specific page wordpress Code Answer. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |