• Resolved tempcode

    (@tempcode)


    Hi all,

    I’ve trying to create a custom post type in my plugin code, but when I’ve activate my plugin the new post type appear in the admin menu and when I click on it I see a “Invalid post type” page and, when I come back to admin interface, the link to my new custom post type is disappear.

    My code is:

    ...
     add_action( 'init', array( $class_instance, 'to_custom_page' ));
    ...

    “Class instance” method called by do_action is:

    public function to_custom_page(){
      //create page for plugin
      $post_id= get_option($this->page_id, -1);
      if($post_id == -1){
        //register post type to hide page
        /*$res= register_post_type( 'carriergem_page',
          array(
    	'label' => __( 'CGL Pages' ),
    	'public' => true,
           )
         );*/
        $res = register_post_type('carriergem_cpt', array(
          'labels' => array(
    	'name' => 'CGL Pagine',
    	'singular_name' => 'CGL Pagina',
    	'add_new' => 'Aggiungi nuova',
    	'add_new_item' => 'Aggiungi nuova pagina',
    	'edit_item' => 'Modifica pagina',
    	'new_item' => 'Nuova pagina',
    	'view_item' => 'Visualizza pagina',
    	'search_items' => 'Cerca pagine',
    	'not_found' => 'Nessun pagina trovata.',
    	'not_found_in_trash' => 'Nessuna pagina presente nel cestino.',
    	'parent_item_colon' => 'Pagina genitore:',
    	'all_items' => 'Tutte le pagine',
    	'archives' => 'Archivi pagine',
    	'insert_into_item' => 'Inserisci nella pagina',
    	'uploaded_to_this_item' => 'Caricato in questa pagina',
    	'featured_image' => 'Immagine in evidenza',
    	'set_featured_image' => 'Imposta l\'immagine in evidenza',
            'remove_featured_image' => 'Rimuovi immagine in evidenza',
            'use_featured_image' => 'Utilizza come immagine in evidenza',
            'filter_items_list' => 'Filtra elenco pagine',
            'items_list_navigation' => 'Navigazione elenco pagine',
            'items_list' => 'Elenco delle pagine',
            'menu_name' => 'CGL Pagine',
            'name_admin_bar' => 'CGL Pagina',
          ),
          'description' => '',
          'public' => true,
          'hierarchical' => true,
          'exclude_from_search' => true,
          'publicly_queryable' => true,
          'show_ui' => true,
          'show_in_menu' => true,
          'show_in_nav_menus' => true,
          'show_in_admin_bar' => true,
          'menu_position' => 21,
          'capability_type' => array('page','pages'),
          'map_meta_cap' => true,
          'taxonomies' => array
               (
               ),
          'can_export' => true,
          'delete_with_user' => true,
          //'_builtin' => true,
          //'name' => 'cgl_cpt',
          'label' => 'CGL Pagine',
        )
        );
        //register page
        $post = array(
          'comment_status' => 'closed',
          'ping_status' =>  'closed' ,
          'post_author' => 1,
          'post_date' => date('Y-m-d H:i:s'),
          'post_name' => 'plugin-post',
          'post_status' => 'publish' ,
          'post_title' => '',
          'post_type' => 'carriergem_cpt',//'page',
        );
        $post_id= wp_insert_post( $post );
        add_option($this->page_id, $post_id);
      }
    
      add_rewrite_rule(..., 'top');
      flush_rewrite_rules();
    }

    I’ve Magic Filed plugin active. This plugin do the same thing in the simple way but I need to create my custom post type by my plugin code. Can Magic Field have a role in this issue?

    I’m not english and my english is really bad. Please be patience šŸ˜€ .

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter tempcode

    (@tempcode)

    I’ve created a clean installation of wordpress 4.5.3 and in the new installation the issue is come again…please help me!

    I can’t understand where my code fail or where it create this issue.

    Thread Starter tempcode

    (@tempcode)

    OK, I’ve done another test and I’ve seen that the problem is not in custom post type creation (now I’ve fixed and improved the code of custom post type creation) but the issue is in the post type insertion.

    I’ve set the wp_error parameter to true but the wp_insert_post doesn’t give me a feedback about any anomalies.

    Where I do it wrong?

    Thread Starter tempcode

    (@tempcode)

    this is my last custom post type definition:

    public function create_custom_post_type(){
     $status = get_option($this->status, -1);
     $post_id = get_option ( $this->page_id, - 1 );
     if ($post_id == - 1 && $status == -1) {
      $labels = array(
      'name' => _x( 'CGL', 'Post Type General Name', $this->plugin_name ),
      'singular_name' => _x( 'CGL', 'Post Type Singular Name', $this->plugin_name ),
      'menu_name' => __( 'CGL', $this->plugin_name ),
      'name_admin_bar'        => __( 'CGL', $this->plugin_name ),
      'archives'  => __( 'Item Archives', $this->plugin_name ),
      'parent_item_colon'     => __( 'Parent Item:', $this->plugin_name ),
      'all_items' => __( 'All Items', $this->plugin_name ),
      'add_new_item'          => __( 'Add New Item', $this->plugin_name ),
      'add_new'   => __( 'Add New', $this->plugin_name ),
      'new_item'  => __( 'New Item', $this->plugin_name ),
      'edit_item' => __( 'Edit Item', $this->plugin_name ),
      'update_item'           => __( 'Update Item', $this->plugin_name ),
      'view_item' => __( 'View Item', $this->plugin_name ),
      'search_items'          => __( 'Search Item', $this->plugin_name ),
      'not_found' => __( 'Not found', $this->plugin_name ),
      'not_found_in_trash' => __( 'Not found in Trash', $this->plugin_name ),
      'featured_image' => __( 'Featured Image', $this->plugin_name ),
      'set_featured_image' => __( 'Set featured image', $this->plugin_name ),
      'remove_featured_image' => __( 'Remove featured image', $this->plugin_name ),
      'use_featured_image' => __( 'Use as featured image', $this->plugin_name ),
      'insert_into_item' => __( 'Insert into item', $this->plugin_name ),
      'uploaded_to_this_item' => __( 'Uploaded to this item', $this->plugin_name ),
      'items_list' => __( 'Items list', $this->plugin_name ),
      'items_list_navigation' => __( 'Items list navigation', $this->plugin_name ),
      'filter_items_list' => __( 'Filter items list', $this->plugin_name ),
    			);
      $args = array(
      'label'     => __( 'CGL', $this->plugin_name ),
      'description'           => __( '', $this->plugin_name ),
      'labels'    => $labels,
      //'supports'  => array( 'title', 'editor'), //ok
      //'taxonomies'            => array( 'category', 'post_tag' ),
      //'hierarchical'          => true,//non serve
      'public'    => true,//true,
      'show_ui'   => true,//true,
      'show_in_menu'          => true,//true,
      'menu_position'         => 20,
      'show_in_admin_bar'     => true,//true,
      'show_in_nav_menus'     => true,//true,
      'can_export'            => true,
      'has_archive'           => false,
      'exclude_from_search'   => true,//ok
      'publicly_queryable'    => true,//ok
      'rewrite'   => false,//from default page config
      'query_var'  			=> false,//ok
      'capability_type'       => 'page',
    			);
    
      $res=register_post_type( 'carriergem', $args );
     }
    }

    the function create_custom_post_type is called in init action hook with priority 0.

    Every kind of post I create by using of the function wp_insert_post make the custom type disappear from the wp-admin menu.

    This is the last code I use, in the wp_loaded action hook, for post creation:

    public function create_custom_page(){
      // create page for plugin
      $status = get_option($this->status, -1);
      $post_id = get_option ( $this->page_id, - 1 );
      if ($post_id == - 1 && $status == -1) {
        // register page
    
    	$post = array (
    	'comment_status' => 'closed',
    	'ping_status' => 'closed',
    	'post_author' => 1,
     	'post_date' => date ( 'Y-m-d H:i:s', current_time( 'timestamp', 0 )),
     	'post_date_gmt' => date ( 'Y-m-d H:i:s', current_time( 'timestamp', 1 )),
     	'post_modified' => date ( 'Y-m-d H:i:s', current_time( 'timestamp', 0 )),
     	'post_modified_gmt' => gmdate ( 'Y-m-d H:i:s', current_time( 'timestamp', 1 )),
     	'post_name' => 'carriergem-linker',
     	//'post_status' => 'publish',
     	'post_title' => '',
     	'post_content' => '',
     	'post_parent' => 0,
     	'menu_order' => 0,
     	'comment_count' => 0,
     	'post_type' => 'carriergem', // 'page',
    	);
    	$post_id = wp_insert_post ( $post , true);
    	//add_post_meta( $post_id, '_edit_last', 1 );
    	//add_post_meta( $post_id, '_edit_lock', current_time( 'timestamp', 0 ).':1' );
    	carriergem_log_me ( $post_id );
    	add_option ( $this->page_id, $post_id );
    
    	}
    }

    No error in the wordpress debug file…I don’t know what is wrong

    It’s because you’re only registering the post type if the post hasn’t been published:

    if ($post_id == - 1 && $status == -1) {

    register_post_type() needs to be called on every page load. It’s not like wp_insert_post() where it’s a one-time insert into the database.

    Thread Starter tempcode

    (@tempcode)

    Doh!

    register_post_type() needs to be called on every page load. It’s not like

    Jacob you have saved me. Thank you very much!

    It’s not a strange think to register a custom post type on every page load?

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Create custom post type in plugin code. Invalid post type page and next…puf’ is closed to new replies.