get_option() not returning expected value from plugin

I am trying to make my first plugin, and so far everything has been fine except one little annoying thing. I have my fields setup and updating data and looks just fine in my database:

mfwp_settings | a:1:{i:test;s:3:"response";}

But when I want to echo the value for 'test' in my Admin Panel it comes back blank or NULL. It echos fine on a front end page but within the the plugin it wont retrieve the data using the following:

$mfwp_opt = get_option('mfwp_settings')['test'];

Again, the above fires fine in a normal php on the front end. So I can only assume the problem is with a missing 'path' and/or 'includes' on the Plugin's main PHP.

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

I put in an ABPATH just in case that was it, but no luck:

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

I've thought that I could copy and adjust the 'get_option' function and use it in my own plugin, but that seems unnecessary. So any help would be appreciated!

Edit

So to be more clear I've put the php I'm working below. Everything about the php works fine EXCEPT it will not call the get_option or retrieve the data through $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"
        h2My 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 . ' testerbr /'; 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');

Topic include plugin-development options Wordpress

Category Web


It looks like your serialized settings are corrupted. I created the option mfwp_settings with the value array( 'test' => 'response' ). When I look at this in the database, the value is stored as a serialized array and looks like this:

a:1:{s:4:"test";s:8:"response";}

Here is a simple debugging function that successfully returns the expected value response for 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 ) );
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.