here is a handy function i use a lot which is based on the_posts
hook
function check_for_shortcode($posts) {
if ( empty($posts) )
return $posts;
$flag = false;
foreach ($posts as $post) {
if ( stripos($post->post_content, '[YOUR_SHORTCODE_HERE') )
$flag = true;
break;
}
if ($flag){
//add scripts and styles here eg:
//wp_enqueue_script
//wp_enqueue_style
}
return $posts;
}
add_action('the_posts', 'check_for_shortcode');
change YOUR_SHORTCODE_HERE
to your shortcode tag and add your styles where it says
//add scripts and styles here
Related Posts:
- How to add inline css/js inside a shortcode
- Inject HTML meta tag inside wordpress tag using add_shortcode
- Change Page’s Tag Using functions.php File
- All shortcodes not working on custom theme
- How can I reduce the amount of files loaded/included per plugin?
- How to parse nested shortcodes?
- Adding Custom Fields for Img in Posts
- How To Ignore a Filter On Applying Filter the Content In a Function
- Shortcode display outside the div
- Pass $this to function nested in another public function of the same class
- change plugin shortcode function [closed]
- Dynamic HTML not displaying at respective place
- Calling shortcode in wp_localize_script strips result
- WordPress shortcodes & performance
- Is it possible to make shortcodes NOT case sensitive?
- Short code for Venues
- Looking for a solution – slightly convoluted problem
- Can’t get custom posts of taxonomy to show
- WP theme with Backbone
- add action wp_head not working
- Shortcode doesn’t work if I directly paste its function in a template file?
- Can wp_localize_script be used within a shortcode?
- get shortcode value
- How to generate CSS from a shortcode within a plugin
- Custom CSS for plugin form
- Show history of post revisions on front end
- How to remove html code in excel downloads using phpspreadsheet with wordpress plugins shortcode
- Shortcode based chart plugin
- Plugin shortcodes not working in custom theme
- Input with spaces in Shortcode attributes overwritten by defaults
- How to put JQuery/Ajax inside shortcode?
- Snippets: is it better to add them in functions.php or make site-specific plugins?
- Can someone please tell me what is wrong with my plugin?
- Trouble with editing template for “List category posts” plugin
- WordPress Shortcode to get URL Parameters $_GET[‘name’] redirects for no reason at all
- WordPress function not being called from jQuery method
- How to fix wrong attribute error for Visual Composer Grid Builder?
- creating html reusable blocks via shortcodes
- Single API call exposed via shortcode with params
- SEO Friendly URL on dynamic product page produced via shortcode
- Shortcode Plugin to dynamically build a shortcode via `do_shortcode` not working
- Making sure that a plugin’s scripts and stylesheets are included for dynamicaly loaded content
- Insert content of a post into another
- How to Create a shortcode to this php function
- Use $_GET inside a shortcode print empty Array
- Render Modula Plugin Shortcode On Ajax Request
- How to add a dvi tag to a shortcode then change a generated text using jQuery
- Deactivate JS Script in Plugin Shortcode
- plugin shortcode not working on ajax request call
- Shortcode not getting replaced
- How to activate a plugin on the activation of a theme?
- Plugin that provides the [edit] shortcode?
- Adding Plugin Assets to Header
- IF condition based on wp_remote_get output
- shortcode not return any thing in Gutnburg
- Shortcode Attributes to Return different $_POST
- Is there a way to convert shortcodes to html content?
- How to manipulate the content within wp_head
- Shortcode not working – quotes seems strange
- Get param from shortcode in plugin function
- Inserting shortcode in href – any ideas or workarounds?
- Get Image Having the ID [closed]
- Add the_post_thumbnail_url to a shortcode in function.php
- Grab ID from post from plugin and use it to create an object
- Form Plugin for Api Requests which is used via Shortcode
- Echo out element to another page.
- Load CSS files in header of Bones theme?
- Adding a Tag Parameter / Filter to My Shortcode
- Filters on the_content with plugins, pages and shortcode
- Which is a better practice when writing shortcodes: pack lots of configuration parameters or just give an id?
- Help understanding dynamic endpoints based on where a shortcode is used
- How do I use a plugin’s shortcode attribute value in another function?
- i create functions.php in plugin files but add_action (‘wp_head’, ‘function_name’) don’t work
- Make the product page as homepage WooCommerce
- Image path in childs theme
- Shortcode function not showing up on mobile browser
- Using Shortcode Result In Custom HTML Block (Using Google Sheet JQuery Result As NumScroller data-max)
- Get Current Post ID in WP Loop For GiveWP
- Shortcode generated widget to appear on same line as heading text and button
- Page takes on two different formats
- Sanitize AROUND shortcode
- Get not the full path
- Sending post data over REST API, how to parse shortcodes in post_content?
- Query Shortcode from a multisite to appear on a different site?
- Best use adding user generated JS/CSS to a theme with a plugin
- shortcode which is introduced into entry the blog, and appears in side bar
- Multiple level shortcodes
- Space in WordPress Attribute Causing Problems
- Shortcode registered from a plugin not recognized
- Strange get_the_* behavior with php 5.4
- Convert usernames listed by the Groups plugin shortcode to displayed names?
- Embedding BitBucket Code in Posting
- Do shortcodes affect page indexing by search engines?
- do_shortcodes() isn’t working
- How to show in front End images using Visual Composer attach_images?
- Seo Problems in My meta Discription [closed]
- cURL External API request displays content above other content on page
- Beginner question: Accessing js script in plugin
- thumbnail_width not working in wordpress most popular post plugin
- Display file contents within Plugin