Get_Option()未从插件返回期望值

时间:2018-12-17 作者:Parkbum

我正在尝试制作我的第一个插件,到目前为止,一切都很好,除了一件令人讨厌的事。我设置了字段并更新了数据,在数据库中看起来很好:

mfwp_settings | a:1:{i:test;s:3:"response";}
但是,当我想在管理面板中回显“test”的值时,它返回为空或NULL。它在前端页面上响应良好,但在插件中,它不会使用以下方式检索数据:

$mfwp_opt = get_option(\'mfwp_settings\')[\'test\'];
同样,上面的代码在前端的普通php中也可以正常运行。因此,我只能假设问题在于插件主PHP上缺少“路径”和/或“包含”。

So.... is there a special path I need to put into the main plugin PHP?

我放了一条ABPATH以防万一,但运气不好:

if ( ! defined( \'ABSPATH\' ) ) {
exit;
}
我认为我可以复制和调整“get\\u option”函数,并在我自己的插件中使用它,但这似乎没有必要。因此,任何帮助都将不胜感激!

Edit

为了更清楚,我把我正在使用的php放在下面。除了不会调用get_option 或通过$wpdb.

<?php
// Initiate Global(s)
global $wpdb;
//TRIED to call normally via get_option, but it wouldn\'t fire
$test = get_option( \'mfwp_settings\' );
//TRIED to call via SQL, but comes back as NULL
$get_settings = $wpdb->get_results ( "
        SELECT * 
        FROM $wpdb->wp_options
        WHERE option_name = mfwp_settings
        LIMIT 1
        " );
$field1 = get_option( mfwp_settings );
// Creating Admin Menu option
add_action(\'admin_menu\', function () {
    add_options_page(
            //CHANGE
            \'My First Wordpress Plugin\', //title browser
            //CHANGE
            \'MFWP\', //menu text
            //KEEP
            \'manage_options\', //require capability **KEEP AS manage_options**
            //CHANGE
            \'mfwp_admin\', //reference slug
            //CHANGE
            \'mfwp_options_page\' //callback to menu body
            );
}
);
// Call Back & create body - CHANGE prefix
function mfwp_options_page() {
    //ob_start(); ?>
    <div class="wrap">
        <h2>My First WordPress Plugin Options</h2>
        <form method="post" action="options.php">
            <?php
                settings_fields( \'mfwp_settings_group\' );
            ?>
            <h4><?php _e(\'Placeholder for Plugin Fields\', \'mfwp_domain\'); ?></h4>
            <p><?php echo $field1 . \' tester<br />\'; echo var_dump($get_settings);?>
                <label class="description" for="mfwp_settings"><?php _e(\'This is a label description\', \'mfwp_domain\'); ?></label>
                <input class="regular-text" id="mfwp_settings" name="mfwp_settings" type="" value="<?php echo $test; ?>"> 
            </p>
            <p class="submit">
                <input type="submit" class="button-primary" value="<?php _e(\'Save Option\', \'mfwp_domain\'); ?>">
            </p>
        </form>
    </div>
    <?php
    //echo ob_get_clean();
}
function mfwp_register_settings() {
        $args = array(
            \'type\' => \'string\', 
            \'sanitize_callback\' => \'sanitize_text_field\', //Sanatizes data
            \'default\' => NULL,
            );
    register_setting(\'mfwp_settings_group\', \'mfwp_settings\', $args);
}
add_action(\'admin_init\', \'mfwp_register_settings\');

1 个回复
SO网友:Dave Romsey

您的序列化设置似乎已损坏。我创建了选项mfwp_settings 具有值array( \'test\' => \'response\' ). 在数据库中查看时,该值存储为序列化数组,如下所示:

a:1:{s:4:"test";s:8:"response";}
下面是一个简单的调试函数,它成功地返回了预期值response 对于test:

add_action( \'init\', \'wpse_option_test\' );
function wpse_option_test() {

    // Example data. Serialized, it will look like this: a:1:{s:4:"test";s:8:"response";}
    $settings_value = array( \'test\' => \'response\' );

    add_option( \'mfwp_settings\' );
    update_option( \'mfwp_settings\', $settings_value );
    $mfwp_settings = get_option( \'mfwp_settings\' )[\'test\'];

    // Output the value for debugging. Result: string(8) "response"
    exit ( var_dump( $mfwp_settings ) );
}