WordPress设置菜单无法正确显示

时间:2018-10-31 01:37:16

标签: php arrays wordpress

我创建了一个包含标签和一些内容的设置菜单。我没有收到PHP错误,但是在加载页面时,我的设置如下所示:

enter image description here

因此您可以看到我的内容在左上角的标签之外。我绝对不确定这是怎么回事。这是我的代码:


我在这里注册新菜单页面:

    // this should be in a while(true) loop
    if (newPhoneNumber.equals(phoneNumber)) {
        sendSms(phoneNumber);
        String code = JOptionPane.showInputDialog("Enter your code");
        boolean result = validateAuthorizationCode(code); // here you validate the code
       if (result) {
           JOptionPane.showMessageDialog(null, "Logged in!" );
       } else {
           JOptionPane.showMessageDialog(null, "Wrong code!" );
       }
    } else {
        noticeWrongNumber(newPhoneNumber); // tell him the number is wrong, please reinput.
    }

这是上面add_action的About函数:

add_action( 'admin_menu', 'register_settings_submenu' );
function register_settings_submenu() {
    add_submenu_page( 'options-general.php', 'Additional Settings', 'Additional Settings', 'manage_options', 'settings-submenu', 'settings_page' );
}

这是选项卡的功能:

function settings_page() {
    global $settings_active_tab;

    $settings_active_tab = isset( $_GET['tab'] ) ? $_GET['tab'] : 'general'; ?>

    <h2 class="nav-tab-wrapper">
        <?php
        do_action( 'payments_tab' )
        ?>
    </h2>

    <?php

    do_action( 'payments_content' );

}

这是内容的功能:

add_action( 'payments_tab', 'payments_tab', 1 );
function payments_tab() {
    global $settings_active_tab; ?>
    <a class="nav-tab <?php echo $settings_active_tab === 'payments' || '' ? 'nav-tab-active' : ''; ?>"
       href="<?php echo admin_url( 'admin.php?page=settings-submenu&tab=payments' ); ?>"><?php _e( 'Zahlungen ', 'woocommerce' ); ?> </a>
    <?php

}

此函数获取此选项卡的内容并将其初始化:

add_action( 'payments_content', 'payments_content' );
function payments_content() {
    global $settings_active_tab;
    if ( '' || 'payments' !== $settings_active_tab ) {
        return;
    } ?>

    <div class="wrap">
        <h2>Zahlungen</h2>
        <form method="post" action="options.php">
            <table class="form-table">
                <tbody>
                <?php
                settings_fields( "payments" );
                do_settings_sections( "payment-options" );
                ?>
                </tbody>
            </table>
            <?php submit_button(); ?>
        </form>
    </div>
<?php }

这是Payment_settings_element函数:

add_action( "admin_init", "display_settings_pages_content" );
function display_settings_pages_content() {

    payment_settings_element();
}

最后是创建新设置元素的函数:

function payment_settings_element() {

    $settings = array(
        'field1' => array(
            'name'     => __( 'Field 1', 'settings' ),
            'id'       => 'wc_tab_field1',
            'type'     => 'text',
            'desc_tip' => __( 'Im a short desc', 'settings' ),
            'desc'     => __( '%', 'settings' )
        ),
        'field2'      => array(
            'name'     => __( 'Field 2', 'settings' ),
            'id'       => 'wc_tab_field2',
            'type'     => 'text',
            'desc_tip' => __( 'Another short desc', 'settings' ),
            'desc'     => __( '$', 'settings' )
        )
    );

    ?>

    <?php
    foreach ( $settings as $key => $setting ) {
        add_settings_field( $key, $setting['name'], createInput( $setting['name'], $setting['id'], $setting['type'], $setting['desc'] ), 'payment-options', 'payments' );
        register_setting( 'payments', $key );
    } ?>

<?php }

我绝对不知道为什么输入在选项卡内容之外,而选项卡内容只是表结构而没有条目。我怎么了?

1 个答案:

答案 0 :(得分:0)

您在此处使用了错误的钩子:

add_action( "admin_init", "display_settings_pages_content" );

尝试一下:

add_action( "payments_content", "display_settings_pages_content" );