Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if( ! post_type_exists( $this->post_type_name ) )
- {
- add_action( 'init', array( &$this, 'register_post_type' ) );
- }
- var_dump(post_type_exists( $this->post_type_name ));
- require_once plugin_dir_path( __FILE__ ) . 'admin/class-custom-post-type.php';
- $location = new LegendPluginWPClassesCustomPostType('Location');
- $project = new LegendPluginWPClassesCustomPostType('Project');
- $project->add_taxomony('Type');
- $project->add_taxomony('Sector');
- namespace LegendPluginWPClasses;
- class CustomPostType {
- private $post_type_name;
- private $args;
- private $labels;
- private $textDomain;
- public function __construct( $name, $args = array(), $labels = array() )
- {
- $this->post_type_name = strtolower( str_replace( ' ', '_', $name ) );
- $this->args = $args;
- $this->labels = $labels;
- // Add action to register the post type, if the post type does not already exist
- var_dump(post_type_exists( $this->post_type_name ));
- if( ! post_type_exists( $this->post_type_name ) )
- {
- add_action( 'init', array( &$this, 'register_post_type' ) );
- }
- }
- public function register_post_type()
- {
- $post_type_name = ucwords($this->post_type_name);
- $post_type_name_plural = $post_type_name . 's';
- $labels = array_merge(
- array(
- 'name' => _x( $post_type_name_plural, 'post type general name' ),
- 'singular_name' => _x( $post_type_name, 'post type singular name' ),
- 'add_new' => _x( 'Add New', strtolower( $post_type_name ) ),
- 'add_new_item' => __( 'Add New ' . $post_type_name ),
- 'edit_item' => __( 'Edit ' . $post_type_name ),
- 'new_item' => __( 'New ' . $post_type_name ),
- 'all_items' => __( 'All ' . $post_type_name_plural ),
- 'view_item' => __( 'View ' . $post_type_name ),
- 'search_items' => __( 'Search ' . $post_type_name_plural ),
- 'not_found' => __( 'No ' . strtolower( $post_type_name_plural ) . ' found'),
- 'not_found_in_trash' => __( 'No ' . strtolower( $post_type_name_plural ) . ' found in Trash'),
- 'parent_item_colon' => '',
- 'menu_name' => $post_type_name_plural
- ),
- $this->labels
- );
- $args = array_merge(
- array(
- 'label' => $post_type_name,
- 'labels' => $labels,
- 'public' => true,
- 'show_ui' => true,
- 'supports' => array( 'title', 'editor' ),
- 'show_in_nav_menus' => true,
- '_builtin' => false,
- ),
- $this->args
- );
- // Register the post type
- register_post_type( $this->post_type_name, $args );
- // Flus the rewrite rules to prevent problems
- flush_rewrite_rules(false);
- }
- public function add_taxomony( $name, $args = array(), $labels = array() )
- {
- if( ! empty( $name ) )
- {
- $post_type_name = $this->post_type_name;
- // Taxonomy properties
- $taxonomy_name = strtolower( str_replace( ' ', '_', $name ) );
- $taxonomy_labels = $labels;
- $taxonomy_args = $args;
- }
- if( ! taxonomy_exists( $taxonomy_name ) )
- {
- $name = ucwords( str_replace( '_', ' ', $name ) );
- $plural = $name . 's';
- $labels = array_merge(
- array(
- 'name' => _x( $plural, 'taxonomy general name' ),
- 'singular_name' => _x( $name, 'taxonomy singular name' ),
- 'search_items' => __( 'Search ' . $plural ),
- 'all_items' => __( 'All ' . $plural ),
- 'parent_item' => __( 'Parent ' . $name ),
- 'parent_item_colon' => __( 'Parent ' . $name . ':' ),
- 'edit_item' => __( 'Edit ' . $name ),
- 'update_item' => __( 'Update ' . $name ),
- 'add_new_item' => __( 'Add New ' . $name ),
- 'new_item_name' => __( 'New ' . $name . ' Name' ),
- 'menu_name' => __( $plural ),
- ),
- $taxonomy_labels
- );
- $args = array_merge(
- array(
- 'label' => $plural,
- 'labels' => $labels,
- 'public' => true,
- 'show_ui' => true,
- 'show_in_nav_menus' => true,
- '_builtin' => false,
- ),
- $taxonomy_args
- );
- // Add the taxonomy to the post type
- add_action( 'init', function() use( $taxonomy_name, $post_type_name, $args ) {
- register_taxonomy( $taxonomy_name, $post_type_name, $args );
- // Flus the rewrite rules to prevent problems
- flush_rewrite_rules(false);
- }
- );
- }
- else
- {
- // Taxonomy is already registered
- // just attach it to the post type
- add_action( 'init', function() use( $taxonomy_name, $post_type_name ) {
- register_taxonomy_for_object_type( $taxonomy_name, $post_type_name );
- // Flus the rewrite rules to prevent problems
- flush_rewrite_rules(false);
- }
- );
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement