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

functions - require get_template_directory() . 'pathto-myfile.php' BREAKS customize > themes functionalit

programmeradmin1浏览0评论

hope to get some help, please <3

i'm building a theme from scratch as an exercise to learn WP and PHP, using WP commercial themes (GeneratePress and WP default themes) as a guide.

when i use:

$theme_dir = get_template_directory();
require $theme_dir . '/inc/structure/header.php';

all the themes disappear when i click 'customize > change' to change themes and it displays 'showing "0" themes' in right upper corner. iow, the theme thumbnails aren't loading or otherwise visible. when i comment out the require stmt, it returns to normal.

this is exactly how other professional, commercial themes do it, and i've even copy/pasted the code but it still happens.

the file content from the required file is included but also shows on the admin as well as other pages and flashes before the rest of the page loads.

i've got basic theme support in functions.php and i'm hooking into wp_head for the viewport meta, pingbacks_url, and skip-to-content link as well as wp_enqueue_scripts for my styles.

i just started, so that's basically all i have in my theme so far.

i'm not advanced enough to make sense of this but here's my debug.php:

[16-Dec-2020 20:44:46 UTC] PHP Stack trace:
[16-Dec-2020 20:44:46 UTC] PHP   1. {main}() /app/public/wp-admin/admin-ajax.php:0
[16-Dec-2020 20:44:46 UTC] PHP   2. require_once() /app/public/wp-admin/admin-ajax.php:22
[16-Dec-2020 20:44:46 UTC] PHP   3. require_once() /app/public/wp-load.php:37
[16-Dec-2020 20:44:46 UTC] PHP   4. require_once() /app/public/wp-config.php:82
[16-Dec-2020 20:44:46 UTC] PHP   5. include() /app/public/wp-settings.php:525
[16-Dec-2020 20:44:47 UTC] PHP Stack trace:
[16-Dec-2020 20:44:47 UTC] PHP   1. {main}() /app/public/index.php:0
[16-Dec-2020 20:44:47 UTC] PHP   2. require() /app/public/index.php:17
[16-Dec-2020 20:44:47 UTC] PHP   3. require_once() /app/public/wp-blog-header.php:13
[16-Dec-2020 20:44:47 UTC] PHP   4. require_once() /app/public/wp-load.php:37
[16-Dec-2020 20:44:47 UTC] PHP   5. require_once() /app/public/wp-config.php:82
[16-Dec-2020 20:44:47 UTC] PHP   6. include() /app/public/wp-settings.php:525

i get these inspector console errors regardless of whether the require is there or not:

Unknown property ‘speak’.  Declaration dropped. dashicons.css:27:8
Unknown property ‘speak’.  Declaration dropped. admin-bar.css:231:8
Unknown property ‘speak’.  Declaration dropped. admin-bar.css:308:8
Unknown property ‘speak’.  Declaration dropped. admin-bar.css:508:8
Unknown property ‘speak’.  Declaration dropped. admin-bar.css:614:8
Unknown property ‘speak’.  Declaration dropped. admin-bar.css:844:9
Error in parsing value for ‘min-width’.  Declaration dropped. admin-bar.css:971:14
Error in parsing value for ‘min-width’.  Declaration dropped.

i'm baffled. thanks for any help!

hope to get some help, please <3

i'm building a theme from scratch as an exercise to learn WP and PHP, using WP commercial themes (GeneratePress and WP default themes) as a guide.

when i use:

$theme_dir = get_template_directory();
require $theme_dir . '/inc/structure/header.php';

all the themes disappear when i click 'customize > change' to change themes and it displays 'showing "0" themes' in right upper corner. iow, the theme thumbnails aren't loading or otherwise visible. when i comment out the require stmt, it returns to normal.

this is exactly how other professional, commercial themes do it, and i've even copy/pasted the code but it still happens.

the file content from the required file is included but also shows on the admin as well as other pages and flashes before the rest of the page loads.

i've got basic theme support in functions.php and i'm hooking into wp_head for the viewport meta, pingbacks_url, and skip-to-content link as well as wp_enqueue_scripts for my styles.

i just started, so that's basically all i have in my theme so far.

i'm not advanced enough to make sense of this but here's my debug.php:

[16-Dec-2020 20:44:46 UTC] PHP Stack trace:
[16-Dec-2020 20:44:46 UTC] PHP   1. {main}() /app/public/wp-admin/admin-ajax.php:0
[16-Dec-2020 20:44:46 UTC] PHP   2. require_once() /app/public/wp-admin/admin-ajax.php:22
[16-Dec-2020 20:44:46 UTC] PHP   3. require_once() /app/public/wp-load.php:37
[16-Dec-2020 20:44:46 UTC] PHP   4. require_once() /app/public/wp-config.php:82
[16-Dec-2020 20:44:46 UTC] PHP   5. include() /app/public/wp-settings.php:525
[16-Dec-2020 20:44:47 UTC] PHP Stack trace:
[16-Dec-2020 20:44:47 UTC] PHP   1. {main}() /app/public/index.php:0
[16-Dec-2020 20:44:47 UTC] PHP   2. require() /app/public/index.php:17
[16-Dec-2020 20:44:47 UTC] PHP   3. require_once() /app/public/wp-blog-header.php:13
[16-Dec-2020 20:44:47 UTC] PHP   4. require_once() /app/public/wp-load.php:37
[16-Dec-2020 20:44:47 UTC] PHP   5. require_once() /app/public/wp-config.php:82
[16-Dec-2020 20:44:47 UTC] PHP   6. include() /app/public/wp-settings.php:525

i get these inspector console errors regardless of whether the require is there or not:

Unknown property ‘speak’.  Declaration dropped. dashicons.css:27:8
Unknown property ‘speak’.  Declaration dropped. admin-bar.css:231:8
Unknown property ‘speak’.  Declaration dropped. admin-bar.css:308:8
Unknown property ‘speak’.  Declaration dropped. admin-bar.css:508:8
Unknown property ‘speak’.  Declaration dropped. admin-bar.css:614:8
Unknown property ‘speak’.  Declaration dropped. admin-bar.css:844:9
Error in parsing value for ‘min-width’.  Declaration dropped. admin-bar.css:971:14
Error in parsing value for ‘min-width’.  Declaration dropped.

i'm baffled. thanks for any help!

Share Improve this question asked Dec 16, 2020 at 21:34 sprouticussprouticus 11 bronze badge 5
  • The inspector console errors are errors in your CSS so won't be relevant, no. – Rup Commented Dec 16, 2020 at 22:16
  • The require looks OK. That file definitely exists does it, at that path? Is it specifically that require that's failing? I don't see that in your call stack output, or a specific error message for it. – Rup Commented Dec 16, 2020 at 22:18
  • But if that's actual content you want to render into the page then you don't want to be requiring it like that from the top level of your theme file: maybe from inside a function that you then call from one of your page templates, probably header.php? Can you be more specific about where you've put those two lines please? If you are rendering out from the theme's functions I could imagine that might break the theme customisation pages, yes. – Rup Commented Dec 16, 2020 at 22:19
  • thx @Rup, the actual path is theme-name/inc/structure/header.php, exactly as it exists in GeneratePress. – sprouticus Commented Dec 17, 2020 at 1:43
  • @Rup, i'm not familiar w debug.log but the require stmt does not appear to be associated with any errors, i'd agree. – sprouticus Commented Dec 17, 2020 at 1:49
Add a comment  | 

1 Answer 1

Reset to default 0

thanks again @Rup for sparking the path to this answer!

rendering out directly from the required file was at least part of the problem.

i believe the other part was not rendering from inside a function as well as not hooking all of that functionality into an action in the target file.

the problem vanishes and the content renders properly when these steps are followed:

require the file in functions.php using built-in WordPress function to the current theme's template directory as indicated above.

create a simple action hook in the target file using do_action() function:

do_action( 'namespace_hook_name' );

create a (pluggable, if necessary) function in required file to render desired content (below is based on GeneratePress formatting). $priority and $accepted_args optional:

if ( ! function_exists( 'namespace_func_name' ) ) { add_action( 'namespace_hook_name', 'namespace_func_name', $priority, $args ); function namespace_func_name() { // code to render } }

sorry about the code formatting.. still new here <3

search commercial themes for how they use action hooks as well as WordPress's own default themes to learn more and practice.

https://developer.wordpress/reference/functions/do_action/

https://developer.wordpress/reference/functions/add_action/

发布评论

评论列表(0)

  1. 暂无评论