In this case, has_shortcode() is your friend.
First, we hook into wp_print_styles, instead of wp_head
. In the func we hook
we check if the post contains our shortcode. If it does, we get the CSS we want and output
it as an inlined <style>
.
add_action ('wp_print_styles', 'wpse_enqueue_shortcode_css') ;
function
wpse_enqueue_shortcode_css ()
{
global $post ;
if (is_single () && has_shortcode ($post->post_content, 'my_shortcode')) {
$append_css = get_post_meta ($post->ID, 'append_css', true) ;
echo <<<EOF
<style type="text/css">
$append_css
</style>
EOF;
}
return ;
}
Related Posts:
- How to add stylesheets only to pages with specific shortcode?
- Conditionally Loading JavaScript/CSS for Shortcodes
- How to force that styles are enqueued in the header?
- Adding Shortcode Values to wp_head
- Conditionally Loading JavaScript/CSS for Shortcodes
- Is it possible to modify the header with a content shortcode?
- How to check if a post has any one of many shortcodes?
- Shortcode inside text widget do not call enqueue style
- Why are you using add_action for shortcode?
- How to get URL param to shortcode?
- Making a Shortcode [NEXT] and [PREVIOUS] to place into specific posts for post navigation
- How to display the site name in a WordPress page, or post
- Native “playlist” shortcode random playing… How?
- How to display the names of users from a specific group with a shortcode?
- shortcode for logo image
- Stop strip_shortcodes() stripping content inside shortcodes
- How do i link gallery thumbnails to different url’s using the wp native gallery?
- Remove specific CSS and JS from the head
- Gallery shortcode menu order fix no longer working on WordPress 4.0
- How to save a shortcode in a Gutenberg custom block?
- Is shortcode_unautop() broken?
- Some doubts about gallery shortcode in WordPress
- Can a shortcode function this way
- Dynamically create shortcodes using add_shortcode and a callback
- Passing attributes to shortcode dynamically
- Stop parsing shortcodes
- How can I make a shortcode from this code?
- Meta tags in shortcode
- Adding PHP/HTML code inside page from custom template
- shortcode javascript not working on custom template file inside theme folder
- shortcode get thumbnail size
- Shortcodes Keep Wrapping in
- Stop auto formatting in shortcodes
- Get URL Param Plugin and Inserting Result in Widget Code
- Problem with using custom shortcode with ACF WYSIWYG field
- Preserve shortcode content formatting
- Populate shortcodes with table data and get next row
- Running shortcode with variable that is modified
- Reloading a Mediaelement audio player
- Shortcode is not processed when added to option field using wp_editor
- How to apply an ID to Shortcode Embedded iframe
- wp_list_pages shortcode jumps above previous content
- Retrieving post excerpt as a shortcode
- Using $this from child class inside shortcode function
- Insert PHP code via shortcode?
- What is the correct syntax to append a URL using a shortcode for an iframe?
- Shortcodes in RSS excerpts
- How to trace shortcode to its functionality
- shortcode issue in the_excerpt
- Display Something in the Header After All Styles are Loaded
- If else with shortcodes
- Using shortcodes to communicate my page design
- enqueue script with get_shortcode_regex
- Use Contact Form 7 with Ajax
- Clean way of using ob_start() and ob_end_clean() in wordpress
- Theme Convert PHP code to shortcode
- call other shortcode in the email contactform7 send [closed]
- find shortcode inside content of post
- WordPress Short code for List style
- Adding a cover image to video shortcode
- Dynamic number within shortcode
- How can i strip shortcode from HTML title tag?
- Show content if user left comment / non-logged visitors?
- How to insert current user ID/entry ID into a shortcode?
- Need help with creating a simple shortcode
- Returning HTML to Shortcode Block
- WP REST API remove the WordPress shortcodes from the JSON wp/v2/posts content->rendered
- Using a WP shortcode in Homepage
- i want to show a modal when user lands on home page very first time
- Hide download option video shortcode
- WordPress Shorcode Display hierarchical taxonomy [child, parent]
- Shortcode not working in Jetpack Top Posts & Pages widget
- Gutenberg running code only available in front end within shortcode
- Insert HTML in content property and render it using shortcodes
- Redirect per shortcode if user is logged in
- Logout user if click on a custom page link
- Shortcode Attribute not defined
- Second attribute of shortcode is not changing why?
- Is Post-Author-Id shortocde
- Load next post by ID on a single page
- Plugin Shortcode value in post
- Add catagory attribute to custom shortcode
- Woocommerce Filter Main Loop by Tag [closed]
- Shortcodes: override a shortcode / change shortcode on the fly
- Class variables in shortcodes
- Separating Custom Template logic
- Remove width and height attributes from [gallery] output
- Shortcode for sectors
- How to not show shortcode contents if cart is empty?
- woocommerce Product shortcode create [closed]
- Help with Shortcode Functionality
- Shortcode Site include in a other
- Why is my ajax live search not working when i use a shortcode to call it?
- How do write this type shortcode? [closed]
- WordPress functions.php shortcode is not working [closed]
- My first WordPress shortcode just returning []
- WordPress shortcode select option not working [closed]
- Working with Shortcode, AJAX, Elementor
- How to exclude some cats from wp_list_categories using shortcut
- How can I create a shortcode that shows a list of categories on the single product page?