最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Child theme functions.php not executing

programmeradmin2浏览0评论

I have the following in a child theme's functions.php file:

<?php

function theme_child_add_scripts() {
    wp_register_script(
        'script',
        get_stylesheet_directory_uri() . '/js/script.js',
        array( 'jquery' ),
        null,
        false
    );
    wp_enqueue_script( 'script' )
}
add_action('wp_enqueue_scripts', 'theme_child_add_scripts');

script.js isn't included on the page and there's no network request going out to get it. What could be going wrong? It's unclear whether the child theme's functions.php file is even being executed.

edit: It seems that the functions.php file is not being executed at all, because I put a die('foo') at the top of the file and the page loaded normally. Why would this be happening?

In styles.css:

/*
Theme Name: Theme-child
Template: Theme
*/

I have the following in a child theme's functions.php file:

<?php

function theme_child_add_scripts() {
    wp_register_script(
        'script',
        get_stylesheet_directory_uri() . '/js/script.js',
        array( 'jquery' ),
        null,
        false
    );
    wp_enqueue_script( 'script' )
}
add_action('wp_enqueue_scripts', 'theme_child_add_scripts');

script.js isn't included on the page and there's no network request going out to get it. What could be going wrong? It's unclear whether the child theme's functions.php file is even being executed.

edit: It seems that the functions.php file is not being executed at all, because I put a die('foo') at the top of the file and the page loaded normally. Why would this be happening?

In styles.css:

/*
Theme Name: Theme-child
Template: Theme
*/
Share Improve this question edited Jan 21, 2016 at 17:29 twsmith asked Jan 20, 2016 at 21:52 twsmithtwsmith 1311 silver badge6 bronze badges 6
  • 1 The parent them is correctly defined in the child's style.css? – vancoder Commented Jan 20, 2016 at 23:19
  • When the template in the child's style.css is Theme-child, the functions.php file is found and executed. However, this displays an error in the admin panel that the parent theme could not be found. – twsmith Commented Jan 21, 2016 at 17:12
  • Template should match the directory name of the parent. That's usually lowercase. – fuxia Commented Jan 21, 2016 at 17:17
  • It's uppercase in this scenario--just double-checked to confirm. – twsmith Commented Jan 21, 2016 at 17:29
  • Is your child theme Activated in the back end? – Sterling Hamilton Commented Jan 21, 2016 at 17:35
 |  Show 1 more comment

2 Answers 2

Reset to default 1

Our problem was that our style.css file was in a css folder inside the child theme directory, not at the root of the child theme. When we placed a style.css file at the root and included the comment block with theme name and template it picked up the functions.php file as expected.

I had a problem with a code that executed perfectly in the theme functions.php but not in the child theme's (that's how I found this page). I found a non-tech solution, using the free plug in Code Snippets the code executed perfectly. I was looking for a php solution, but this has saved me a bit of time.

发布评论

评论列表(0)

  1. 暂无评论