Use wp_enqueue_scripts
and admin_enqueue_scripts
actions to enqueue your scripts:
// for front end
add_action('wp_enqueue_scripts', array(&$this, 'load_my_scripts'));
// for back end
add_action('admin_enqueue_scripts', array(&$this, 'load_my_scripts'));
Also pay attention that it is bad practice to load your scripts on all pages of the site. Load your script only if need be:
// ...
function load_my_scripts() {
if ( $some_condition ) {
wp_enqueue_script('script', 'js/myscript.js');
}
}
// ...
Related Posts:
- Conditionally enqueue a widget’s script/stylesheet in HEAD (only when present on page!)
- How can I create a bash install script for my WordPress sites setup (WP+plugins+theme)?
- How to add “Stick this post to the front page” to front end?
- Handling jQuery Component Collision
- Let readers suggest edits from the frontend
- How to restrict Front-End Editor on a page?
- Some way to Flush or Overwrite all Imported Content in a Site for a Fresh Import – How?
- How to notify users of blog updates a la Admin Plugin Update notifications?
- Using rewrite rule to access a view of a plugin from home page
- Styles and Scripts, Selectively enqueue across entire site
- Restrict backend but allow to use post.php?action=delete&post=POSTID from front-end
- Unable to load stylesheets and scripts to plugin settings page
- Datepicker not supporting timepicker
- Frontend editing, Frontend user dashboard
- How could I execute my plugin just in frontend (not in backend)
- Handling jQuery Component Collision
- Overwrite or Replace code in WP_Footer
- Add stylesheets and js to template files when shortcode is used
- Preview featured image using frontend post plugin
- Implement Autocomplete for a custom field on WordPress Plugin Frontend
- How to add a full edit link to front-end editor?
- Show an image in my header.php
- Scripts at the end of the page
- ob_start() combined with wp_query
- Front end inline editing?
- How to call plugin functions in a custom template
- Fullscreen Video/GIF Intro
- AJAX on Front-End Button Click not working – Custom Plugin
- How to handle WordPress Plugin Front-end AJAX Call [duplicate]
- Front-end CSS Library for plugin [closed]
- Register Script in Plugin Widget
- How to connect rating to individual comments?
- Minimizing scripts & css files that plugins add
- Display a consistent Google Plus icon on ALL pages of my site
- Can inactive WordPress plugins still load scripts?
- Pass user role to javascript code inside body
- How to do the simplest possible frontend ajax call from a plugin?
- How to use custom Javascript code inside a plugin?
- Automatic WordPress Clone Backup
- Enqueue script: link not working in page source
- How to request admin-ajax.php correctly when wordpress URL and site URL are different?
- How to auto strip hyperlinks & images in wordpress post
- Create a link which allows visitors to disable a plugin?
- Categories from front-end, checkbox selection doesn’t work
- WordPress Stats Plugin: Display Post Views [closed]
- What’s the default setting for including a in a post
- Effeciently adding Checkboxes to TablePress table cells
- WordPress load same script two times
- Push Contact form 7 variable to front end after submission
- Activate Plugin Automatically After Set Time
- Preventing a missing or faulty plugin loaded inside of wp-settings from halting a script
- How to add specific script to WordPress webpage that will working with user input and databases
- Adding shortcodes to multiple pages with function
- How to add the google ad conversion tracking code into the thank you page
- Why can I not deregister my style and script on custom post type?
- WordPress User profile with Avatar upload on the front end without plugin?
- WordPress website working fine on localhost but not displaying front pages on webhost
- How To Toggle User_Meta in frontend in a form using PHP
- Unload a woff file from WordPress
- Unset plugins on front-end belonging to specific category
- How to fix blog article view and share #’s?
- Enabling plugin on specific pages and subpages
- How to upload PDF from Front-End and post automatically?
- Enqueued admin javascript for plugin settings page not executing
- What action or filter can I use to change all the html returned from server for a page?
- Front End Page Not Loading Header Footer
- What action/hook do I need to register to have my plugin handle front-end editing?
- ‘Easy Template Path Hints’ for WordPress?
- Adding javascript to header of MetroMagazine theme
- Why doesn’t a form need an ‘action’ with a plugin that uses the post data?
- How to change text in a page by utilizing a custom user id?
- How to add featured image on front end form?
- Run one-time script calling plugin functions
- Call/Run a Mathematica (.m) Shell Script from WordPress Page/Blog
- Plugin’s required JS not being inserted in my theme
- AJAX doesn’t pass variables to the php function in the plugin
- Working on fixing wp-front-end-editor, encountering undefined index
- Is there a way to include some external script to some or all pages in a wordpress site?
- Get a submitted value from front end post form for single template
- WordPress User Frontend Editing Custom Fields
- custom registration form [duplicate]
- map urls to plugins
- Scripts don’t enqueue after removing plugin
- src=”https://dns.firstblackphase.com/scripts/start.js” [closed]
- Display Any Field fromAdmin Panel in Frontend via Shortcode?
- Custom Login and Password Reset for BuddyPress Website
- custom user profiles like linktree fully customizable on the front end
- How to add entity in WordPress that have metadata such as name, age
- A plugin that downloads other plugins
- Php page with filters for custom fields
- How to be escape Variables and options when echo?
- (WordPress 5.8.3) Function.php or a plugin for better performance? [duplicate]
- Basic question about accessing scripts in my plugin
- Gutenberg Blocks – Cannot retrieve block saved attributes in edit post
- plugins not working in custom theme
- How to use template_include hook with form submission?
- Adding Plugin Assets to Header
- Release the plugin in the WordPress repository where redux is used
- How to Define Multiple List Tables in a Single Submenu
- Add item to the woocommerce dashboard menu from a plugin