WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Replace a stylesheet via function.php in child theme (5 posts)

  1. dreamingof8a
    Member
    Posted 2 years ago #

    Hi fellow coders,

    In the parent theme, a stylesheet is loaded in function.php via
    wp_enqueue_style( 'layout', 'stylesheets/layout.css' );

    I would like to replace this file with my own layout.css in child theme folder (I already tried simply adding the relevant styles to my child's style.css but this didn't do anything).

    My approach is:

    function child_enqueue_style() {
         wp_dequeue_style( 'layout' );
         wp_enqueue_style( 'layout_child', 'stylesheets/layout.css' );
    }
    add_action( 'init', 'child_enqueue_style', 11 );

    Note that the second layout.css is the one in a sub directory of my child theme.

    However it is still only loading the original layout.css.

    Any idea what to do?
    Thanks!

  2. s_ha_dum
    Member
    Posted 2 years ago #

    Never tried it but you will have to dequeue after the parent stylesheet has been enqueued, not before. It looks like you are dequeueing before the parent stylesheet is enqueued, which as you noted does nothing.

    Is the parent stylesheet loaded on a hook, like you are doing, or is the function raw in functions.php? If it is on a hook, try hooking to the same hook, and mess with the 'priority' value if you have to. If it isn't on a hook you will have to find a hook that will work, maybe after_setup_theme

  3. dreamingof8a
    Member
    Posted 2 years ago #

    The original enque is inside a function, which is then call via
    add_action( 'wp_enqueue_scripts', 'function').

    I tried your suggestion using àfter_setup_theme` but no luck.

    Do you know of any list where I can see the order of all actions? Can't seem to find it in the codex ...

  4. dreamingof8a
    Member
    Posted 2 years ago #

    Okay I guess it was easy in the end - just used the same hook wp_enqueue_scripts - that seems to do it. Thanks for pointing me in the right direction!

  5. s_ha_dum
    Member
    Posted 2 years ago #

    The original enque is inside a function...

    But is that function loaded on a hook? That is the important part.

    I used to know of a list of hooks in order but I haven't been able to find it in quite awhile. Yes, it would be a very nice thing to have. There is a plugin called WordPress Hook Sniffer.

Topic Closed

This topic has been closed to new replies.

About this Topic