To properly include style you need to do it with wp_enqueue_scripts
action.
With a shortcode, you need to include the file differently as the header has already been send.
One way, Can be to include file in the shortcode. Other way, in the wp_enqueue_scripts
callback function can be to scan the_content
, detect shortcode and include the style like you’ve done.
If you make your own plugin, you can enqueue the style on each post conditionnaly with has_shortcode
function custom_shortcode_scripts(){
global $post;
if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'custom-shortcode') ) {
wp_enqueue_script( 'custom-script');
}
}
add_action( 'wp_enqueue_scripts', 'custom_shortcode_scripts');
Related Posts:
- Conditionally Loading JavaScript/CSS for Shortcodes
- How to add stylesheets only to pages with specific shortcode?
- How to check if a shortcode is being executed in a widget or post
- Custom shortcode in widget forced to top of widget
- How to use shortcodes on a widget sidebar when doing an ajax call?
- gutenberg widget outputting paragraph marks
- Why are Shortcodes Disabled in Widgets by Default?
- List monthy archives for specific year with wp_get_archives
- Custom shortcode is not working in text widget
- How to force that styles are enqueued in the header?
- add widget to page content (using shortcode)
- Conditionally Loading JavaScript/CSS for Shortcodes
- How to check if a post has any one of many shortcodes?
- How to add shortcode to html widget
- Add inline style to pages where shortcode was used
- Use atributes of shortcode in get template part
- How to completely prevent WordPress from destroying/modifying my shortcode outputs?
- Loading shortcode stylesheet only when shortcode is in text widget [closed]
- How do I get shortcode, widget and template tag CSS to load in the head only as required? [duplicate]
- do_shortcode autommatic content generation
- Add widget/shortcode to sidebar OUTSIDE of the theme
- how to have the gallery shortcode output one single UL list instead of several DL?
- Get post id shortcode
- How execute shortcode with javascript
- Getting attribute value from shortcode
- My shortcode is not working in Contact Form 7 Message Body
- How to make this shortcode work for post content
- How I make a shortcode for this code?
- Problem with email_exists in shortcode
- Get shortcode from the content and display it in other place (in sidebar, for example)
- Alternate text if shortcode returns no data
- sql query in shortcode not working
- Global, network-wide shortcodes or text replace functions
- How to use div class between the shortcode variable?
- Error do_shortcode In WooCommerce Template
- Embedding Instagram via shortcode doesn’t work as advertised in official docs
- Executing a shortcode from a “normal” text/hyperlink
- Execute shortcode only once in the page
- WooCommerce Multi-Currency shortcode not working
- Order Woocommerce Products by Latest Reviewed
- $content not null in shortcode, even though it is self closing
- Nesting shortcodes results in the inner shortcode being placed AFTER outer shortcode
- Create a shortcode that creates a form for users to edit their information
- How to split text text text into array
- i have create shortcode that work but not perfect coding
- How do you stop a shortcode from firing in the editor?
- menu item to display the most recent post
- how to format / execute post content and shortcode?
- WP – Genesis – How to get the content of a widget in php? [closed]
- How can I implement shortcode on click event
- Login form from shortcode doesn’t redirect after successful login
- Single post content custom order
- Adding a class to shortcode API
- Parse Error: unexpected ‘}’ while creating Shortcode with Loop [closed]
- Pass data back to TinyMCE from Thickbox
- Forcing WP to embedd a video when using a shortcode
- Shortcode for output of wp_get_archives displays at top of post
- How can I execute shortcode outside the loop?
- Shortcode is not returned correctly
- Echo HTML in custom shortcode
- Add Shortcode tag in Widget/Sidebar
- How do I make a shortcode?
- How to have shortcode autofilling the content if the attribution is specified?
- Do I need to escape number in this shortcode function?
- How can I list only custom shortcodes?
- Substite Category Slug in a Shortcode
- FontAwesome Shortcode isn’t working properly
- Change variable value for different shortcode attributes?
- How to copy [gallery] shortcode between posts using polylang [closed]
- How to add a recent post function to a text widget I created for practice?
- Shortcode not displaying video as expected
- How to add a shortcode that creates an adjacent column of text in the content editor?
- How to Display Shortcode’s Content after Short Product Description?
- Jetpack gallery shortcode with slideshow
- Shortcode atts for WP Query args
- return an array with a shortcode
- PHP not outputting the onclick in anchor tag
- Shortcode generated widget to appear on same line as heading text and button
- Create Button for Shortcode in Text Editor
- Send Mail with link to current_user
- shortcode atts not working
- Title not Emptying in a Widget
- do_shortcode for is_page(slug)
- Create shortcode to display specific post in relationship current date
- Add custom shortcodes to the Welcome Mail
- Nested WordPress Shortcodes of Different Types
- WordPress Not rendering short code properly?
- Initialize add_action() for every copy of a specific shortcode
- Can I use widgets & shortcode if I need to return an array?
- WP_Editor Shortcode Issue
- Shortcodes that are created based on user input information
- Remove some tags from shortcodes output? Trying to fix autop
- do_shortcode & render custom field won’t work
- shortcodes inside shortcode to sum values
- is_mobile as shortcode
- How to attach sidebar to shortcode’s output?
- Is it possible to change a shortcode parameter based on a media query?
- Wrapping a function in html tag and return?
- How to edit a widget code?
- Why does my shortcode query not working with search term