Unable to delete option

I am able to delete the option with form action admin-post.php but it gives me a blank page after clicking the button

<form action="<?php echo admin_url('admin-post.php'); ?>" method="post">
  <input type="hidden" name="action" value="my_media_update">
  <input type="submit" value="Update Media Titles and ALT Text">
</form>

public function kh_update_media_seo() {
    delete_option('myoption');
}
add_action( 'admin_post_my_media_update', 'kh_update_media_seo' );

if I change the action ="admin_url('admin.php?page=mycustomoptionspage');" it redirects to my original page but does not delete the option -> delete_option('myoption')

Answers 1

  • You're almost there... If you send the request to admin-post.php, then only your callback will print the response. And since your callback doesn't print anything, then you're getting blank screen.

    Most of the time what you want to do is to perform a redirect inside such callback:

    public function kh_update_media_seo() {
        delete_option('myoption');
        wp_redirect( admin_url('admin.php?page=mycustomoptionspage') );
        exit;
    }
    add_action( 'admin_post_my_media_update', 'kh_update_media_seo' );
    

Related Questions