0

I'm writing my second WordPress plugin, I successfully enqueued JavaScript and CSS (Bootstrap) to backend settings page. Now, to interact with frontend, I want to create some shortcodes using Bootstrap. How can I use Bootstrap (and other custom CSS and JavaScript) only in my shortcodes without affecting style of the pages that will use the shortcodes?

1
  • 2
    you can give your own class to the` shortcode` ad and target the element using that class Commented Mar 20, 2019 at 9:12

1 Answer 1

1

You can put a div-container around your content with a unique class name.

function shortcode_func(){
  return '<div class="your-unique-shortcode-class">...</div>';
}
add_shortcode( 'your-shortcode', 'shortcode_func' );

Than you can add your stylesheet with the wp_enqueue_style function.

function shortcode_footer_func() {
  wp_enqueue_style( 'shortcode_styles', 'your/path/to/style.css');
}
add_action( 'wp_footer', 'shortcode_footer_func' );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.