Home Forums Backend Issues (wp-admin) Meta Boxes with button to save Acf values and run function


Meta Boxes with button to save Acf values and run function

  • Can someone help me to add a submit button to any metaboxes i define on my post/page?

    <!--Add a button and a nonce-field to your form-->
    <input type="hidden" name="my_ajax_nonce" value="<?php echo wp_create_nonce('my_ajax_action');?>" />
    <button id="submit-my-form" type="submit"><?php _e('Save custom meta data')?></button>

    And then there is my ajax code:

    jQuery(document).ready(function($) {
        $('body').on('click', '#submit-my-form', function(e) {
            var $me = $(this),
                action = 'my_ajax_action';
            var data = $.extend(true, $, {
                action: action,
                form_data: $('#post').serializeArray()
            $.post(ajaxurl, data, function(response) {
                if(response == '0' || response == '-1'){
                    //TODO: Add Error handling (Wrong nonce, no permissions, …) here.
                } else {
                    //TODO: Do stuff with your response (manipulate DOM, alert user, …)

    My function code is as follows:

    function my_ajax_action() {
        if(!wp_verify_nonce( $_POST['my_ajax_nonce'], 'my_ajax_action' )) {
        //TODO: Add your button saving stuff here.
    function my_ajax_action_init() {
        if ($_POST['action'] == 'my_ajax_action') {
    if (is_admin()){
        add_action('wp_ajax_my_ajax_action', 'my_ajax_action');
    add_action( 'init', 'my_ajax_action_init');

    Can someone help to how I can add this to hook to my ACF metabox?

  • This isn’t really the place to get general WP coding help. The best place to start when trying to do AJAX in WP it here It says AJAX in plugins but the information there can be applied to doing AJAX anywhere in WP.

  • Good news I was looking to add a button to the admin metabox created by ACF to pass an ajax action and found a great solution for you here.

    This code will allow you to add a plug-in that adds a ‘button’ to your field types and there you can set it for external URL or AJAX – the rest is like every other AJAX solution that is well documented around the web.

    Hope this helps you. I’ve already used it for my project. It worked great!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Meta Boxes with button to save Acf values and run function’ is closed to new replies.