user1406440
user1406440

Reputation: 1645

Remove parent theme enqueued files in Wordpress child theme

I'm trying to make a child theme off the html5blank theme. I've managed to get my child theme CSS working using the following:

<?php
    add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
    function my_theme_enqueue_styles() {
        wp_enqueue_style( 'main-css', get_stylesheet_directory_uri() . '/css/main.css' );
    }
 ?>

But the styles from the parent are still inherited. These are style.css and normalize.css. I have no need for these files at all.

So I've been told I need to use wp_dequeue_style(). to "remove parent enqueued files with wp_dequeue_style()" and add your hook with a later priority than the default 10. This is a third, optional parameter for add_action(). Something like 100.

Unfortunately I'm really struggling with this basic task, I've tried but I can't get it working. I tried the following but obviously it's totally wrong!

<?php
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
function my_theme_enqueue_styles() {
    wp_enqueue_style( 'main-css', get_stylesheet_directory_uri() . '/css/main.css' );
    wp_dequeue_style( 'parent-style-css', get_template_directory_uri() . '/style.css', 100 );
    wp_dequeue_style( 'parent-normalize-css', get_template_directory_uri() . '/normalize.css', 100 );
}
?>

Can anyone help with this? I'm sure it's straight forward but I don't have a clue!

Also, I'm guessing if I don't need the JS from the parent either as I'll use that in my child them, I guess I'd use the methods to enqueue/dequeue them?

EDIT

Is this more like it...

<?php

add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles', 100 );
function my_theme_enqueue_styles() {
    wp_enqueue_style( 'main-css', get_stylesheet_directory_uri() . '/css/main.css' );
    wp_dequeue_style( 'normalize');
    wp_dequeue_style( 'html5blank');
}

add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_scripts', 100 );
function my_theme_enqueue_scripts() {

    wp_deregister_script( 'conditionizr');
    wp_deregister_script( 'modernizr');
    wp_deregister_script( 'html5blankscripts');

    wp_register_script('jquery', get_template_directory_uri() . '/js/jquery-1.12.0.min.js', array('jquery'), '1.12.0'); // Custom scripts
    wp_enqueue_script('jquery'); // Enqueue it!

    wp_register_script('responsive-nav', get_template_directory_uri() . '/js/responsive-nav.js', array('jquery'), '1.0.0'); // Custom scripts
    wp_enqueue_script('responsive-nav'); // Enqueue it!

    wp_register_script('uniform-js', get_template_directory_uri() . '/js/jquery.uniform.min.js', array('jquery'), '1.0.0'); // Custom scripts
    wp_enqueue_script('uniform-js'); // Enqueue it!

    wp_register_script('main-js', get_template_directory_uri() . '/js/main.js', array('jquery'), '1.0.0'); // Custom scripts
    wp_enqueue_script('main-js'); // Enqueue it!
}

?>

Upvotes: 2

Views: 6634

Answers (1)

BA_Webimax
BA_Webimax

Reputation: 2679

The instructions were simply telling you to change this...

add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );

into this...

add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles', 100 );

The full method call is...

add_action( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )

Only the first two are required, the others have defaults they will use in case you leave them blank.

Also, to dequeue, you only need the name of the parent identifier, not all of the file details.

wp_dequeue_style( 'name-of-identifier-in-parent-theme' );

It looks like you put the priority on the wrong function call.

And Yes, you can dequeue javascript files as well using this method.

Upvotes: 2

Related Questions