2

Is there a way to enqueue my style.css (and other stylesheets) AFTER a certain plugin enqueues its styles?

Specifically, I want my style.css to overwrite cforms's styles, but when I change the priority of the add_action, it doesn't do anything.

Here's what I have:

    function rm_theme_styles()  
    {
            wp_register_style( 'rm_styles', get_template_directory_uri() . '/style.css', array(), null, 'all' );
            wp_register_style( 'jquery.fancybox', get_template_directory_uri() . '/js/libs/fancybox/jquery.fancybox-1.3.4.css', false, null, 'screen' );

            // enqueing:
            wp_enqueue_style( 'rm_styles' );
            wp_enqueue_style( 'jquery.fancybox' );
    }

    add_action('wp_enqueue_scripts', 'rm_theme_styles',10);

cforms:

    ### some css for positioning the form elements
    function cforms_style() {
        global $wp_query, $cforms_root, $localversion, $cformsSettings;

        ### add content actions and filters
        $page_obj = $wp_query->get_queried_object();

        $exclude  = ($cformsSettings['global']['cforms_inexclude']['ex']=='1');
        $onPages  = str_replace(' ','',stripslashes(htmlspecialchars( $cformsSettings['global']['cforms_inexclude']['ids'] )));
        $onPagesA = explode(',', $onPages);

        if( $onPages=='' || (in_array($page_obj->ID,$onPagesA) && !$exclude) || (!in_array($page_obj->ID,$onPagesA) && $exclude)){

            if( $cformsSettings['global']['cforms_no_css']<>'1' )
                    echo '<link rel="stylesheet" type="text/css" href="' . $cforms_root . '/styling/' . $cformsSettings['global']['cforms_css'] . '" />'."\n";

                    ### add jQuery script & calendar
                    if( $cformsSettings['global']['cforms_datepicker']=='1' ){
                        wp_enqueue_script('jquery',false,false,false,false);
                        wp_enqueue_script('jquery-ui-core',false,false,false,false);
                        wp_enqueue_script('jquery-ui-datepicker',false,false,false,false);
                    }
                    echo '<script type="text/javascript" src="' . $cforms_root. '/js/cforms.js"></script>'."\n";

            }
        }
    }

    add_filter('wp_head', 'cforms_style');
4
  • 1
    Is the code in your question your code, or cforms code? Can you post both? Aug 1, 2012 at 23:39
  • @ChipBennett Yes, that's my code. I don't want to mess with cforms's code (or any plugin's code). Cforms is a large plugin and there's no obvious functions.php for it, so I can't post it's code. I just know that it's JS and CSS files are loaded in the header after my enqueued stylesheets.
    – cfree
    Aug 2, 2012 at 15:06
  • "Cforms is a large plugin and there's no obvious functions.php for it, so I can't post it's code." - do a search for add_action( to find the Plugin's hook callbacks. You can look specifically for wp_enqueue_script( and wp_enqueue_style( to find those function calls, or wp_head and wp_enqueue_scripts and wp_print_styles for possible hook calls. Aug 2, 2012 at 15:09
  • @ChipBennett Found it.. Edited above.
    – cfree
    Aug 2, 2012 at 15:32

1 Answer 1

5

Note that cForms is hooking into wp_head, and you're attempting to hook into wp_enqueue_scripts. The wp_enqueue_scripts hook is fired inside the wp_head hook (at priority 0, IIRC).

So, your stylesheet is being enqueued at wp_head, priority 0, and the cForms stylesheet is being enqueued at wp_head, priority 10. Since it outputs later, it is taking precedence over yours.

The solution is to use the same wp_head hook, with a lower priority (i.e. a number higher than 10), for your stylesheet.

5
  • That makes sense, though when I changed the hook and priority, it was either added before cforms again (priority: 1-9) or added to wp_footer() (priority > 9) add_action('wp_head', 'rm_theme_styles', 11);
    – cfree
    Aug 2, 2012 at 16:06
  • Ah, if I echo out the <link>'s (instead of enqueuing them), then they get added to the <head> after cforms using wp-head hook and priority 11. It makes more sense just to place the links in header.php. Thanks for the help!
    – cfree
    Aug 2, 2012 at 16:14
  • 1
    Yep; using wp_head, you have to echo your stylesheet link. Sorry for not mentioning that earlier. :) Aug 2, 2012 at 16:15
  • Note also that wp_enqueue_style() allows you to specify other stylesheets as dependencies in it's 3rd parameter: $deps An array of registered style handles this stylesheet depends on. Default empty array. This wouldn't help in the cforms example as they don't register their stylesheet properly, but in general this is probably the better way to go!
    – benz001
    Aug 10, 2014 at 9:34
  • Great explanation for action hooks. Some plugins even uses other actions, e.g. template_redirect. Thanks!
    – BasTaller
    Aug 19, 2016 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.