#StackBounty: #custom-post-types #plugins #menus #settings Update menu when saving settings

Bounty: 50

I have a settings page for my plugin in which the user can toggle a custom post type on or off. When the plugin is initialized I then check this setting to see if the custom content type should be registered or not. The
initialize() method is called on the init action:

function initialize() {
  if ($this->setting->get('enable_cpt')) {
    $this->registerPostType();
  }
}

function registerPostType() {
  $args = array(
    ...
  );
  register_post_type('my-custom-cpt', $args);
}

This works as expected, but the problem is that when I’m saving the form the menu item for the custom post type is not updated until I reload the page. Obviously this is because the menu is already created.

I tried to use wp_redirect() after the setting is saved, but I’m not able to set any headers because the output has already started.

Can someone please explain how I can make the menu item for my custom post type update when saving the setting for my plugin?


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.