-
Notifications
You must be signed in to change notification settings - Fork 12
Custom objects
To overwrite the default Clarkson_Object create wordpress-objects/post.php or any other internal post type like page or attachment.
To overwrite the default Clarkson_Term create wordpress-objects/category.php or any other internal taxonomy like post_tag.
To overwrite the default Clarkson_User create wordpress-objects/user.php.
Clarkson Core autoloads all registered Custom Post Types, Taxonomies and Users from the wordpress-objects directory within your active theme.
Clarkon Core is _Child Theme_compatible. So when you have a post.php in both your parent theme and your child theme, the child theme class gets loaded instead of the parent one. 🎉
You can create a Base class which the Post class Extends from, but you should load this class manually.
It’s possible to extend the Clarkson Object to your own Custom Post Type by creating classes that represent the Custom Post Types.
- Register your CPT Just register your CPT like you always do via:
<?php register_post_type( 'll-company', $args );
-
Add your custom class Add a custom WordPress object class to the
wordpress-objectsdirectory within your current active theme viaclarkson-theme/wordpress-objects/ll-company.php:
class ll_company extends Clarkson_Post {
public function get_website() {
return get_post_meta( $this->get_id(), 'll-website' );
}
}
When you register a Custom Post Type ll_company your custom class ll_company gets loaded in the objects variable within the loop. For example: When visiting the archive of this CPT like archive-company.twig, all the posts are of the class ll_company.
In the same way as you extend a CPT, you can register a Custom Taxonomy, create a class for it and extend the basic Clarkson_Term class.
This means when you have a Custom Taxonomy named company-category:
register_taxonomy( 'll-company-category', 'll-company', $args );
a custom WordPress Term:
class ll_company_category extends Clarkson_Term {
// custom code
}
Which will result in some magic so that when you want to retrieve all terms via $company->get_terms('ll-company-category') an array of terms based on the class ll_company_category will be returned:
$company = new ll_company( $post );
$company_categories = $company->get_terms('ll-company-category');
When using a custom template Clarkson Core automatically loads a corresponding custom WordPress Object. So if your template is called template-headquarter.twig then within it will load wordpress-objects/template_headquarter.php into the object variable instead of the default Clarkson Object.
class template_headquarter extends Clarkson_Object {
}
When Clarkson retrieves an User object, it will check if there is a custom WordPress Object available based on the current user_role of the current User. So if you are logged in as an Administrator it will use the wordpress-objects/user_administrator.php class when calling object->get_author();.
class user_administrator extends Clarkson_User {
}
The loading order of which object Class Clarkson will use is as following, where a higher number will overrule a lower number.
Custom Post Types:
- Default
Clarkson_Object. - Custom
wordpress-object/page.phpor an other Custom Post Type. - Custom
wordpress-object/template_headquarter.php.
Users:
- Default
User_Object. - Custom
wordpress-object/user_administrator.phpor an other User role name.
Introduction
Documentation