设置字段似乎未运行

时间:2019-10-21 作者:davemackey

我正在为插件创建一个设置页面。它补充道:

子菜单项注册设置添加节,然后呈现页面/节/字段这一切都非常简单,唯一的“复杂性”是在呈现字段时,我获取选项(数组)并将其与get_taxonomies().

菜单项、页面和节都显示良好,但字段没有显示任何内容。我想我可能得到了一个空数组,但我添加了一个echo \'<p>Hello!</p>\' 看看是否调用了渲染函数(lqdnotes_settings_render_field) - 但事实似乎并非如此。

我确信这是一个简单的错误,但我已经盯着这段代码看了太久了。非常感谢您的任何建议!

<?php
/**
 * Plugin Name: Liquid Notes Settings Page
 * Version: 0.0.1
 * Author: Liquid Church, Dave Mackey
 * License: GPLv2 or later
 * Text Domain: lqdnotes
 */

/**
 * Add Submenu Item to Settings Menu
 */
function lqdnotes_add_menu_item() {
    $page_title = \'Liquid Notes Settings\';
    $menu_title = \'Liquid Notes\';
    $capability = \'manage_options\';
    $menu_slug = \'lqdnotes\';
    $settings_page_render = \'lqdnotes_settings_render_page\';

    add_options_page(
        $page_title,
        $menu_title,
        $capability,
        $menu_slug,
        $settings_page_render
    );
}

/**
 * Initialize the setting, section, and field we\'ll use on our Settings page.
 *
 * References:
 * - Register Setting: https://developer.wordpress.org/reference/functions/register_setting/
 * - Adding Section: https://developer.wordpress.org/reference/functions/add_settings_section/
 * - Adding Field: https://developer.wordpress.org/reference/functions/add_settings_field/
 */
function lqdnotes_settings_init() {
    // Register Setting
    $settings_option_group = \'lqdnotes_settings_taxonomies\';
    $settings_option_name = \'lqdnotes_settings_taxonomies\';
    // $args = array( \'type\', \'description\', \'sanitize_callback\', \'show_in_rest\', \'default\' )

    register_setting(
        $settings_option_group,
        $settings_option_name
    );

    // Add Section
    $settings_slug_name = \'lqdnotes_main\';
    $settings_title = \'Liquid Notes Settings\';
    $settings_callback = \'lqdnotes_settings_render_section\';
    $settings_page = \'lqdnotes\';

    add_settings_section(
        $settings_slug_name,
        $settings_title,
        $settings_callback,
        $settings_page
    );

    // Add Field
    $settings_field_slug_name = \'lqdnotes_settings_taxonomies\';
    $settings_title = \'Select taxonomies that should be associated with Liquid Notes.\';
    $settings_field_callback = \'lqdnotes_settings_render_field\';
    // $settings_page
    // $section = \'default\'
    // $args = array( \'label_for\', \'class\' )

    add_settings_field(
        $settings_field_slug_name,
        $settings_title,
        $settings_field_callback,
        $settings_page
    );
}

add_action( \'admin_init\', \'lqdnotes_settings_init\' );

add_action( \'admin_menu\', \'lqdnotes_add_menu_item\' );

/**
 * Render Section
 */
function lqdnotes_settings_render_section() {
    echo \'<p>Settings for Liquid Notes.</p>\';
}

/**
 * Render Field
 */
function lqdnotes_settings_render_field() {
    echo \'<p>Hello!</p>\';
    $selected_taxonomies = get_option( \'lqdnotes_settings_taxonomies\' );
    // https://developer.wordpress.org/reference/functions/get_taxonomies/
    $all_taxonomies = get_taxonomies();

    foreach ( $all_taxonomies as $taxonomy => $tax ) {
        if ( in_array( $tax, $selected_taxonomies ) ) {
            echo \'<input type="checkbox" name="lqdnotes_taxonomies[]" value="\' .
                $tax . \'" checked="checked">\' . $tax->label . \'<br>\';
        } else {
            echo \'<input type="checkbox" name="lqdnotes_taxonomies[]" value="\' . $tax->label . \'<br>\';
        }
    }
}

/**
 * Render Settings Page
 */
function lqdnotes_settings_render_page() {
    ?>
<div class="wrap">
    <form action="options.php" method="post">
        <?php settings_fields( \'lqdnotes_settings_taxonomies\' ); ?>
        <?php do_settings_sections( \'lqdnotes\' ); ?>

        <input name="Submit" type="submit" value="<?php esc_attr_e( \'Save Changes\', \'lqdnotes\' ); ?>"
               class="button button-primary" />
    </form>
</div>
<?php
}

1 个回复
最合适的回答,由SO网友:Daniel Milner 整理而成

您需要将第五个参数添加到add_settings_field() 包含段塞的,$settings_slug_name.

相关推荐

Options表崩溃并修复后无法访问wp-admin

我的选项表崩溃了,我成功地修复了它。但现在当我登录时,它会重定向到没有工具栏的主页,如果我键入/wp admin,我会得到“抱歉,您不允许访问此页面。”我尝试了通常的禁用插件和主题的方法。正在删除。htaccess文件。似乎什么都没用。我确实有一个5天前的DB备份,可以恢复访问,但在这段时间内做了很多更改,所以我希望以不同的方式解决这个问题。