Description
You can extend the your Post Type.
Structure
My WP Post Type the following.
- MywpPostType my-wp/post-type/class.post-type.php My WP Post Type API.
- MywpPostTypeInit my-wp/post-type/post-type.init.php Register post types on WordPress already setting data of modules.
- MywpPostTypeAbstractModule my-wp/post-type/abstract.post-type.module.php A class to help register/action the post type. You can extend this.
How to extend post type
Include extend file
Include the your extend class PHP file with add filter.
mywp_post_type_plugins_loaded_include_modules
For Plugin Developer Hookmywp_post_type_after_setup_theme_include_modules
For Theme Developer Hook
add_filter( "mywp_post_type_plugins_loaded_include_modules" , "example_post_type_include_module" ); function example_post_type_include_module( $includes ) { // Your include path $dir = dirname( __FILE__ ) . "/"; // Your include extend class file name $file = $dir . "mywp.post-type.module.example.php"; $includes["example_post_type_include"] = $file; return $includes; }
Create the PHP Class extend file.
Create a PHP file, and define the class your name with extend abstract class “MywpPostTypeAbstractModule“.
e.g.) mywp.post-type.module.example.php
class MywpPostTypeModuleExample extends MywpPostTypeAbstractModule { }
Properties of MywpPostTypeAbstractModule.
MywpPostTypeAbstractModule the following properties.
- $id require protected static $id is uniqu name and this value is post type name.
class MywpPostTypeModuleExample extends MywpPostTypeAbstractModule { protected static $id = "mywp_example"; }
Methods of MywpPostTypeAbstractModule.
MywpPostTypeAbstractModule the following methods.
- mywp_post_types( $post_types ) recommended not override public static This method is setting the post types with register post type method.
- $post_types (array) Post types in the middle of addition.
- get_regist_post_type_args() protected static This method is setting the register post type args.
- current_mywp_post_type_get_post( $post ) public static This method is extend the get post with custom if you use the MywpPostType class to get post/posts method().
- $post (object)
- current_manage_posts_columns( $posts_columns ) public static This method is extend the custom post type columns table header of admin panel.
- $posts_columns (array)
- current_manage_posts_custom_column( $column_name , $post_id ) public static This method is extend the custom post type columns table body(contents) of admin panel.
- $column_name (string)
- $post_id (int)
- current_edit_per_page( $per_page ) public static This method is extend the show per post types on 1 page of admin panel.
- $per_page (int)
class MywpPostTypeModuleExample extends MywpPostTypeAbstractModule { protected static function get_regist_post_type_args() { $args = array( "label" => "My WP Example Post Type", "public" => true, "show_ui" => true, "supports" => array( "title" , "page-attributes" , "custom-fields" ), ); return $args; } public static function current_mywp_post_type_get_post( $post ) { $post_id = $post->ID; $post->example_field = MywpPostType::get_post_meta( $post_id , "example_field" ); return $post; } public static function current_manage_posts_columns( $posts_columns ) { $old_columns = $posts_columns; $posts_columns = array(); $posts_columns["cb"] = $old_columns["cb"]; $posts_columns["id"] = "ID"; $posts_columns["title"] = $old_columns["title"]; $posts_columns["field"] = "Field"; return $posts_columns; } public static function current_manage_posts_custom_column( $column_name , $post_id ) { $mywp_post = MywpPostType::get_post( $post_id ); if( empty( $mywp_post ) ) { return false; } if( $column_name == "field" ) { echo $mywp_post->example_field; } } public static function current_edit_per_page( $per_page ) { $per_page = 1; return $per_page; } }
Download the example
You can use the example download file and activate the plugin after add plugin of admin plugins panel.
Git Hub: https://github.com/gqevu6bsiz/mywp_extend_post-type_module_example
- Download and Unzip the ZIP file.
- Upload the
mywp-extend-post-type-example
dir inmywp_post-type_module_example--master
to your WordPress plugins dir. - Activate the
My WP Post Type Extends Example
.
This Post Has 0 Comments