最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Custom plugin settings page with tab. Error: "Sorry, you are not allowed to access this page."

programmeradmin1浏览0评论

I have the code below to show different settings for each tab. Contents shown depends on the url paratmer action. When I try selecting a tab, wordpress gives me a Sorry, you are not allowed to access this page. error. Please see the code below.

function minutes_settings_content() {
    ?>
    <div class="wrap">

        <div id="icon-themes" class="icon32"></div>
        <h2>Email & SMS Settings</h2>
        <?php settings_errors(); ?>

        <?php $active_tab = isset( $_GET[ 'action' ] ) ? $_GET[ 'action' ] : 'email_sms_options'; ?>

        <h2 class="nav-tab-wrapper">
            <a href="<?php echo esc_url( add_query_arg( array( 'action' => 'email_sms_options' ), admin_url( 'edit.php?post_type=wellbeing-minutes&page=minutes-email-settings' ) ) ); ?>" class="nav-tab">Email & SMS</a>
            <a href="<?php echo esc_url( add_query_arg( array( 'action' => 'testing_mode_options' ), admin_url( 'edit.php?post_type=wellbeing-minutes&page=minutes-email-settings' ) ) ); ?>" class="nav-tab">Testing Mode</a>
        </h2>

        <form action='options.php' method='post'>

            <?php
            if ( $active_tab == 'email_sms_options' ) {
                settings_fields( 'EmailSmsMinute' );
                do_settings_sections( 'EmailSmsMinute' );
            }
            elseif ( $active_tab == 'testing_mode_options' ) {
                settings_fields( 'wb_minute_testing_mode' );
                do_settings_sections( 'wb_minute_testing_mode' );
            }

            submit_button();
            ?>

        </form>
    </div>

    <?php
}

I have the code below to show different settings for each tab. Contents shown depends on the url paratmer action. When I try selecting a tab, wordpress gives me a Sorry, you are not allowed to access this page. error. Please see the code below.

function minutes_settings_content() {
    ?>
    <div class="wrap">

        <div id="icon-themes" class="icon32"></div>
        <h2>Email & SMS Settings</h2>
        <?php settings_errors(); ?>

        <?php $active_tab = isset( $_GET[ 'action' ] ) ? $_GET[ 'action' ] : 'email_sms_options'; ?>

        <h2 class="nav-tab-wrapper">
            <a href="<?php echo esc_url( add_query_arg( array( 'action' => 'email_sms_options' ), admin_url( 'edit.php?post_type=wellbeing-minutes&page=minutes-email-settings' ) ) ); ?>" class="nav-tab">Email & SMS</a>
            <a href="<?php echo esc_url( add_query_arg( array( 'action' => 'testing_mode_options' ), admin_url( 'edit.php?post_type=wellbeing-minutes&page=minutes-email-settings' ) ) ); ?>" class="nav-tab">Testing Mode</a>
        </h2>

        <form action='options.php' method='post'>

            <?php
            if ( $active_tab == 'email_sms_options' ) {
                settings_fields( 'EmailSmsMinute' );
                do_settings_sections( 'EmailSmsMinute' );
            }
            elseif ( $active_tab == 'testing_mode_options' ) {
                settings_fields( 'wb_minute_testing_mode' );
                do_settings_sections( 'wb_minute_testing_mode' );
            }

            submit_button();
            ?>

        </form>
    </div>

    <?php
}
Share Improve this question asked Aug 13, 2019 at 7:49 marccapsmarccaps 592 silver badges8 bronze badges 2
  • what is the url in the browser after clicking – Makiomar Commented Aug 13, 2019 at 8:23
  • also what is the page you are adding these settings to. the page you added to register_settings – Makiomar Commented Aug 13, 2019 at 9:40
Add a comment  | 

1 Answer 1

Reset to default 1

I've found that there are two causes for this error when developing with the settings API:

  1. you are not running under the 'admin_menu' hook
  2. some other error has occurred, check the error log.

Both of these derive from current_user not being available at the time.

发布评论

评论列表(0)

  1. 暂无评论