1

Trying to:

Submit form using admin-ajax.php to update post data from 'draft' status to 'publish' status and add custom meta field for filter type.

Form contents:

<form action="<?php echo admin_url( 'admin-ajax.php' ) ?>" method="post">

        <?php wp_nonce_field( 'submit_filter', 'my_filter_nonce' ); ?>

        <div class="small-6 medium-3 large-3 columns">
            <div class="card">
                <img src="<?php echo get_the_post_thumbnail_url( $posts[0]->ID, 'thumbnail' ); ?>" class="" id="normal">
                    <div class="card-section">
                        <input type="submit" value="No Filter" name="normal">
                    </div>
            </div>
        </div>
</form>

Form action:

// process form actions
add_action( 'wp_ajax_nopriv_submit_filter', 'my_submission_filter' );
add_action( 'wp_ajax_submit_filter', 'my_submission_filter' );


function my_submission_filter() {

    $postid = get_the_ID();
    $post_data = array(
          'ID'              => $postid,
          'post_status'     => 'publish'

     );


// add filter meta data
add_post_meta($postid, 'filter', $_POST['filter']); 

// Update the post into the database
wp_update_post( $my_post );

// redirect back to site to see post
wp_redirect( site_url());

die();

}

Results:

Blank page with a "0" in the top right corner and no updates to post.

Would like:

Help making this work :)

2
  • I'll give you the same response everyone gave me for trying to use AJAX in WordPress. Try the REST API instead. Aug 4, 2017 at 19:22
  • I don't believe the REST API or Admin AJAX are appropriate here as form handlers. There's no javascript request happening here
    – Tom J Nowell
    Aug 4, 2017 at 21:53

3 Answers 3

1

You don't set an action in your form, so nothing is executed.

Add a hidden field with action to the form:

<input type="hidden" value="submit_filter" name="action">

Also note that this:

$postid = get_the_ID();

won't work. This is a new request, so there is no post to get the ID from. You have to pass this with your form data as well.

1

admin-ajax.php is intended for AJAX requests, but this is not an AJAX request, it's a form submission! Perhaps if this was a javascript based submission it would be appropriate, but, you should avoid the quirks of Admin AJAX and make a standard form handler.

When submitting a form, submit it to the same page the form is located on. This way the form handling and display are next to each other and validation becomes a lot easier to display.

For example:

if ( ! empty( $_POST['submission'] )) {
    // the form was submitted!

    $success = false;
    // fetch the parameters, do some validation, etc

    // ... handle the form submission

    // all done, if successful say so else load the form
    if ( true === $success ) {
        get_template_part( 'theform', 'success' ); // theform-success.php
    } else {
        // show the form again with whatever validation errors there were
        get_template_part( 'theform' ); 
    }
} else {
    // the form was not submitted, DISPLAY THE FORM!!! First page
    get_template_part( 'theform' );
}

theform.php will contain your form, and for the action of the form you'll just leave it empty, e.g. <form method="POST" action="">

Handling Multiple Pages

You can add a hidden field named page, and then load different templates depending on its value, and process when on the final page + 1. There is something to keep in mind though, values from page 1 won't be there when you submit page 2, so you need to carry them over in hidden fields.

1

You are not submitting your data with AJAX, so there is no need to use admin-ajax.php and the wp_ajax hook. Instead, you need the admin_post hook.

You need to make a few changes to your code in order to make it work. First, change your form action to <?php echo esc_url( admin_url('admin-post.php') ); ?> and add a hidden action field at the end of your form <input type="hidden" name="action" value="submit_filter">.

This way, anytime a request is send to WordPress and the action parameter is set to "submit_filter", the hook admin_post_submit_filter will be executed.

This is how your form code should look:

<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post">
   <?php wp_nonce_field( 'submit_filter', 'my_filter_nonce' ); ?>
   <div class="small-6 medium-3 large-3 columns">
      <div class="card">
          <img src="<?php echo get_the_post_thumbnail_url( $posts[0]->ID, 'thumbnail' ); ?>" class="" id="normal">
          <div class="card-section">
              <input type="submit" value="No Filter" name="normal">
          </div>
      </div>
    </div>
    <input type="hidden" name="action" value="submit_filter">
</form>

And the function code to process your request:

add_action( 'admin_post_nopriv_submit_filter', 'my_submission_filter' );
add_action( 'admin_post_submit_filter', 'my_submission_filter' );

function my_submission_filter() {
  $postid = get_the_ID();

  $post_data = array(
    'ID'              => $postid,
    'post_status'     => 'publish'
  );

  add_post_meta($postid, 'filter', $_POST['filter']); 
  wp_update_post( $my_post );

  wp_redirect( site_url());
}

It's also good to check for errors using is_wp_error

https://codex.wordpress.org/Plugin_API/Action_Reference/admin_post_(action) https://codex.wordpress.org/Function_Reference/is_wp_error

Your Answer

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

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