Skip to content

Mywp Post Type Extensions (class)

Extends Post Type Example

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 Hook
  • mywp_post_type_after_setup_theme_include_modules For Theme Developer Hook
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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

1
2
3
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.
1
2
3
4
5
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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

  1. Download and Unzip the ZIP file.
  2. Upload the mywp-extend-post-type-example dir in mywp_post-type_module_example--master to your WordPress plugins dir.
  3. Activate the My WP Post Type Extends Example.

This Post Has 0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top
Close mobile menu