设置API子菜单和数据未填充

时间:2017-09-01 作者:The WP Intermediate

在函数中。PHP→

locate_template( \'inc/admin/class.settings-api.php\', true );
locate_template( \'inc/admin/o-functions.php\', true );
o功能。php代码:
<?php

/**
* Registers settings section and fields
*/
function wedevs_admin_init() {

    $sections = array(
        array(
            \'id\'    => \'wedevs_basics\',
            \'title\' => __( \'Basic Settings\', \'wedevs\' )
        ),
        array(
            \'id\'    => \'wedevs_advanced\',
            \'title\' => __( \'Advanced Settings\', \'wedevs\' )
        ),
        array(
            \'id\'    => \'wedevs_others\',
            \'title\' => __( \'Other Settings\', \'wpuf\' )
        )
    );

    $fields = array(
        \'wedevs_basics\' => array(
            array(
                \'name\' => \'text\',
                \'label\' => __(\'Text Input\', \'wedevs\'),
                \'desc\' => __(\'Text input description\', \'wedevs\'),
                \'type\' => \'text\',
                \'default\' => \'Title\'
            ),
            array(
                \'name\' => \'textarea\',
                \'label\' => __(\'Textarea Input\', \'wedevs\'),
                \'desc\' => __(\'Textarea description\', \'wedevs\'),
                \'type\' => \'textarea\'
            ),
            array(
                \'name\' => \'checkbox\',
                \'label\' => __(\'Checkbox\', \'wedevs\'),
                \'desc\' => __(\'Checkbox Label\', \'wedevs\'),
                \'type\' => \'checkbox\'
            ),
            array(
                \'name\' => \'radio\',
                \'label\' => __(\'Radio Button\', \'wedevs\'),
                \'desc\' => __(\'A radio button\', \'wedevs\'),
                \'type\' => \'radio\',
                \'options\' => array(
                    \'yes\' => \'Yes\',
                    \'no\' => \'No\'
                )
            ),
            array(
                \'name\' => \'multicheck\',
                \'label\' => __(\'Multile checkbox\', \'wedevs\'),
                \'desc\' => __(\'Multi checkbox description\', \'wedevs\'),
                \'type\' => \'multicheck\',
                \'options\' => array(
                    \'one\' => \'One\',
                    \'two\' => \'Two\',
                    \'three\' => \'Three\',
                    \'four\' => \'Four\'
                )
            ),
            array(
                \'name\' => \'selectbox\',
                \'label\' => __(\'A Dropdown\', \'wedevs\'),
                \'desc\' => __(\'Dropdown description\', \'wedevs\'),
                \'type\' => \'select\',
                \'default\' => \'no\',
                \'options\' => array(
                    \'yes\' => \'Yes\',
                    \'no\' => \'No\'
                )
            )
        ),
        \'wedevs_advanced\' => array(
            array(
                \'name\' => \'text\',
                \'label\' => __(\'Text Input\', \'wedevs\'),
                \'desc\' => __(\'Text input description\', \'wedevs\'),
                \'type\' => \'text\',
                \'default\' => \'Title\'
            ),
            array(
                \'name\' => \'textarea\',
                \'label\' => __(\'Textarea Input\', \'wedevs\'),
                \'desc\' => __(\'Textarea description\', \'wedevs\'),
                \'type\' => \'textarea\'
            ),
            array(
                \'name\' => \'checkbox\',
                \'label\' => __(\'Checkbox\', \'wedevs\'),
                \'desc\' => __(\'Checkbox Label\', \'wedevs\'),
                \'type\' => \'checkbox\'
            ),
            array(
                \'name\' => \'radio\',
                \'label\' => __(\'Radio Button\', \'wedevs\'),
                \'desc\' => __(\'A radio button\', \'wedevs\'),
                \'type\' => \'radio\',
                \'default\' => \'no\',
                \'options\' => array(
                    \'yes\' => \'Yes\',
                    \'no\' => \'No\'
                )
            ),
            array(
                \'name\' => \'multicheck\',
                \'label\' => __(\'Multile checkbox\', \'wedevs\'),
                \'desc\' => __(\'Multi checkbox description\', \'wedevs\'),
                \'type\' => \'multicheck\',
                \'default\' => array(\'one\' => \'one\', \'four\' => \'four\'),
                \'options\' => array(
                    \'one\' => \'One\',
                    \'two\' => \'Two\',
                    \'three\' => \'Three\',
                    \'four\' => \'Four\'
                )
            ),
            array(
                \'name\' => \'selectbox\',
                \'label\' => __(\'A Dropdown\', \'wedevs\'),
                \'desc\' => __(\'Dropdown description\', \'wedevs\'),
                \'type\' => \'select\',
                \'options\' => array(
                    \'yes\' => \'Yes\',
                    \'no\' => \'No\'
                )
            )
        ),
        \'wedevs_others\' => array(
            array(
                \'name\' => \'text\',
                \'label\' => __(\'Text Input\', \'wedevs\'),
                \'desc\' => __(\'Text input description\', \'wedevs\'),
                \'type\' => \'text\',
                \'default\' => \'Title\'
            ),
            array(
                \'name\' => \'textarea\',
                \'label\' => __(\'Textarea Input\', \'wedevs\'),
                \'desc\' => __(\'Textarea description\', \'wedevs\'),
                \'type\' => \'textarea\'
            ),
            array(
                \'name\' => \'checkbox\',
                \'label\' => __(\'Checkbox\', \'wedevs\'),
                \'desc\' => __(\'Checkbox Label\', \'wedevs\'),
                \'type\' => \'checkbox\'
            ),
            array(
                \'name\' => \'radio\',
                \'label\' => __(\'Radio Button\', \'wedevs\'),
                \'desc\' => __(\'A radio button\', \'wedevs\'),
                \'type\' => \'radio\',
                \'options\' => array(
                    \'yes\' => \'Yes\',
                    \'no\' => \'No\'
                )
            ),
            array(
                \'name\' => \'multicheck\',
                \'label\' => __(\'Multile checkbox\', \'wedevs\'),
                \'desc\' => __(\'Multi checkbox description\', \'wedevs\'),
                \'type\' => \'multicheck\',
                \'options\' => array(
                    \'one\' => \'One\',
                    \'two\' => \'Two\',
                    \'three\' => \'Three\',
                    \'four\' => \'Four\'
                )
            ),
            array(
                \'name\' => \'selectbox\',
                \'label\' => __(\'A Dropdown\', \'wedevs\'),
                \'desc\' => __(\'Dropdown description\', \'wedevs\'),
                \'type\' => \'select\',
                \'options\' => array(
                    \'yes\' => \'Yes\',
                    \'no\' => \'No\'
                )
            )
        )
    );

    $settings_api = new WeDevs_Settings_API();

    //set sections and fields
    $settings_api->set_sections( $sections );
    $settings_api->set_fields( $fields );

    //initialize them
    $settings_api->admin_init();
}

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


/**
* Register the plugin page
*/
function wedevs_admin_menu() {
    add_options_page( \'Settings API\', \'Settings API\', \'delete_posts\', \'settings_api_test\', \'wedevs_plugin_page\' );
}

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


/**
* Display the plugin settings options page
*/
function wedevs_plugin_page() {
    $settings_api = new WeDevs_Settings_API();

    echo \'<div class="wrap">\';
    settings_errors();

    $settings_api->show_navigation();
    $settings_api->show_forms();

    echo \'</div>\';
}

/**
* Get the value of a settings field
*
* @param string $option settings field name
* @param string $section the section name this field belongs to
* @param string $default default text if it\'s not found
* @return mixed
*/
function my_get_option( $option, $section, $default = \'\' ) {

    $options = get_option( $section );

    if ( isset( $options[$option] ) ) {
    return $options[$option];
    }

    return $default;
}

Code for class.settings-api.php

但我认为子菜单没有注册,这就是后端没有填充任何内容的原因。看这里→enter image description here

解决方法是什么?

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

我对WeDevs的事情一无所知,但我对设置API本身有点了解。

尽管如此,我还是有一个不确定的答案。在您的wedevs_plugin_page() 函数,执行$settings_api 对象需要显示哪个设置部分的参数?喜欢$settings_api->show_navigation(\'wedevs_basic\');?

或者,对象实例化本身可能需要一个参数?喜欢$settings_api = new WeDevs_Settings_API(\'wedevs_basic\');?

同样,我不知道WeDevs框架,但在admin\\u init函数期间创建的WeDevs\\u Settings\\u API对象稍后可以与在options page函数中创建的另一个WeDevs\\u Settings\\u API对象通信,这似乎很奇怪。除非$settings_api->admin_init(); 做一些很酷的事情来促进交流,只是这里似乎缺少了一些东西。

EDIT:

好的,我认为你不应该使用设置API插件(比如不要激活它),而是应该在你自己的主题中使用它作为你自己代码的示例。我想这就是作者所说的“这个插件是为开发人员而不是一般用户设计的”

无论如何,去上课吧。设置api。php文件,并将其包含在您的主题中(我认为您已经在这样做了)。不要更改此文件中的任何内容。

然后在o函数中。php文件,只需去掉所有程序性的东西并使用oop示例即可。改为php文件。比如从该文件中复制代码并将其粘贴到o函数中。php文件。

现在编辑o形函数。php文件,以满足您的需要。您应该做的第一件事是将类的名称从“WeDevs\\u Settings\\u API\\u Test”更改为对主题更具描述性的名称。不要忘记更改条件中的类名-if ( !class_exists(\'WeDevs_Settings_API_Test\' ) ):

然后更改的参数add_options_page( \'Settings API\', \'Settings API\', \'delete_posts\', \'settings_api_test\', array($this, \'plugin_page\') );

您需要更改第一个、第二个和第四个参数,这些参数分别是页面标题、菜单标题和菜单段塞。特别是slug必须是独一无二的。

然后可以根据需要更改节和字段。

所以,只要您从oop示例中复制代码,就可以快速回顾一下。php,您需要更改类的名称,更改add_options_page(), 更改中的节名称function get_settings_sections(), 并更改中的字段数据function get_settings_fields(). 请勿触摸此代码中的任何其他内容。

现在,在您使用的位置之后,使用新类实例化一个对象locate_template(), 像这样:

locate_template( \'class.settings-api.php\', true );
locate_template( \'o-functions.php\', true );
new My_Settings_API();
请注意,当我在o函数中重命名该类时。php,我称之为“My\\u Settings\\u API”。所以你需要使用你给它起的任何名字。

结束

相关推荐

Order Admin sub-menu items?

我正在使用向CPT管理菜单添加项目add_submenu_page 这很好,但它们被添加到CPT选项后的子菜单底部。我希望能够将它们放在最上面,但我想这个问题也可以应用于订购所有基于管理员的子菜单项。我所尝试的(不起作用,我尝试了几种变体),function custom_menu_order($menu_ord) { if (!$menu_ord) return true; return array( \'edi