Issue with Post Request on Post Edit Screen

I’m developing a WordPress plugin, and I need to add a button to the edit screen of a custom post type that triggers a function when clicked (the button will update a custom field on that post and redirect to an admin page). I used this example from the Codex to write the code below to add a meta box with a POST Request button:

// Add meta box to applicant post type
function share_applicant_button() {
add_meta_box(
‘share-applicant-button’,
‘Share Applicant’,
‘share_applicant_button_callback’,
‘applicants’,
‘side’
);
}

add_action( ‘add_meta_boxes’, ‘share_applicant_button’ );

// Create the form
function share_applicant_button_callback( $post ) {
?>
<form action=”<?php echo esc_url( admin_url(‘admin-post.php’) ); ?>” method=”post”>
<input type=”hidden” name=”action” value=”share_application_button”>
<input type=”submit” value=”Share Applicant” class=”button-primary”>
</form>
<?php
}

// Function triggered by button click
function share_application_internal() {

// Function code here

}

add_action( ‘admin_post_share_application_button’,’share_application_internal’ );

When I press the button, it takes me to the edit.php screen (for posts, not even the same custom post type). It also does this if I click the Update button on the post.

If I delete this part of the code (specifically name=”action”):

<input type=”hidden” name=”action” value=”share_application_button”>

It stops interfering with the post Update button, but obviously the post request still doesn’t work.

I have a similar button on the edit.php screen for that post type that executes a different function, and that works fine. The code is basically the same except for how the meta box is added.

How do I make this work?

Read more here:: Issue with Post Request on Post Edit Screen

Leave a Reply

Your email address will not be published. Required fields are marked *