Question

I am trying to work on my child theme, but I can't get rid of the filemtime(): stat failed warning, which appeared as soon as I activated my child theme. After reading other questions, I understand that the filemtime warning is often caused by incorrect use of URLs and paths. However, I think I provided URLs and paths correctly, and if that's the case, then there is something else I'm missing.

This function is from the parent theme:

function mtt_styles()
{
    wp_enqueue_style(
    'mtt-custom-style', 
    get_stylesheet_directory_uri() . '/dist/css/custom-style.css', 
    false, 
    filemtime(get_stylesheet_directory() . '/dist/css/custom-style.css'), 
    'all'
    );
  
    wp_enqueue_style('mtt-main-style', get_stylesheet_uri());    
}
add_action('wp_enqueue_scripts', 'mtt_styles');

This is from my child theme:

function child_styles()
{
    $theme = wp_get_theme();    
    wp_enqueue_style(
        'mtt-custom-style',
        get_template_directory_uri() . '/dist/css/custom-style.css',
        array(),
        filemtime(get_template_directory() . '/dist/css/custom-style.css'),
        'all'
    );
    wp_enqueue_style(
        'mtt-main-style',
        get_template_directory_uri() . '/style.css',
        array(),
        $theme->parent()->get('Version'),
        'all'
    );
    wp_enqueue_style(
        'child-main-style', 
        get_stylesheet_uri(), 
        array('mtt-custom-style', 'mtt-main-style'));
}
add_action('wp_enqueue_scripts', 'child_styles');

When I go to the page source, I see that the enqueueing worked. All the files are there, and all the styles from the parent theme work well within the child, but the warning remains.

Could someone help me spot what I am missing or doing wrong here?

Was it helpful?

Solution

The problem is in your parent theme. The parent theme is using get_stylesheet_directory() here:

filemtime(get_stylesheet_directory() . '/dist/css/custom-style.css'),

When the parent theme is active, that's fine, because get_stylesheet_directory() will point that file in the parent theme. The problem is that when you activate a child theme, it is trying to get the filemtime() of '/dist/css/custom-style.css' in your child theme, and I'm guessing that this file doesn't exist there. Hence the failure.

The issue is that because filemtime() is run right away, it doesn't matter if you re-define the script's URL, or dequeue it, because it's already tried and failed to check the time, throwing the error.

If you're the author of the parent theme, then fixing the issue is as simple has replacing get_stylesheet_directory() with get_template_directory_uri() (or better yet, get_parent_theme_file_path()). Then the error won't occur when loading a stylesheet that's missing that file, and you won't need to re-eneueue it from that child theme.

If you're not the original author, then the right solution would be to just unhook mtt_styles() from wp_enqueue_scripts entirely. Then from the child theme just re-enqueue them using the correct path. You're already doing the latter, so you just need to do the unhooking part. The trick with that is that you'll need to unhook it from within the after_setup_theme hook, because otherwise the original hook will not have been added yet otherwise, since the child theme is loaded first:

add_action(
    'after_setup_theme',
    function()
    {
        remove_action( 'wp_enqueue_scripts', 'mtt_styles' );
    }
);

OTHER TIPS

There are 2 major problems here:

Problem 1: get_template__.... and get_stylehsheet_... are not the same

filemtime(get_template_directory() . '/dist/css/custom-style.css'), means get the file modified time of the parent themes dist/css/custom-style.css file.

I suspect, there is no such file in the parent theme because it exists only in the child theme, so PHP fires off a warning that you asked for the file modified time of a file that does not exist.

get_template_directory and get_stylesheet_directory do not do the same thing.

  • get_template_directory is the parent theme ( current if not using a child theme
  • get_stylesheet_directory is the current theme, aka the child theme

The same goes for all the other functions starting with get_template_... and get_stylesheet_...

Problem 2: A Misunderstanding of functions.php in child and parent themes, and how child themes work.

If you use a child theme, the parent themes functions.php still runs. Child themes don't let you override any file by putting a new file with the same name in the child theme. That only works with templates loaded via get_template_part.

A PHP file in a child theme, or a JS/CSS file in a child theme does not override the same file in the parent theme.

So if you enqueue a CSS file in the parent theme, then copy the functions.php to the child theme and change it, it has not overriden the parent. Instead:

  • both functions.php files run
  • all the duplicated code now runs twice
  • the original JS is enqueued, as well as the new JS file you tried to overwrite it with
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top