未输出插件选项

时间:2020-03-04 作者:Knott

我为wordpress制作了一个插件,添加了两个简单的文本字段选项。这些选项已成功保存在插件选项页面中,但不起作用,它不会输出已保存的选项。

enter image description here

代码如下:

add_action( \'admin_menu\', \'viptips_add_admin_menu\' );
add_action( \'admin_init\', \'viptips_settings_init\' );


function viptips_add_admin_menu(  ) { 
  $icon       = \'dashicons-editor-table\';
    add_menu_page( \'VIP Tips\', \'VIP Tips\', \'manage_options\', \'vip_tips\', \'viptips_options_page\', $icon );

}


function viptips_settings_init(  ) { 

    register_setting( \'pluginPage\', \'viptips_settings\' );

    add_settings_section(
        \'viptips_pluginPage_section\', 
        __( \'Main plugin options\', \'viptips\' ), 
        \'viptips_settings_section_callback\', 
        \'pluginPage\'
    );

    add_settings_field( 
        \'viptips_category_name\', 
        __( \'Category name:\', \'viptips\' ), 
        \'viptips_category_name_render\', 
        \'pluginPage\', 
        \'viptips_pluginPage_section\' 
    );

    add_settings_field( 
        \'viptips_postperpage\', 
        __( \'Number of posts:\', \'viptips\' ), 
        \'viptips_postperpage_render\', 
        \'pluginPage\', 
        \'viptips_pluginPage_section\' 
    );


}


function viptips_category_name_render(  ) { 

    $options = get_option( \'viptips_settings\' );
    ?>
  <input type=\'text\' name=\'viptips_settings[viptips_category_name]\' value=\'<?php echo $options[\'viptips_category_name\']; ?>\'>
  <p class="description" id="tagline-description">Name of the category that contains predictions</p>
    <?php

}


function viptips_postperpage_render(  ) { 

    $options = get_option( \'viptips_settings\' );
    ?>
  <input type=\'number\' name=\'viptips_settings[viptips_postperpage]\' value=\'<?php echo $options[\'viptips_postperpage\']; ?>\'>
  <p class="description" id="tagline-description">Number of posts to be displayed in table.</p>
    <?php

}


function viptips_settings_section_callback(  ) { 

    echo __( \'Don\\\'t forget to use the <strong>[tabscat]</strong> shortcode, in the page you want the tips table to be displayed!*\', \'viptips\' );

}


function viptips_options_page(  ) { 

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

            <h1>VIP Tips</h1>

            <?php
            settings_fields( \'pluginPage\' );
            do_settings_sections( \'pluginPage\' );
            submit_button();
            ?>

        </form>
        <?php

}
要输出保存的选项,我使用:

$category_name = get_option(\'viptips_category_name\');
$postperpage = get_option(\'viptips_postperpage\');


    $args = array(
     \'post_type\' => \'post\',
     \'post_status\' => \'publish\',
     \'category_name\' => $category_name,
     \'post_per_page\' => $postperpage
     );
我哪里出错了?

2 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

Basically, you\'re not using the correct option name.

在您的viptips_settings_init() 函数,则使用名称viptips_settingsregistering the setting:

// The syntax is: register_setting( \'<settings group name>\', \'<database option name>\' )
register_setting( \'pluginPage\', \'viptips_settings\' );
WordPress会用这个viptips_settings 保存选项时(i.e. update_option( \'viptips_settings\', \'<value>\' )) 因此,在从数据库获取选项时,无论该选项是一个值数组还是一个值,都应该使用相同的名称:

具有值数组的输入:

<input type=\'text\' name=\'viptips_settings[viptips_category_name]\' ...>
<input type=\'number\' name=\'viptips_settings[viptips_postperpage]\' ...>

<input type=\'text\' name=\'viptips_settings\' ...>
$category_name 和$postperpage 变量,可以执行以下操作(但请确保实际设置了数组键/项):

$options = (array) get_option( \'viptips_settings\' );

$args = array(
    \'post_type\'     => \'post\',
    \'post_status\'   => \'publish\',
    \'category_name\' => $options[\'viptips_category_name\'],
    \'post_per_page\' => $options[\'viptips_postperpage\']
);
如果您的代码中包含以下内容:

PHP

// Here we\'ve got two different database option names.
register_setting( \'pluginPage\', \'viptips_category_name\' );
register_setting( \'pluginPage\', \'viptips_postperpage\' );

<!-- And here two different input names; both not array -->
<input type=\'text\' name=\'viptips_category_name\' ...>
<input type=\'number\' name=\'viptips_postperpage\' ...>
那么您的代码或此代码可以工作:

$category_name = get_option( \'viptips_category_name\' );
$postperpage = get_option( \'viptips_postperpage\' );
因此,我希望这个(修改后的)答案对你(更多)和其他人也有帮助。:)

SO网友:Chetan Vaghela

你必须使用viptips_settings 获取值的选项。它将返回数组,您可以使用下面的代码获得这两个值。

$options = get_option(\'viptips_settings\');
$category_name = $options[\'viptips_category_name\'];
$postperpage = $options[\'viptips_postperpage\'];


    $args = array(
     \'post_type\' => \'post\',
     \'post_status\' => \'publish\',
     \'category_name\' => $category_name,
     \'post_per_page\' => $postperpage
     );

相关推荐

Testing Plugins for Multisite

我最近发布了一个WordPress插件,它在单个站点上非常有效。我被告知该插件在多站点安装上不能正常工作,我理解其中的一些原因。我已经更新了代码,现在需要一种方法来测试更新后的代码,然后才能转到实时客户的多站点安装。我有一个用于测试的WordPress安装程序的单站点安装,但需要在多站点安装上进行测试。根据我所能找到的唯一方法是在网络上至少有两个站点来安装整个多站点安装,以测试我的插件。设置WordPress的整个多站点安装是插件开发人员的唯一/首选方式,还是有更快的测试环境可用。