admin管理员组

文章数量:1130349

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

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

本文标签: theme developmentUnable to delete option