0

Apologies if this isn't an appropriate question for the forum, I'm new to wordpress development and needed to bring this question to some experts.

I've written a plugin that creates custom post types. Currently, I've created a menu page with add_menu_page. I've hooked this to a function that creates a page that includes a form to add a custom post type.

When the user submits the form, the page inside the admin dashboard refreshes. However, I have to manually refresh the page to see the new custom post type appear in my admin menu!

I'd like to force a refresh of the entire admin dashboard (including the left side admin menu panel) but I don't fully understand if I can trigger an action from the client or how I can use javascript effectively to do so.

I've tried embedding JavaScript on the form submit button: <input type="submit" onclick="window.location.reload()" but this doesn't work and comes with wierd consequences like event bubbling when focusing on the form.

I've tried using registered_post_type and admin_menu action hooks combined with echoing http-equiv="refresh" .. and header('Location: '.$_SERVER[ 'HTTP_REFERER' ]) in my php code but this either causes infinite loops, being unable to switch to the backend because of Location: $_SERVER[ ... ] of my dashboard, or refreshing every second due to http-equiv="...".

I've also tried hooking up a javascript that listens for the submit event on the form and calls location.reload() but this also doesn't have to the expected result (it doesn't reload the entire page, only the menu page itself).

Is there a simple way to trigger an admin menu refresh? I feel like I need to init another WordPress rerender somehow.

It could be the placement/location in the code to some of these calls to refresh the page are misplaced.

Here's a link to my code on github: https://github.com/songfarm-david/Peak-Custom-Post-Types

Here's a similar question with some of the solutions I've talked about: Refresh page after form action

Link to similar question: Refresh Taxonomies

Thanks in advance,

  • This is just a thought… Should you perhaps be interrupting the admin load via the admin_init hook so you can update the page with the new data? In other words, is there maybe an earlier hook you can use to save your data so that when the admin page loads, your data has already been added?? – Ted Stresen-Reuter Sep 4 at 8:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.