[wp-trac] [WordPress Trac] #57267: wp_enqueue_style is not working under shortcode

WordPress Trac noreply at wordpress.org
Thu Dec 15 13:08:25 UTC 2022


#57267: wp_enqueue_style is not working under shortcode
-------------------------------+------------------------------
 Reporter:  hasanrang05        |       Owner:  (none)
     Type:  defect (bug)       |      Status:  new
 Priority:  normal             |   Milestone:  Awaiting Review
Component:  Shortcodes         |     Version:  6.1.1
 Severity:  normal             |  Resolution:
 Keywords:  reporter-feedback  |     Focuses:  css
-------------------------------+------------------------------
Changes (by costdev):

 * keywords:   => reporter-feedback
 * component:  General => Shortcodes


Comment:

 Hi @hasanrang05, welcome to Trac!

 I have tried the following:

 1. Insert a `Shortcode` block and enter the following shortcode:
 `[my_shortcode]`.

 2. Create the following stylesheet `wp-content/plugins/test_57267/css/my-
 shortcode.css`:
 {{{#!css
 .my_shortcode { color: red; }
 }}}

 4. Create the following plugin file: `wp-
 content/plugins/test_57267/test_57267.php`.
 5. **Test 1** - Add this to the file and activate the plugin:
 {{{#!php
 <?php

 /**
  * Plugin Name: Enqueue a style in a shortcode.
  */

 add_action( 'init', 'register_style_57267' );
 function register_style_57267() {
         wp_register_style( 'my_shortcode_styles', plugins_url( 'css/my-
 shortcode.css', __FILE__ ) );
 }

 add_shortcode( 'my_shortcode', 'my_shortcode_callback' );
 function my_shortcode_callback() {
         wp_enqueue_style( 'my_shortcode_styles' );
         return '<p class="my_shortcode">Hello</p>';
 }
 }}}

 6. Load the post on the frontend. You should see [[span(style=color:
 #FF0000, Hello )]].

 7. **Test 2** - Change the contents of the plugin file above to:
 {{{#!php
 <?php

 /**
  * Plugin Name: Enqueue a style in a shortcode.
  */

 add_shortcode( 'my_shortcode', 'my_shortcode_callback' );
 function my_shortcode_callback() {
         wp_enqueue_style(
                 'my_shortcode_styles',
                 plugins_url( 'css/my-shortcode.css', __FILE__ )
         );
         return '<p class="my_shortcode">Hello</p>';
 }
 }}}

 8. Refresh the page on the frontend. You should still see
 [[span(style=color: #FF0000, Hello )]].

 In both cases, I saw [[span(style=color: #FF0000, Hello )]] on the
 frontend.

 -----

 Can you provide more detail about what's not working? Could it be a
 plugin/theme conflict occurring?

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/57267#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list