wordpress create custome post type

Create a folder in plugins directly called “custome-post” an create a file custome-post.php and add the following content in that file (Instead of creating plug in you can directly copy past this content in function.php it will work )

<?php
/*
 Plugin Name: custome post sample
 Plugin URI: http://www.workassis.com
 Description: description
 Author: me	
 Version: 1.0
 Author URI: https://wiki.workassis.com
 */

class custome_post {
    function __construct() {
        #installation script
        register_activation_hook( __FILE__, array( $this, 'installSetup' ) );

        #creating custom post type
        add_action( 'init', array( $this,'ticketTrackerPost') );
        add_action( 'add_meta_boxes', array( $this, 'ticketTrackerPostMeta' ) );
        add_action( 'save_post', array( $this, 'ticketTrackerPostMetaSave' ) );
       
       --------------------------
     }

    #custome post type
    function ticketTrackerPost() {
        register_post_type( 'tickettracker',
            array(
                'labels' => array(
                        'name' => __( 'Ticket Tracker' ),
                        'singular_name' => __( 'Ticket Tracker' ),
                        'add_new_item'       => __( 'Add New Ticket'),
                    ),
                'public' => true,
                'has_archive' => true,
                #'rewrite' => array('slug' => 'tickettracker'),
                'rewrite' => true,
                'supports' => array('title') //, 'editor', 'author', 'custom-fields' )
             )
        );
    }

    # Adds the meta box container.
    public function ticketTrackerPostMeta( $post_type ) {
        add_meta_box('ticketTrackerPostMeta', 'Ticket Details', array( $this, 'ticketTrackerPostMeta_render' ), 'tickettracker', 'advanced', 'high');
    }
	
	
    # Render Meta Box content.
    public function ticketTrackerPostMeta_render( $post ) {
	
        require_once(dirname(__FILE__).'/addTicket.php');
		
    }	
		
    #Save the meta when the post is saved.
    public function ticketTrackerPostMetaSave( $post_id ) {
		
        require_once(dirname(__FILE__).'/saveTicket.php');
    }	

}
new custome_post();

addTicket.php

<?php

    wp_enqueue_script('ticketTrackerJs');
    wp_enqueue_style("ticketTrackerCss");
    wp_nonce_field( 'ticketTracker_', 'ticketTracker_nonce' );
	

    $tfp=get_post_meta( $post->ID, 'tt_fields_priority', true );
     
   <input class="datepicker" name="tt_fields_priority" type="text" value="<?php echo $tfp  ?>" />

?>
<?php 

    // Check if our nonce is set. 
    if ( ! isset( $_POST['ticketTracker_nonce'] ) ) { 
        return $post_id; 
    }
 
    $nonce = $_POST['ticketTracker_nonce']; 

    // Verify that the nonce is valid. 
    if ( ! wp_verify_nonce( $nonce, 'ticketTracker_' ) ) { 
        return $post_id; 
    }
 
    // If this is an autosave, our form has not been submitted, 
    // so we don't want to do anything. 
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { 
        return $post_id; 
    }
 
    // Check the user's permissions. 
    if ( 'page' == $_POST['post_type'] ) { 
        if ( ! current_user_can( 'edit_page', $post_id ) ) 
            return $post_id; 
    } else { 
        if ( ! current_user_can( 'edit_post', $post_id ) ) 
             return $post_id; 
    } 

    /* OK, its safe for us to save the data now. */ 
    // updating to post meta 
    foreach ($_REQUEST as $key =--> $val) { 
	
	if (strpos($key, 'tt_fields_') !== false) {
	    $data = sanitize_text_field($val);
	    update_post_meta( $post_id, $key, $data );
	}

    }

?>

 

Author: bm on July 14, 2014
Category: wordpress

Your comment:

Your Name

Comment:




Last articles