Custom CSS for plugin form

I have a short code that is adding a form to a page. How do I ensure that the CSS for the form is loaded in time with the shortcode?

Related posts

Leave a Reply

3 comments

  1. you can check with the_posts hook if your shortcode exists and only enqueue the style if so :

    function check_for_shortcode($posts) {
        if ( empty($posts) )
            return $posts;
    
        $found = false;
    
        foreach ($posts as $post) {
            if ( stripos($post->post_content, '[CHANGE_TO_YOUR_SHORT_CODE') )
                $found = true;
                break;
            }
    
        if ($found){
            $url = get_bloginfo( 'template_directory' );
            wp_enqueue_style( 'my_login_Stylesheet',$url.'/my_login_Stylesheet.css' );
    
        }
        return $posts;
    }
    add_action('the_posts', 'check_for_shortcode');
    
  2. You add the css for this form to your theme’s style.css. The css is already there no matter if you have a form or not. That’s the easiest solution.