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
- Using require_once in a Plugin?
- How to Loop Plugin Option Field Set?
- How to call plugin path in JS?
- AJAX Call Via Vanilla JavaScript In WordPress Plugin Development
- How can I make content disappear when a user logs in?
- Rename existing old uploaded images containing accented French characters (not during upload)
- How to Create a Directory in a Plugin Folder?
- How to save Clicks of a download link button while it doesn’t matter if we refresh the page or again login/logout
- 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?
- HELP: Integrating WP with raw PHP code once published button is hit
- Loading class files via autoload method
- How to create popup same as wordpress popup feature
- I would like to use create a function in my custom plugin to tell WP to use a different header
- Passing RichText attributes to function onChange
- Getting media library popup in custom plugin admin page
- jQuery function didn’t work in my plugin
- Sanitizing, Validating and Escaping in WordPress (Plugin)
- Escape when echoed
- WooCommerce get_price returning wrong price when used via ajax
- Open Post Thumbnail in New Child Theme File in WordPress
- Redirecting to home page after login as custom role WordPress
- Add keywords meta to index.php page
- Updating the Drag-To-Share eXtended share URLs?
- What are the specifics of WordPress development I need to know? [closed]
- How to create a custom post type that contains another custom post type?
- 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?
- Why is my custom post type not being activated on plug-in activation?
- JavaScript in a PHP plugin
- How to generate an all in one WordPress New content, plugin and theme update report on a website? [closed]
- 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
- 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
- More gentle way to hook WordPress custom url
- 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
- How to Call Specific .PHP file on add_submenu_page selection in Plugin Development?
- 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?
- Hide content for specific users with id
- adding dynamic/multiple slug values in ‘option_none_value’
- 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
- 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?
- Saving plugin data returns “You do not have sufficient permissions to access this page.”
- 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?
- Load my plugin before cache
- 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?
- Function not being called on form submit, only blank admin-post.php page
- How can I save the selected page in the dropdown after anyone clicks on Save Changes?
- How can I save the selected page in the dropdown after anyone clicks on Save Changes?
- WordPress Throwing Deprecated Errors on its own Files
- plugin doesn’t retrieve data from database
- Woocommerce attatch files and custom input meta to cart items and then to order
- Call to undefined function create_function() – PHP 8.2