php - Trying to reload page with update_option_{$option} hook causing infinite reload

admin2025-04-19  0

How can I reload the page after this option is updated? What I'm trying below is causing an infinite page reload loop. I've also tried add if($old_value !== $) check but still no luck.

add_action( 'update_option_kdc-site-functions', 'kdc_reload_admin', 10, 2 );
/**
 * Reload admin page after settings page is saved.
 */
function kdc_reload_admin( $old_value, $value ) {
    ?>
    <script type="text/javascript">
    document.location.reload(true);
    </script>
    <?php
}

How can I reload the page after this option is updated? What I'm trying below is causing an infinite page reload loop. I've also tried add if($old_value !== $) check but still no luck.

add_action( 'update_option_kdc-site-functions', 'kdc_reload_admin', 10, 2 );
/**
 * Reload admin page after settings page is saved.
 */
function kdc_reload_admin( $old_value, $value ) {
    ?>
    <script type="text/javascript">
    document.location.reload(true);
    </script>
    <?php
}
Share Improve this question asked Oct 3, 2019 at 1:54 Wade KoehnWade Koehn 31 bronze badge 4
  • 1 That hook really isn't the sort of hook that should be used to output HTML. Can you elaborate on what you're trying to achieve? When and how is this option being updated that you require a refresh? – Jacob Peattie Commented Oct 3, 2019 at 2:02
  • @JacobPeattie This is an option and settings page generated using metabox.io. I'm using it to conditionally load or unload CPTs and metaboxes. After the settings are saved, I'd like the page to reload to update the menu. The option is saving and everything works fine right now, I just have to reload the page to see the changes. – Wade Koehn Commented Oct 3, 2019 at 2:12
  • 1 I haven't used metabox.io, but by default normal WordPress settings pages reload when they are saved anyway. If metabox.io settings pages don't do that, then they're doing something custom and the solution would be specific to metabox.io, and 3rd-party plugins are off topic here. – Jacob Peattie Commented Oct 3, 2019 at 2:21
  • The settings page reloads but requires a second manual reload before the menu items change. Probably because in my CPT code, I'm checking this option before loading the CPT and this isn't happening soon enough? Just thought I'd add in a page reload to quick fix it for now... – Wade Koehn Commented Oct 3, 2019 at 2:28
Add a comment  | 

1 Answer 1

Reset to default 0

The reason this was happening, is metabox.io for some reason triggers a page refresh before saving the option. This meant that after the page refresh, the new options did not have time to take effect. My workaround for now is to use header("refresh:0") which accomplishes what I was trying to do in my original post but without causing a refresh loop like the javascript refresh method. Simply forces another page refresh after the option is successfully saved.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1745075351a283534.html

最新回复(0)