Sunday, 8 July 2018

PHP Wordpress Plugin For Custom Post

In This Tutorial We will Create a Plugin For Custom Post.

Do Below steps for create plugin.

  • Create a folder "wordpress-plugin" in wp-content folder of your wordpress setup.
  • Creat index.php file in wordpress-plugin folder and keep it blank. Reason behind to keep file blank is if somebody visit this path they see only see blank page.So our they can't see our other file.So it's secure for us.
  • Create other file wpplugin.php in wordpress-plugin folder and put below code in it. Need Some php tag for php code

/*
  Plugin Name: WordPress Custom Plugin
  Description: Plugin for testing purpose
  Version: 1
  Author: BS
  Author URI: http://www.google.com
 */
 
 
 // Product Custom Post Type
function product_init() {
    // set up product labels
    $labels = array(
        'name' => 'Products',
        'singular_name' => 'Product',
        'add_new' => 'Add New Product',
        'add_new_item' => 'Add New Product',
        'edit_item' => 'Edit Product',
        'new_item' => 'New Product',
        'all_items' => 'All Products',
        'view_item' => 'View Product',
        'search_items' => 'Search Products',
        'not_found' =>  'No Products Found',
        'not_found_in_trash' => 'No Products found in Trash',
        'parent_item_colon' => '',
        'menu_name' => 'Products',
    );
   
    // register post type
    $args = array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'show_ui' => true,
        'capability_type' => 'post',
        'hierarchical' => false,
        'rewrite' => array('slug' => 'product'),
        'query_var' => true,
        'menu_icon' => 'dashicons-randomize',
        'supports' => array(
            'title',
            'editor','thumbnail'
          
        )
    );
    register_post_type( 'product', $args );
               
                                                 register_taxonomy(
                                                                'product_category',
                                                                'product',
                                                                array('hierarchical' => true,
                                                                 'label' => 'Category',
                                                                  'query_var' => true,
                                                                   'rewrite' => array( 'slug' => 'product-category' )));

 

 }
 add_action( 'init', 'product_init' );
 
 /**
 * Register meta box(es).
 */
function wpdocs_register_meta_boxes() {
    add_meta_box( 'meta-box-id', __( 'My Meta Box', 'textdomain' ), 'wpdocs_my_display_callback', 'product' );
}
add_action( 'add_meta_boxes', 'wpdocs_register_meta_boxes' );

/**
 * Meta box display callback.
 *
 * @param WP_Post $post Current post object.
 */
function wpdocs_my_display_callback( $post ) {
    // Display code/markup goes here. Don't forget to include nonces!
                $product_author = get_post_meta( $post->ID, "product_author", true );
                <table class="form-table">
                                <tbody>
                                                <tr>
                                                                <th scope="row"><label for="product_author">Product Meta</label></th>
                                                                <td><input name="product_author" id="product_author" class="regular-text" type="text" value=""></td>
                                                </tr>
                                </tbody>
                </table>
}

function wpdocs_save_meta_box( $post_id ) {
    // Save logic goes here. Don't forget to include nonce checks
                update_post_meta( $post_id, "product_author", $_POST["product_author"] );               
}
add_action( 'save_post', 'wpdocs_save_meta_box' );

0 comments:

Post a Comment