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

php - Can't extend some core classes

programmeradmin0浏览0评论

I am relatively new to wordpress development, I am trying to extend 'Walker_Category_Checklist' class found in /wp-admin/includes/class-walker-category-checklist.php to customize it for wp_terms_checklist() or wp_category_checklist() but when I try to require_once a link to my extended class in the child-theme function.php I get this error and site not loading!

x" Fatal error: Class 'Walker_Category_Checklist' not found in ..... "

Is it because of this class file in 'wp-admin/includes/' dir? if so then how to extend it ?

wordpress ver: 5.6 - php ver: 7.2

Thanks in advance Cheers Mo.

I am relatively new to wordpress development, I am trying to extend 'Walker_Category_Checklist' class found in /wp-admin/includes/class-walker-category-checklist.php to customize it for wp_terms_checklist() or wp_category_checklist() but when I try to require_once a link to my extended class in the child-theme function.php I get this error and site not loading!

x" Fatal error: Class 'Walker_Category_Checklist' not found in ..... "

Is it because of this class file in 'wp-admin/includes/' dir? if so then how to extend it ?

wordpress ver: 5.6 - php ver: 7.2

Thanks in advance Cheers Mo.

Share Improve this question edited Jan 25, 2021 at 23:30 Tom J Nowell 61k7 gold badges79 silver badges148 bronze badges asked Jan 25, 2021 at 22:11 MoSaberMoSaber 113 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

You can extend it, but the class definition and any code that uses it, can't run outside of the admin area.

Wrap your class in this conditional:

if ( is_admin() ) {
    ...
}

If you are trying to use this on the frontend or other non-Admin areas, do not do that.

Generally, classes WordPress uses should not be extended, walker classes and WP_Widget are the notable extensions.

Also, it's good practice to put classes in their own files.

I could not extend directly Walker_Category_Checklist I think because it's in wp-admin dir. even it's for "output an unordered list of category checkbox"!! similar classes to it etc. "Walker_Nav_Menu" existing in wp-include witch is should be the right place for the "Walker_Category_Checklist" I guess.. The answer that I found is going around by extending the parent class "Walker" to "my_Walker_Category_Checklist" then copy the code from "Walker_Category_Checklist" then do my customization on it, like that everything if working.

发布评论

评论列表(0)

  1. 暂无评论