• Hi,

    I am try to develop a plugin for create post type but the register post type is not working can any one help me.

     add_action('init','newposttype_create');
    
     function newposttype_create()
     {
         $newpost="mysliderservice";
         $labels = array(
             'name'                  => _x( $newpost, 'Post type general name', 'textdomain' ),
             'singular_name'         => _x( $newpost, 'Post type singular name', 'textdomain' ),
             'menu_name'             => _x( 's', 'Admin Menu text', 'textdomain' ),
             'name_admin_bar'        => _x( $newpost, 'Add New on Toolbar', 'textdomain' ),
             'add_new'               => __( 'Add New', 'textdomain' ),
             'add_new_item'          => __( 'Add New ', 'textdomain' ),
             'new_item'              => __( 'New ', 'textdomain' ),
             'edit_item'             => __( 'Edit ', 'textdomain' ),
             'view_item'             => __( 'View ', 'textdomain' ),
             'all_items'             => __( 'All ', 'textdomain' ),
             'search_items'          => __( 'Search ', 'textdomain' ),
             'parent_item_colon'     => __( 'Parent :', 'textdomain' ),
             'not_found'             => __( 'No found.', 'textdomain' ),
             'not_found_in_trash'    => __( 'No found in Trash.', 'textdomain' ),
             'featured_image'        => _x( ' Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'textdomain' ),
             'set_featured_image'    => _x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
             'remove_featured_image' => _x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
             'use_featured_image'    => _x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
             'archives'              => _x( ' archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'textdomain' ),
             'insert_into_item'      => _x( 'Insert into Location', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'textdomain' ),
             'uploaded_to_this_item' => _x( 'Uploaded to this Location', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'textdomain' ),
             'filter_items_list'     => _x( 'Filter Locations list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'textdomain' ),
             'items_list_navigation' => _x( ' list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'textdomain' ),
             'items_list'            => _x( ' list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'textdomain' ),
         );
    
         $args = array(
             'labels'             => $labels,
             'public'             => true,
             'publicly_queryable' => true,
             'show_ui'            => true,
             'show_in_menu'       => true,
             'query_var'          => true,
             'rewrite'            => array( 'slug' => $newpost ),
             'capability_type'    => 'post',
             'has_archive'        => true,
             'hierarchical'       => false,
             'menu_position'      => null,
             'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt'),
         );
         register_post_type( $newpost, $args );
       }
    
       function handle_form() {
          if(
              ! isset( $_POST['awesome_form'] ) ||
              ! wp_verify_nonce( $_POST['awesome_form'], 'awesome_update' )
          ){ ?>
              <div class="error">
                 <p>Sorry, your nonce was not correct. Please try again.</p>
              </div> <?php
              exit;
          } else {
                $firstpost = sanitize_text_field($_POST['fposttype'] );
                $secondpost = sanitize_text_field($_POST['sposttype'] );
                $newpost=$firstpost.$secondpost;
                echo $newpost;
    
                add_action('init','newposttype_create');
    
          }
    
        }
    
       <form method="POST" action="">
          <input type="hidden" name="updated" value="true" />
          <?php wp_nonce_field( 'awesome_update', 'awesome_form' ); ?>
           <table class="form-table">
               <tbody>
                   <tr>
                       <th><label for="fposttype">Select Post Type</label></th>
                       <td>
                         <select name="fposttype" id="fposttype"/>
                         <?php
                         $args = array(
                          'public'   => true,
                          '_builtin' => false
                         );
                         $output = 'names'; // 'names' or 'objects' (default: 'names')
                         $operator = 'and'; // 'and' or 'or' (default: 'and')
                         $post_types = get_post_types( $args, $output, $operator );
                         if ( $post_types ) {
                           foreach ( $post_types  as $post_type ){
                         ?>
                           <option value="<?=$post_type?>" onchange="javascript:changesecondpslop(this.value);"><?=$post_type?></option>
                         <?php
                             }
                           }
                         ?>
                         </select>
                       </td>
                   </tr>
                   <tr>
                       <th><label for="sposttype">Select Post Type</label></th>
                       <td>
                         <select name="sposttype" id="sposttype"/>
                         <option>Select One</option>
                         </select>
                       </td>
                   </tr>
                   <tr>
                       <th><label for="seltmp">Select Template</label></th>
                       <td>
                         <select name="seltmp" id="seltmp"/>
                         <option>Select One</option>
                         <?php
                         $default = ''; $post_type = 'page';
                         $templates = get_page_templates( null, $post_type );
                         ksort( $templates );
                         foreach ( array_keys( $templates ) as $template ) {
                             $selected = selected( $default, $templates[ $template ], false );
                             echo "\n\t<option value='" . esc_attr( $templates[ $template ] ) . "' $selected>" . esc_html( $template ) . '</option>';
                         }
                         ?>
                         </select>
                       </td>
                   </tr>
               </tbody>
           </table>
           <p class="submit">
               <a>Submit</a>
           </p>
           <p class="submit">
               <input type="submit" name="submit" id="submit" class="button button-primary" value="Check My Info!">
           </p>
       </form>
    • This topic was modified 4 years, 7 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Everything else WordPress topic
Viewing 5 replies - 1 through 5 (of 5 total)
  • Are you putting this in your functions.php file? If you just include the add_action(‘init’,’newposttype_create’) with the function newposttype_create(), do you see the “mysliderservice” in the wordpress admin? If so, then your register post type is working and it’s a problem with the form.

    Thread Starter letternanda

    (@letternanda)

    Thanks for your reply but i put this in plugin folder in php file not in theme-> function.php. I am trying to create a plugin.

    Thread Starter letternanda

    (@letternanda)

    user go to wp-admin plugin page and then put some in the form and then it will create a custom post type with this name.

    Ah I see. I’m not really sure what to suggest except have you activated your custom plugin to see the post type shows in the admin bar?

    Hi @letternanda,

    I checked your My Slider Service plugin code for custom post type “mysliderservice”, I implemented it at my end and it is working fine.

    plugin path: wp-content/plugins/mysliderservice/mysliderservice.php

       <?php
        /*
        Plugin Name: Plugin Development 101
        Plugin URI: https://abc.com/mysliderservice/
        Description: An introductory demo for plugin development
        Author: Mangesh Nadekar
        Version: 1.0.0
        */
    
      add_action('init','newposttype_create');
    
      function newposttype_create() {
         $newpost="mysliderservice";
         $labels = array(
             'name'                  => _x( $newpost, 'Post type general name', 'textdomain' ),
             'singular_name'         => _x( $newpost, 'Post type singular name', 'textdomain' ),
             'menu_name'             => _x( 's', 'Admin Menu text', 'textdomain' ),
             'name_admin_bar'        => _x( $newpost, 'Add New on Toolbar', 'textdomain' ),
             'add_new'               => __( 'Add New', 'textdomain' ),
             'add_new_item'          => __( 'Add New ', 'textdomain' ),
             'new_item'              => __( 'New ', 'textdomain' ),
             'edit_item'             => __( 'Edit ', 'textdomain' ),
             'view_item'             => __( 'View ', 'textdomain' ),
             'all_items'             => __( 'All ', 'textdomain' ),
             'search_items'          => __( 'Search ', 'textdomain' ),
             'parent_item_colon'     => __( 'Parent :', 'textdomain' ),
             'not_found'             => __( 'No found.', 'textdomain' ),
             'not_found_in_trash'    => __( 'No found in Trash.', 'textdomain' ),
             'featured_image'        => _x( ' Cover Image', 'Overrides the “Featured Image” phrase for this post type. Added in 4.3', 'textdomain' ),
             'set_featured_image'    => _x( 'Set cover image', 'Overrides the “Set featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
             'remove_featured_image' => _x( 'Remove cover image', 'Overrides the “Remove featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
             'use_featured_image'    => _x( 'Use as cover image', 'Overrides the “Use as featured image” phrase for this post type. Added in 4.3', 'textdomain' ),
             'archives'              => _x( ' archives', 'The post type archive label used in nav menus. Default “Post Archives”. Added in 4.4', 'textdomain' ),
             'insert_into_item'      => _x( 'Insert into Location', 'Overrides the “Insert into post”/”Insert into page” phrase (used when inserting media into a post). Added in 4.4', 'textdomain' ),
             'uploaded_to_this_item' => _x( 'Uploaded to this Location', 'Overrides the “Uploaded to this post”/”Uploaded to this page” phrase (used when viewing media attached to a post). Added in 4.4', 'textdomain' ),
             'filter_items_list'     => _x( 'Filter Locations list', 'Screen reader text for the filter links heading on the post type listing screen. Default “Filter posts list”/”Filter pages list”. Added in 4.4', 'textdomain' ),
             'items_list_navigation' => _x( ' list navigation', 'Screen reader text for the pagination heading on the post type listing screen. Default “Posts list navigation”/”Pages list navigation”. Added in 4.4', 'textdomain' ),
             'items_list'            => _x( ' list', 'Screen reader text for the items list heading on the post type listing screen. Default “Posts list”/”Pages list”. Added in 4.4', 'textdomain' ),
         );
    
         $args = array(
             'labels'             => $labels,
             'public'             => true,
             'publicly_queryable' => true,
             'show_ui'            => true,
             'show_in_menu'       => true,
             'query_var'          => true,
             'rewrite'            => array( 'slug' => $newpost ),
             'capability_type'    => 'post',
             'has_archive'        => true,
             'hierarchical'       => false,
             'menu_position'      => null,
             'supports'           => array( 'title', 'editor', 'thumbnail', 'excerpt'),
         );
         register_post_type( $newpost, $args );
       }

    Check link:

    Slider listing: https://tinyurl.com/y48l85ao

    Single Slide: https://tinyurl.com/y2n4d42w

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Plugin development for custom post type’ is closed to new replies.