include – 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' ) ) {

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!


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.

// 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 () {
            'My First WordPress Plugin', //title browser
            'MFWP', //menu text
            'manage_options', //require capability **KEEP AS manage_options**
            'mfwp_admin', //reference slug
            '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">
                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 class="submit">
                <input type="submit" class="button-primary" value="<?php _e('Save Option', 'mfwp_domain'); ?>">
    //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');

Dave Romsey 2 years 2018-12-17T18:27:47-05:00 0 Answers 0 views 0

Leave an answer