Look into wp_register_script() and wp_enqueue_script(). You may be able to get by with only the latter.
Pay special attention for your case of wanting it before the closing </body>
that you can specify in_footer
as a parameter.
wp_enqueue_script( $handle, //(string) name of script
$src="", // uri to script
$deps = array(), //handles of other scripts this script is dependent on
$ver="1.0", version number of script
$in_footer = true //if script should be in footer
)
You can hook the above via the wp_enqueue_scripts
hook
function enqueue_your_scripts() {
wp_enqueue_script( 'script-name', plugin_dir_url( __FILE__ ) . '/js/example.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'enqueue_your_scripts' );
In the above, plugin_dir_url( __FILE__ )
is returning the plugin directory the file is in, and we’re assuming a subdirectory of /js/
and a file named example.js
Related Posts:
- Ajax call doesn’t work in frontend but it’s working in backend (when I’m logged in)
- Pulling data from custom plugin settings using PHP shortcode and Javascript
- Redirect to another page using contact form 7? [closed]
- Pass user role to javascript code inside body
- Problem with wordpress has_action() function
- Multiple image selection custom widget
- Create a form and have custom menu display based on user answers?
- How to limit each front-end user to view just his own uploaded files on Amazon S3?
- Creating a user ‘add custom field’ section
- How to create post comparison in wordpress
- Problems with autoloading classes via sp_autoload_register / maybe interfering WP-specific autoloader
- How to remove duplicate sub-menu name for top level menu items in a plugin?
- category_name not working (not showing up in sql query debug)
- How can I make my custom shortcode work in a Custom HTML Widget?
- How to Loop Plugin Option Field Set?
- Update Multiple Post Meta for the Same Post In One call?
- How do I configure WordPress to talk to a Microsoft SQL Server database?
- Rename existing old uploaded images containing accented French characters (not during upload)
- How to Create a Directory in a Plugin Folder?
- How to Reload the Dashboard After Clicking Update in Quick-Edit?
- Create plugin that works in background
- Checking for user role in a custom plugin
- White text on white background in TinyMCE when wp_editor is called in WP 3.9
- Is it possible to filter the wp_footer() scripts, read the content, and insert them inline?
- How can I make the “Preview Post” button save and preview in the same window?
- HELP: Integrating WP with raw PHP code once published button is hit
- Loading class files via autoload method
- I would like to use create a function in my custom plugin to tell WP to use a different header
- Has anyone used require.js for handling plugin scripts?
- Passing RichText attributes to function onChange
- jQuery function didn’t work in my plugin
- Escape when echoed
- Write to / remove from default .htaccess file from plugin?
- How to cancel WordPress’ action/filter when using OOP with anonymous callback
- How to load plugin after page is loaded – pagespeed issues
- Programatically download a plugin
- Redirecting to home page after login as custom role WordPress
- How would I go about creating a user ranked post popularity page?
- add image crop function like in wp customizer
- Calculate price and display on woocommerce product single page under price (simple price, variable price) [closed]
- How to edit content before post update
- wp.media javascript issue with on select
- Create a plugin from within WordPress
- Class variables not correct on page
- Why is one phpunit test throwing an error on one class when all other classes are similar without error?
- Activate my plugins via FTP
- Why is my custom post type not being activated on plug-in activation?
- How to add apply_filter for a class property in plugin
- JavaScript in a PHP plugin
- How to generate an all in one WordPress New content, plugin and theme update report on a website? [closed]
- A server-side hook failed when committing plugin code to SVN
- Content-Security-Policy Headers are there and showing the correct settings, but still getting a refused connection
- How do I add $_SESSION[”] to my wordpress page?
- output html on post or page from custom plugin [closed]
- Built a second plugin but it overwrote the first one
- How to delete all categories programatically?
- Adding a simple Javascript file with a plugin
- How to add/change (woocommerce) product tab icon
- Comment “like” problem – “users who like this” avatar linking to current user profile instead of “liker’s” profile
- I want to generate the pdf and save it in library?
- Using meta_query in a WP_Query not working for numbers properly
- How to write to a separate PHP file in plugin settings
- How to display results from a data table with an encrypted user id?
- What’s the default setting for including a in a post
- Code for unique user visit count on every page WordPress
- Set Button in PluginDocumentSettingPanel Content (WordPress Gutenberg)
- Checkbox show / hide output result
- admin_notices show after load completed
- Activate Plugin Automatically After Set Time
- Fixed: Console.log twice in the edit function
- How can we get this dynamically as this folder may not be by the same name always → wp-admin
- Custom Plugin: How to Include Install Buttons of other 3rd Party Plugins?
- Why is my shortcode not working?
- How can I create a secure file download in WordPress?
- Getting Fatal error: Uncaught Error: Call to undefined function plugin_dir_path() when linking to another file within my wordpress plugin
- front end editor creation for Restropress plug in – displaying information from a WP admin area, on a different URL
- How can I see $post object in frontend from functions.php?
- Move plugin pages from dashboard to front-end
- implement checkbox or toggle switch in plugin code
- 500 Internal server error wp_handle_upload_prefilter
- Custom Field used to allow a Free Story; no longer works
- Get post content inside plugin class method
- Enabling plugin on specific pages and subpages
- Problem with conditional woocommerce custom checkout field
- How to change text in a page by utilizing a custom user id?
- How can I insert a record into a custom table from my custom form in my custom admin page?
- Need to hide an element depending on date and post category
- How to add a handler for a button in plugin?
- Can anyone tell me why I can’t edit a plugin when it is installed without having to re-install?
- Can’t insert files in other inputs
- Is it possible to run javascript on plugin deactivated?
- How do I output user_registered time in my correct timezone?
- Ajax in Plugins: returns the whole page
- How to fix ‘Call to undefined function do_settings()’ error?
- wordpress select multiple options and illegal string offset ‘timeslot’ [closed]
- How to tweak a plugin without preventing it from updating
- how to search through plugin in wordpress cimy-user-extra-fields?
- wp_handle_upload – specified file failed upload test
- How to make a Template page to show the information of different things Shop and Product page?
- public custom posts not showing in my wordpress plugin