-1

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.

6
  • Where is the problem? Where is the code?
    – Max Yudin
    Commented Jan 19, 2018 at 18:03
  • i add code Max Yadin where is my problem. why data not save. Commented Jan 19, 2018 at 18:37
  • Max Yudin where is my problem in my code???? Commented Jan 19, 2018 at 19:08
  • You probably need to pass the post_id in the ajax too.
    – Shibi
    Commented Jan 19, 2018 at 19:10
  • how please tell me shibi??? Commented Jan 19, 2018 at 19:13

1 Answer 1

0

You can get the post id like this

var post_id = $('input[name="post_ID"]').val();

See this full working code. BTW its only save the value to the meta in the ajax it won't save it on the publish button you need to add another hook with another function. you can't use the same function that you used in the ajax because the die()

add_action( 'add_meta_boxes', 'rt_book_sales' );
add_action( 'wp_ajax_rt_book_name_rander', 'rt_book_name_rander' );
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($post){
?>
</div class= "wrap">
   <input type="text" name="book_name" id="book_name" value="<?php echo esc_attr(get_post_meta( $post->ID, 'rt_book', true )); ?>">
   <input type="submit" class="rt_bk_btn">
</div>
<?php
}

add_action('admin_footer', 'my_admin_add_js');
function my_admin_add_js() {
    ?>
    <script>
        jQuery(document).ready(function($) {
            $(document).on('click', '.rt_bk_btn', function(e) {
                e.preventDefault();
                var bk_name = $('#book_name').val();
                var post_id = $('input[name="post_ID"]').val();
                var data = {
                    'post_id': post_id,
                    'bk_name': bk_name,
                    'action': 'rt_book_name_rander',
                }
                $.post(ajaxurl, data, function(resp){
                    console.log(resp);
                });
            });
        });
    </script>
    <?php
}

function rt_book_name_rander(){
    if (isset($_POST['bk_name']) && isset($_POST['post_id'])) {
        $bk_name = $_POST['bk_name'];
        update_post_meta( $_POST['post_id'], 'rt_book', $bk_name );
    }

    die();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.