Add custom field product single page
To add custom meta field in the product single page and save the field data then get value, Need to add this following code
You can add this code in your function.php file
// Adding a custom Meta container to admin products pages
add_action(‘add_meta_boxes’, ‘create_custom_meta_box’); | |
if (!function_exists(‘create_custom_meta_box’)) { | |
function create_custom_meta_box() | |
{ | |
add_meta_box( | |
‘custom_product_meta_box’, | |
__(‘ Product Main URL <em>(orginal)</em>’, ‘cmb’), | |
‘add_custom_product1_meta_box’, | |
‘product’, | |
‘side’ | |
); | |
} | |
} | |
// Custom metabox content in admin product pages | |
if (!function_exists(‘add_custom_product1_meta_box’)) { | |
function add_custom_product1_meta_box($post) | |
{?> | |
<label for=”product1″>Add your main product link:</label> | |
<a class=”btn btn-success” href=”<?php echo get_post_meta($post->ID,’mainpro1′,true);?>” target=”_blank”>Open the original product link</a> | |
<input type=”url” id=”product1″ name=”mainpro1″ class=”widefat” value=”<?php echo get_post_meta($post->ID,’mainpro1′,true);?>”> | |
<label for=”product2″>Add your main product link:</label> | |
<a class=”btn btn-success” href=”<?php echo get_post_meta($post->ID,’mainpro2′,true);?>” target=”_blank”>Open the original product link</a> | |
<input type=”url” id=”product2″ name=”mainpro2″ class=”widefat” value=”<?php echo get_post_meta($post->ID,’mainpro2′,true);?>”> | |
<label for=”product3″>Add your main product link:</label> | |
<a class=”btn btn-success” href=”<?php echo get_post_meta($post->ID,’mainpro3′,true);?>” target=”_blank”>Open the original product link</a> | |
<input type=”url” id=”product3″ name=”mainpro3″ class=”widefat” value=”<?php echo get_post_meta($post->ID,’mainpro3′,true);?>”> | |
<label for=”product4″>Add your main product link:</label> | |
<a class=”btn btn-success” href=”<?php echo get_post_meta($post->ID,’mainpro4′,true);?>” target=”_blank”>Open the original product link</a> | |
<input type=”url” id=”product4″ name=”mainpro4″ class=”widefat” value=”<?php echo get_post_meta($post->ID,’mainpro4′,true);?>”> | |
<label for=”product5″>Add your main product link:</label> | |
<a class=”btn btn-success” href=”<?php echo get_post_meta($post->ID,’mainpro5′,true);?>” target=”_blank”>Open the original product link</a> | |
<input type=”url” id=”product5″ name=”mainpro5″ class=”widefat” value=”<?php echo get_post_meta($post->ID,’mainpro5′,true);?>”> | |
<?php } | |
} | |
function save_custom_product1_meta_box($post_id){ | |
update_post_meta($post_id,’mainpro1′,$_POST[‘mainpro1’]); | |
update_post_meta($post_id,’mainpro2′,$_POST[‘mainpro2’]); | |
update_post_meta($post_id,’mainpro3′,$_POST[‘mainpro3’]); | |
update_post_meta($post_id,’mainpro4′,$_POST[‘mainpro4’]); | |
update_post_meta($post_id,’mainpro5′,$_POST[‘mainpro5’]); | |
} | |
add_action(‘save_post’,’save_custom_product1_meta_box’); |