Adding Tabs to Sub Menu Option Page

I have not been able to find a suitable answer on the web for what I am trying to do. I have created a custom post type “donation” and under that, have a submenu of “settings”. I want the settings page to use tabs for different things to keep it simple.

Here is what I have:

function sg_settings_setup() {
add_submenu_page( ‘edit.php?post_type=donation’, ‘Settings’, ‘Settings’, ‘manage_options’, ‘sg-settings’, ‘sg_settings_page’ );

function sg_settings_page() { ?>
<div class=”wrap”>
<h1>Main Title</h1>

<?php settings_errors(); ?>

<?php $active_tab = isset( $_GET[ ‘tab’ ] ) ? $_GET[ ‘tab’ ] : ‘general-settings’; ?>

<h2 class=”nav-tab-wrapper”>
<a href=”?page=sg-settings&tab=general-settings” class=”nav-tab <?php echo $active_tab == ‘general-settings’ ? ‘nav-tab-active’ : ”; ?>”>General Settings</a>
<a href=”?page=sg-settings&tab=email-settings” class=”nav-tab <?php echo $active_tab == ’email-settings’ ? ‘nav-tab-active’ : ”; ?>”>Email Settings</a>
<a href=”?page=sg-settings&tab=stripe-settings” class=”nav-tab <?php echo $active_tab == ‘stripe-settings’ ? ‘nav-tab-active’ : ”; ?>”>API Settings</a>

<form method=”post” action=”options.php”>
<?php settings_fields( ‘sg_api_settings’ ); ?>
<?php do_settings_sections( ‘sg_api_settings’ ); ?>
<?php submit_button(); ?>
<?php } ?>

When I click on a tab, I get the error message “Sorry, you are not allowed to access this page.” I can see in the address bar that the URL is being rewritten where it no longer has “edit.php?post_type=donation” in the URL. I tried adding this to the links for the tabs, but it gets edited out by WordPress.

I can make this work if the settings page is a menu_page, but the URL somehow needs to be rewritten for submenu_page. Hopefully someone else has run into this and knows a quick fix!

Read more here:: Adding Tabs to Sub Menu Option Page

Leave a Reply

Your email address will not be published. Required fields are marked *