Can’t extend some core classes

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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.

Method 2

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.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x