WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] WP Enqueue/Register Styles Feature Request (18 posts)

  1. Josh Levinson
    Member
    Posted 1 year ago #

    Coming from a theme perspective, I think it would be nice to have an option in wp_enqueue/wp_register_style to allow for the specified style-sheet to be loaded last. I realize that the functions already have the "dep" parameter to allow for an array of dependencies, but this is obviously not a solution to my request as one would have to load in each registered sheet they wanted to override, including new ones as plugins are added. This problem has recently come to my attention as the need to add !important declarations in many of my rules has become an issue, specifically in the case of overriding plugin styles (like Gravity Forms).

    This request could possibly be answered by answering this: In what order are styles queued, apart from the "dep" parameter? Do they have any semblance of a priority?

  2. catacaustic
    Member
    Posted 1 year ago #

    The "easy" and built-in way to do that would be to set up the priority when you use add_action() to call the function that enqueue's your scripts/styles. Setting the priority there to a higher number should push your stylesheets back in the order. It may be worth looking at the plugins to see what priority they set so that you know what number you nead to be higher then.

  3. Josh Levinson
    Member
    Posted 1 year ago #

    Right, I should have mentioned in my initial post that I also recognize that the priority parameter is available as well. However, when using this for client sites, there is no way for me to guarantee that a plugin installed at any time will override my styles. I'm looking for a real solution to the problem, not just an implementation of an already available parameter that doesn't do exactly what I described.

  4. catacaustic
    Member
    Posted 1 year ago #

    That's just not possible.

    Think about it this way. You want a way to ensure that your styles/js loads last. What happens when another developer thinks the same thing, and loads their styles/js last? You can do it for yours, so that means that they can do it for theirs too, and that will still make yours not quite last which could break yours. It doesn't matter what is changed, added or used there will always be a chance that another plugin/theme will add theirs in after yours and there's no way around that.

    If it's client sites, you need to give them better training. Make sure that they understand that if they make changes, add new plugins, modify their themes, etc, that there are things that can go wrong and that they should be cautious. Then you can make sure to tell then that if they want ot do something let you know so you can do a backup of everything and guide them thorugh whatever it is so that if anything doesn't go as expected you can help. As well as making you look like you're doing the right thing, it is also a good chance to get a bit more billable time up.

  5. Josh Levinson
    Member
    Posted 1 year ago #

    That is an excellent point! I don't know how I failed to recognize that logic. However, I fail to see how that scenario would apply to theming. For plugins, it obviously wouldn't work. But one only ever has one theme active at a time.

  6. catacaustic
    Member
    Posted 1 year ago #

    It applies to plugins as well because they use the same wp_enqueue_script() and wp_enqueue_style() functions as your theme will so they will have to follow exactly the same rules. You can have your theme set up however you like, but plugins can still over-ride your themes stylesheet if they force their styles/js further down the queue then the ones in your style are.

  7. Josh Levinson
    Member
    Posted 1 year ago #

    The trouble I'm in that caused me to bring up this question was that I am using a plugin that doesn't use the add_action function to register or queue the stylesheet - it does so with a function.

  8. esmi
    Forum Moderator
    Posted 1 year ago #

    So? You can trigger that function using an add_action.

  9. catacaustic
    Member
    Posted 1 year ago #

    As esmi said, the foundation of WordPress is the add_action() function, and that calls the function that does the wp_enqueue_script() calls. if you look through the plugins code you'll find something like this:

    add_action ("wp_enqueue_scripts", "queue_function_name");

    If you look at the codes page for add_action() you'll see that the third param is the priority. If there's no priority set in the plugins call, then you can use any number that's greater then 10 (because 10 is the default) and your call will be added after the plugins call.

  10. Josh Levinson
    Member
    Posted 1 year ago #

    @esmi @catacaustic
    I appreciate the responses. I was already aware of the availability of add_action's priority parameter. I don't know why I put that the plugin used a function to call wp_enqueue_style, as *EDIT* I am the one putting the add_action in my functions.php *EDIT*. However, for some reason, using add_action with a number greater than 10 (and even extremely high numbers) does not render the style last. In fact, it consistently renders first.

    The code I am using in functions.php is this:

    function generate_css() {
      if(function_exists('wpsass_define_stylesheet'))
        wpsass_define_stylesheet("styles.scss", 'styles.css');
    }
    add_action( 'after_setup_theme', 'generate_css', 100);
  11. catacaustic
    Member
    Posted 1 year ago #

    So what is the priority that's set in the plugin that you're trying to over-write?

  12. Josh Levinson
    Member
    Posted 1 year ago #

    Updated previous post, I've been going bonkers lately. I am the one using the add_action, not the plugin.

  13. catacaustic
    Member
    Posted 1 year ago #

    But you are trying to set your scripts/CSS later then some other scripts/CSS aren't you? You mentioned Gravity Forms in your first post, so I've been assuming that you're trying to set yours up after that or something else similar all this time. I'll guarantee that the pluign that you're trying to over-write uses the same add_action call that you do. That's the way that it's done, and that's how plugin authors do it.

    Find out where the other scripts are being queued, and see what their priority is set as. In the case of Gravity Forms, they are set as:

    wp_enqueue_style("gforms_css", GFCommon::get_base_url() . "/css/forms.css", null, GFCommon::$version);

    So, they are not setting a priority which means that it (should) be set as a priority of 10 as that's the default, although they didn't get the call right (they missed the dependancies array).

  14. Josh Levinson
    Member
    Posted 1 year ago #

    I'd like to add more to this thread, as I don't view this as resolved.

    Considering that according to http://codex.wordpress.org/Theme_Review#Including_Stylesheets_and_Scripts theme's MUST enqueue their styles and not simply link them, why isn't there a theme specific (read not usable or useful by plugins) function to enqueue a theme's stylesheet, without having to worry about the priority of that function?

    I understand that I can use priority to enqueue my theme's style later down the road, but what happens when a plugin I/a client installs sets its own style priority at 50 bajillion? I have heard "well, clients shouldn't do yadayada" or "just change your theme's enqueue priority after the plugin install". Please consider that I have thousands of clients I have to deal with; this suggestion is simply not feasible.

    Sorry for all the caps and bolds :) Just trying to get my point across.

  15. catacaustic
    Member
    Posted 1 year ago #

    Like I said before. If you can do it, what's stopping the other theme/plugin devleopers from doing the same thing and still over-writing what you have done?

    Adding in a separate function to deal with stylesheets form the theme isn't needed at all. There's an extremely simple way to do that without the need ofr any extra coding - you just need to set out your header.php file the right way.

    Instead of...

    <link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo ("stylesheet_url"); ?>" />
    <?php wp_head (); ?>

    Do this...

    <?php wp_head (); ?>
    <link rel="stylesheet" type="text/css" media="all" href="<?php bloginfo ("stylesheet_url"); ?>" />

    Simple, solves all of your problems, and no need for any extra functions to confuse people.

  16. Josh Levinson
    Member
    Posted 1 year ago #

    Am I incorrect in saying that the page here: http://codex.wordpress.org/Theme_Review is a guideline for the best practices for theme coding? The section here: http://codex.wordpress.org/Theme_Review#Including_Stylesheets_and_Scripts
    states that

    Themes are required to enqueue all stylesheets and scripts, using wp_enqueue_style()/wp_enqueue_script(), and hooked into an appropriate hook via callback function, rather than hard-coding stylesheet/script links or tags in the template.

    That is exactly the opposite of what you are advising to do.

    In addition, only one theme is active at any given time. How would another theme developer overwrite my function if his theme is active? His theme being active renders my theme, and it's code (namely my desired function) unused and unparsed by WordPress.

    If this is a theme specific function (again, read - ONLY used in themes), obviously plugin authors won't (hopefully can't, though I'm not sure how to achieve this) use the function.

  17. catacaustic
    Member
    Posted 1 year ago #

    Every theme that I've seen, including the default Twenty Eleven and Twenty Twelve, set up the themes stylesheet the way that I have shown. And if you're going to quote those points, you really should have looked at the last point in that list:

    Themes may optionally link the default stylesheet (style.css) directly in the document head, or via wp_enqueue_style(). Whichever method is used, the default stylesheet must be referenced via get_stylesheet_uri()

    That spells it out plainly that it's also acceptable to do it this way - which will solve your problems.

    As much as I understand that there is only one theme active at a time, there's no feasable way to block a function from being used in a plugin. Functions can be called from anywhere so trying to block functionality something's not called from a theme is going to be extremely hard, if not impossible. That's why a theme-only function will not work, and will only add more complexity to the code base.

  18. Josh Levinson
    Member
    Posted 1 year ago #

    *Sigh* It seems I let my frustration get the better of me in my quest for the answer I wanted to hear! ;) Good find with the fine print on that page. You are correct in saying that this will solve my problems! Thank your for your attention to this thread and your good answer(s).

Topic Closed

This topic has been closed to new replies.

About this Topic