theme development - Unable to delete option

admin2025-06-05  5

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')

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')

Share Improve this question edited Dec 5, 2018 at 16:04 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Dec 5, 2018 at 16:01 user145078user145078
Add a comment  | 

1 Answer 1

Reset to default 0

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' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749126642a316590.html

最新回复(0)