![]() If you’re working in a plugin, as in our example, using plugin_dir_url() with the magic _FILE_ constant is the easiest and most common way to get to the plugin’s root folder. Your wp_enqueue_script() call will be specific to your JavaScript file’s “shorthand name,” file location, and filename.Our example looks like: wp_enqueue_script('qgjs', plugin_dir_url(_FILE_).'quick-guide.js'). Add a body to the function which actually registers and enqueues your script, using wp_enqueue_script().Create that function qg_enqueue() with a basic function qg_enqueue() expression.Don’t save yet, because saving without having written the qg_enqueue() function itself will temporarily “break” your site. Write the line add_action('wp_enqueue_scripts', 'qg_enqueue').(If you’re trying to do this for a theme, you’d put the following code in your functions.php file.) If not, check out our primer on making a WordPress plugin. You’re going to be writing PHP, and we’re going to assume you’ve got a custom plugin set up to do this in.If ( 'myplugin_settings.And here’s the text version of that same guide for how to use wp_enqueue_script() in WordPress… How To Use wp_enqueue_script() to Add JavaScript to Each Page on Your WordPress Site This is the complete enqueue code for our example: add_action( 'admin_enqueue_scripts', 'my_enqueue' ) to ensure that you only enqueue your script where it is needed. In that case, use template tags such as is_home(), is_single(), etc. The front-end version does not pass anything. Use this information to only enqueue your script on pages where it is needed. The admin_enqueue_scripts hook passes the current page filename to your callback. For front-end pages use wp_enqueue_scripts, except for the login page, in which case use login_enqueue_scripts. For administration pages, use admin_enqueue_scripts. ![]() Scripts must be enqueued from one of a few action hooks – which one depends on what sort of page the script needs to be linked to. You cannot enqueue scripts directly from your plugin code page when it is loaded. Plugins_url( '/js/myjquery.js', _FILE_ ), The enqueue call for our example looks like this: wp_enqueue_script( Always use an array even if it is for a single dependency. Since we are using jQuery to send an AJAX request, you will at least need to list ‘jquery’ in the array. The third parameter is an array of any script tags that your script is dependent on. If you are enqueuing the script for something besides a plugin, use some related function to create a proper URL – never hardcode it. ![]() For portability, use plugins_url() to build the proper URL. The second is the complete URL to your script file. The first is an arbitrary tag or handle that is used to refer to your script in other functions. The enqueue function takes three parameters. As a plugin developer, you do not have ready access to the header template, but this rule bears mentioning anyway. Never hardcode such links in the header template. Use the function wp_enqueue_script() to get WordPress to insert a meta link to your script in the page’s section. Never send requests directly to your plugin pages. The other is that all AJAX requests need to be sent through wp-admin/admin-ajax.php. One is the need to enqueue scripts in order to get meta links to appear correctly in the page’s head section. This section covers the two major quirks of AJAX in WordPress that trip up experienced coders new to WordPress. Second is the actual handling of the AJAX request. First we need to enqueue the jQuery script on the web page and localize any PHP values that the jQuery script needs. There are two parts to the server side PHP script that are needed to implement AJAX communication. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |