I want to add meta-box in my plugin but i want add two inputs and one button like submit button when i click on submit button then input fields data send to database and received but how this is possible without publish button i never use publish button. please any help with simple code.
code.
add_action( 'add_meta_boxes', 'rt_book_sales' );
add_action( 'wp_ajax_rt_book_name_rander', 'rt_book_name_rander' );
add_action( 'save_post', 'rt_book_name_rander', 10, 3 );
function rt_book_sales(){
add_meta_box( 'rt_bk_goal', 'Meta Box Title', 'rt_books_meta_rander', 'post', 'normal', 'high' );
}
function rt_books_meta_rander(){
?>
</div class= "wrap">
<input type="text" name="book_name" id="book_name" >
<input type="submit" class="rt_bk_btn">
</div>
<?php
}
?>
and ajax jquery code.
$(document).on('click', '.rt_bk_btn', function(e) {
e.preventDefault();
var bk_name = $('#book_name').val();
var data = {
'bk_name': bk_name,
'action': 'rt_book_name_rander',
}
$.post(ajaxurl, data, function(resp){
console.log(resp);
});
and save post function.
function rt_book_name_rander(){
global $post;
if (isset($_POST['bk_name'])) {
$bk_name = $_POST['bk_name'];
update_post_meta( $post->ID, 'rt_book', $bk_name );
}
die(0);
}
this is my code and no data save in database when i get this data with get_post_meta() function.. where is my problem.
post_id
in the ajax too.