Skip to content

Instantly share code, notes, and snippets.

@ibrahimkholil
Created December 12, 2018 06:52
Show Gist options
  • Save ibrahimkholil/6ea5e0773034ce85284fba18d0ea4c9c to your computer and use it in GitHub Desktop.
Save ibrahimkholil/6ea5e0773034ce85284fba18d0ea4c9c to your computer and use it in GitHub Desktop.
custom post type with metabox texanomy
<?php
/**
* register product post type and taxonomy
*/
function hip_med_register_product()
{
register_post_type('product', [
'labels' => [
'name'=> __('Products','hip'),
'singular_name'=>__('Product','hip'),
'all_items' => __('All Products','hip'),
'add_new_item'=>__('Add New Product','hip'),
'edit_item' => __('Edit Product','hip'),
'view_item' => __('View Product','hip'),
'new_item' => __('New Product','hip'),
'search_items' => __('Search Products','hip')
],
'public' => true,
'has_archive' => 'products',
'hierarchical'=> true,
'show_in_admin_bar'=> true,
'menu_position' => 25,
'menu_icon' => 'dashicons-products',
'supports' => [
'title',
'editor',
'thumbnail',
'excerpt',
'page-attributes',
'revisions'
]
]);
register_taxonomy('product_category','product',[
'labels' => [
'name'=> __('Product Categories','hip'),
'singular_name'=> __('Product Category','hip'),
'all_items' => __('All Product Categories','hip'),
'add_new_item'=> __('Add New Product Category','hip'),
'edit_item' => __('Edit Product Category','hip'),
'view_item' => __('View Product Category','hip'),
'new_item' => __('New Product Category','hip'),
'search_items' => __('Search Products Categories','hip')
],
'public' => true,
'show_ui' => true,
'has_archive'=> 'product-category',
'hierarchical'=> true,
'show_admin_column' => true
]);
}
add_action('init', 'hip_med_register_product');
/**
* register product metabox
*/
function register_product_metabox()
{
add_meta_box(
'product-metabox',
'Product Name',
'product_metabox_content',
'product',
'advanced',
'high'
);
}
add_action('add_meta_boxes', 'register_product_metabox');
/**
* content product metabox
*/
function product_metabox_content($post)
{
wp_nonce_field(basename(__FILE__), 'product_metabox_nonce');
$product_name = get_post_meta($post->ID,'product_name',true);
?>
<div style="padding: 10px 0">
<input name="product_name" type="text" class="widefat" value="<?php echo !empty($product_name) ? $product_name : ''; ?>" placeholder="Enter product display name"/>
</div>
<?php
}
/**
* save product metabox value
*/
function save_product_metabox_value($post_id,$post)
{
$product_name = '';
if (!isset($_POST["product_metabox_nonce"]) || !wp_verify_nonce($_POST["product_metabox_nonce"], basename(__FILE__))) {
return $post_id;
}
if (!current_user_can("edit_post", $post_id)) {
return $post_id;
}
if (defined("DOING_AUTOSAVE") && DOING_AUTOSAVE) {
return $post_id;
}
if ('product' != $post->post_type) {
return $post_id;
}
if (isset($_POST['product_name'])) {
$product_name = sanitize_text_field($_POST['product_name']);
}
update_post_meta($post_id, 'product_name', $product_name);
return $post_id;
}
add_action('save_post', 'save_product_metabox_value',10,2);
/**
* move product name metabox to top
*/
function move_metabox_to_top()
{
global $post, $wp_meta_boxes;
if(get_post_type() == 'product'){
do_meta_boxes(get_current_screen(), 'advanced', $post);
unset($wp_meta_boxes['product']['advanced']);
}
}
add_action('edit_form_after_title', 'move_metabox_to_top');
function add_product_metabox_margin(){
$screen = get_current_screen();
if($screen->id == 'product'){
add_action('admin_head',function(){
echo '<style id="product-custom">#product-metabox{margin-top: 20px;}</style>';
});
}
}
add_action('current_screen','add_product_metabox_margin');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment