put wp_enqueue_script() in your shortcode handler.
add_shortcode('myshortcode', 'my_shortcode_handler');
function my_shortcode_handler($atts) {
wp_enqueue_script('my-script', plugins_url('my-script.js', __FILE__), array('jquery'), '1.0', true);
// actual shortcode handling here
return "bacon flavored shortcode here";
}
this definitely loads the my-script.js file for me. it gets loaded in the footer though. i don’t think there is a way for it to load conditionally in the header as it won’t have gotten to the post content to know whether the shortcode is used or not.
check this:
http://scribu.net/wordpress/conditional-script-loading-revisited.html
Related Posts:
- Styles and Scripts, Selectively enqueue across entire site
- Unable to load stylesheets and scripts to plugin settings page
- Minimizing scripts & css files that plugins add
- Best way to overide plugin CSS?
- How can I use WordPress functions in my stylesheet?
- Conditionally enqueue a widget’s script/stylesheet in HEAD (only when present on page!)
- How do I cleanly override a plugin’s CSS with a child theme?
- Why does wp_enqueue_style() in plugin not load stylesheet?
- How can I reduce the amount of files loaded/included per plugin?
- How can I dequeue a Plugin Stylesheet?
- Some way to Flush or Overwrite all Imported Content in a Site for a Fresh Import – How?
- Add CSS animation as Preloader to WordPress
- Best way to inject css into admin_head in plugins?
- Layout shop page: resize images and columns
- How extend shortcode default values for a plugin?
- Is it possible to use Snap Scrolling? [closed]
- Image resizing on home page (product category) [closed]
- W3 Total Cache: Load CSS asynchronously for better PageSpeed score? [closed]
- How do I write into a file (css)
- Why isn’t “background-image: url”working with Bootstrap carousel on WP?
- How to generate CSS from a shortcode within a plugin
- Custom CSS for plugin form
- How do I modify the position of a plugin?
- Is there an earlier hook than login_head or login_enqueue_scripts?
- How to disable CSS rule in Twentythirteen style.css?
- Theme’s default styles are overriding my plugin’s custom CSS
- Remove ALL css for a specific plugin page
- CSS from textarea in options page to frontend what to do
- custom changes removed after plugin update
- Can someone please tell me what is wrong with my plugin?
- Scripts at the end of the page
- WordPress plugin output formatting
- how to access all user dashboard on fronted page [closed]
- What would I need to write into a custom plugin in order to add a switch for a custom string of CSS to the edit page?
- Bootstrap and Custom CSS in custom plugin are being overwritten by theme
- Ajax Search Lite: Customize Autoscroll
- How to use my menu PHP code as sidebar?
- Add script into front from my plugin
- How to add CSS via custom plugin?
- Plugin Theme CSS [closed]
- Full list of registered scripts or styles, but from an admin options page
- How to add a dvi tag to a shortcode then change a generated text using jQuery
- Hide a Menu Item on Login
- How to connect rating to individual comments?
- Wooslider custom css
- Basics of changing plugin output
- Making that Admin Bar transparent or a blue color
- Display a consistent Google Plus icon on ALL pages of my site
- plugin dev – minimum base css specificity?
- get specific value of a array | PHP
- How to make a dynamic css class whose name changes every visit to confuse scraper
- Moving Jetpack buttons to appear below the first H2 title on all project/post pages [closed]
- Historical customisations won’t go away [closed]
- Flickering sidebar when scrolling page [closed]
- change or add css to theme from plugin
- Add logo to admin menu in my plugin
- I would like to remove date display on photo preview
- At a Loss on over Page Peel Plugin Customization [closed]
- Is wp_deregister_script the best way to deal with unneeded plugins in a page?
- Blocking Plugin Css to load custom in template directory
- Block wordpress style for my plugin admin page
- WordPress Stats Plugin: Display Post Views [closed]
- uncaught TypeError: Cannot read properties of null (reading ‘classList’) custom plugin
- How do I minify a plugins CSS/JS code?
- Button and Countdown overlapping mobile menu
- Want to create custom design of progress indicator in wpforms
- Strange admin-ajax / CSS / $_SESSION issue
- inserting custom css, html and js into the current page as a popup window
- How to remove Unused CSS From a WordPress Theme?
- Overriding inline !important CSS with Javscript code snippet with media query
- Is there any known plugins that would replace WordPress classes in editor with their Bootstrap 4 equivalent?
- How to add specific script to WordPress webpage that will working with user input and databases
- Apply only the css in my style.css file
- How get horizontal div instead vertical CSS
- Changing the CSS with a plugin
- SASS not showing in customized preloader plugin
- Why can I not deregister my style and script on custom post type?
- Implementing Scrollable Images
- WordPress custom blockquote with black background & white text?
- Speeding up WordPress on a Local Network
- Edit the stylesheet of a child theme
- How do I override WordPress plugin css [closed]
- WP Plugin CSS not being applied to page
- Customize CSS for “Swipebox ” lightbox
- how to add custom css at top above all css file for specific url
- Adding javascript to header of MetroMagazine theme
- Best use adding user generated JS/CSS to a theme with a plugin
- Run one-time script calling plugin functions
- How to set up php,javascript and css to allow for our finished product to be displayed within a page?
- Like/Dislike Bar not working when updating
- Possible jquery / library conflict in plugin usage
- How to add different CSS for galleries
- How to activate/deactivate menu tab and keep the same id?
- MultiPostThumbnails and media queries
- How to display a sitmap horizontally? [closed]
- Content expands beyond its column container [closed]
- Woocommerce Product Page Collapsible Box Using CSS
- Scripts don’t enqueue after removing plugin
- Option value not getting updated until page refresh in WordPress
- How to create a “Most Popular” & “Latest” TAB in WordPress