Powered By Blogger

Friday, October 28, 2016

Add custom taxonomy type under post with form

REF : https://metabox.io/plugins/custom-taxonomy/


// Post Stock box
add_action( 'add_meta_boxes', 'post_stock' );
function post_stock() {
   add_meta_box(
       'post_stock',
       __( 'Stock', 'agrg' ),
       'post_stock_content',
       'post',
       'side',
       'high'
   );
}

function post_stock_content( $post ) {
wp_nonce_field( 'myplugin_meta_boxee', 'myplugin_meta_box_nonceee' );
$post_stock = get_post_meta( $post->ID, 'post_stock', true );

echo '<label for="post_stock"></label>';
echo '<input type="text" id="post_stock" name="post_stock" placeholder="'._e('Enter Stock here','agrg').'" value="';
echo $post_stock;
echo '">';

}

add_action( 'save_post', 'post_stock_save' );
function post_stock_save( $post_id ) {
global $post_stock;
if ( ! isset( $_POST['myplugin_meta_box_nonceee'] ) ) {
return;
}
if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonceee'], 'myplugin_meta_boxee' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}

if(isset($_POST["post_stock"]))
$post_stock = $_POST['post_stock'];
update_post_meta( $post_id, 'post_stock', $post_stock );

}


$stock_labels = array(
            'name' => __( 'Stock', 'agrg' ),
            'singular_name' => __( 'Stock', 'agrg' ),
            'search_items' =>  __( 'Search Stocks', 'agrg' ),
            'popular_items' => __( 'Popular Stocks', 'agrg' ),
            'all_items' => __( 'All Stocks', 'agrg' ),
            'parent_item' => __( 'Parent Stock', 'agrg' ),
            'parent_item_colon' => __( 'Parent Stock:', 'agrg' ),
            'edit_item' => __( 'Edit Stock', 'agrg' ),
            'update_item' => __( 'Update Stock', 'agrg' ),
            'add_new_item' => __( 'Add New Stock', 'agrg' ),
            'new_item_name' => __( 'New Stock Name', 'agrg' ),
            'separate_items_with_commas' => __( 'Separate Stocks with commas', 'agrg' ),
            'add_or_remove_items' => __( 'Add or remove Stock', 'agrg' ),
            'choose_from_most_used' => __( 'Choose from the most used Stocks', 'agrg' ),
            'menu_name' => __( 'Stocks', 'agrg' )
        );
register_taxonomy("Stock",
array("post"),
array("hierarchical" => true,
'labels' => $stock_labels,
'show_ui' => true,
                'query_var' => true,
                'rewrite' => array('slug' => __('Stock', 'agrg'))
)
);


Add value in custom taxonomy pro-grammatically :

REF: https://codex.wordpress.org/Function_Reference/wp_insert_term

/* Location update while seller registration */
$saddress = sanitize_text_field($address);
//check location is exist
$loc_exist = term_exists( $saddress ); // array is returned if taxonomy is given

if (empty($loc_exist) ) {

$parent_term = term_exists( 'location' ); // array is returned if taxonomy is given
       $parent_term_id = $parent_term['term_id']; // get numeric term id
wp_insert_term(
 $saddress, // the term
 'location', // the taxonomy
 array(
   'description'=> 'A seller location.',
   'slug' => $saddress,
   'parent'=> $parent_term_id
                   )
    );
}


No comments:

Post a Comment