1

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">
        <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');
  • So to be more clear I've put the php I'm working on into a Gist: link Everything about the php works fine EXCEPT it will not call the get_option or retrieve the data through $wpdb. – Parkbum Dec 19 '18 at 14:22
0

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 ) );
}
  • Didn't work quite right, but the Serializing is definitely a good point. I messed with my "register_setting" to look more like this:function mfwp_register_settings() { $args = array( 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field', 'default' => NULL, ); register_setting('mfwp_settings_group', 'mfwp_settings', $args); } add_action('admin_init', 'mfwp_register_settings'); But it still isn't firing... I also tried to call it using $wpdb and it's not querying... – Parkbum Dec 18 '18 at 23:39
  • I think there's some missing info here. Please edit your original question and include all of the code necessary to reproduce the issue. There's definitely something fishy with the way your data is stored. I'd suggest deleting the mfwp_settings option in the DB and starting fresh. Consider using the Custom Settings Page example from the docs as a starting point. – Dave Romsey Dec 18 '18 at 23:56
  • 1
    Thanks for the help. I've added a comment to the original with a gist leading to the document I'm working on: link – Parkbum Dec 19 '18 at 14:24
  • Taking a look over the code. You should use $test = get_option( 'mfwp_settings' ); to get all of the options stored for mfwp_settings. You should not be using $wpdb to get settings stored using the settings API. Again, start with the example from the handbook. It works. Then, tweak it to fit your needs. Quick note --in the future, please paste any relevant code directly into the question. You can always edit your original question to add updates, etc. – Dave Romsey Dec 20 '18 at 19:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.