How to See Everything in get_option()?

You need a way of managing your settings. You can use whatever methods of management as you see fit. However, I’ve came up with a fairly “simple” Settings PHP Class, which you can use to retrieve your Plugin’s Settings as an Object with Properties.

The Object is your Plugin’s Settings. The Properties of the Object are what contain the actual settings themselves.

It’s easy to customize and expand off. Hopefully this helps you to easier maintain your plugin(s) and/or theme(s).

This class will:

  1. Help you to better understand the bridge between what the plugin
    does by default, and what the user wants the plugin to do.
  2. Help you to better understand how your plugin works, and lessen the
    chance of unexpected errors related to settings management.
  3. Help you to consistently retrieve all settings or
    specific settings in a structured way.

Example of Plugin Settings Class:

<?php

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

if ( class_exists( 'MBE_Settings' ) ) {
    return false;
}

/**
 * This is an example Settings Class for managing Plugin Settings.
 *
 * @author Michael Ecklund
 * @author_url https://www.michaelbrentecklund.com
 *
 * Class MBE_Settings
 */
class MBE_Settings {

    /**
     * @var array
     */
    public $general = array();

    /**
     * @var array
     */
    public $display = array();

    /**
     * @var array
     */
    private $default_settings = array();

    /**
     * @var array
     */
    private $user_settings = array();

    /**
     * @var array
     */
    private $settings = array();

    /**
     * Settings constructor.
     *
     * @author Michael Ecklund
     * @author_url https://www.michaelbrentecklund.com
     *
     */
    public function __construct() {

        $this->set_default_settings();
        $this->set_user_settings();

        if ( ! $this->set_settings() ) {

            $this->__destruct();

            return false;

        }

        return true;

    }

    /**
     * Sets the default plugin settings in the event that the user hasn't configured the plugin.
     *
     * @author Michael Ecklund
     * @author_url https://www.michaelbrentecklund.com
     *
     * @return bool
     */
    private function set_default_settings() {

        $default_settings = array();

        # General Settings
        $default_settings['general'] = array(
            'posts_per_page' => 10,
            'description'    => 'Example Description'
        );

        # Display Settings
        $default_settings['display'] = array(
            'icons'        => 0,
            'descriptions' => 1
        );

        $this->default_settings = $default_settings;

        return true;

    }

    /**
     * Sets plugin settings configured by the user.
     *
     * @author Michael Ecklund
     * @author_url https://www.michaelbrentecklund.com
     *
     * @return bool
     */
    private function set_user_settings() {

        $user_settings            = array();
        $user_settings['general'] = get_option( 'mbe-general-settings' ); // General Settings
        $user_settings['display'] = get_option( 'mbe-display-settings' ); // Display Settings

        $this->user_settings = array_filter( $user_settings );

        return true;

    }

    /**
     * Merges default settings with user configured settings, producing final settings to be used by the plugin.
     *
     * @author Michael Ecklund
     * @author_url https://www.michaelbrentecklund.com
     *
     * @return bool
     */
    private function set_settings() {

        $default_settings = $this->get_default_settings();

        if ( $user_settings = $this->get_user_settings() ) {

            $settings['general'] = wp_parse_args( $user_settings['general'], $default_settings['general'] );
            $settings['display'] = wp_parse_args( $user_settings['display'], $default_settings['display'] );

        } else {
            $settings = $default_settings;
        }

        $this->settings = $settings;

        # General Settings
        if ( isset( $settings['general'] ) ) {
            $this->general = $settings['general'];
        }

        # Display Settings
        if ( isset( $settings['display'] ) ) {
            $this->display = $settings['display'];
        }

        return true;

    }

    /**
     * Retrieve default plugin settings.
     *
     * @author Michael Ecklund
     * @author_url https://www.michaelbrentecklund.com
     *
     * @return array
     */
    public function get_default_settings() {
        return $this->default_settings;
    }

    /**
     * Retrieve plugin settings configured by the user.
     *
     * @author Michael Ecklund
     * @author_url https://www.michaelbrentecklund.com
     *
     * @return array
     */
    public function get_user_settings() {
        return $this->user_settings;
    }

    /**
     * Retrieve all plugin settings or specific plugin settings.
     *
     * @author Michael Ecklund
     * @author_url https://www.michaelbrentecklund.com
     *
     * @param String|null $group
     *
     * @return array|bool
     */
    public function get_settings( String $group = null ) {

        if ( ! is_null( $group ) ) {

            if ( $group == 'default' ) {
                return $this->get_default_settings();
            } else {

                $group = str_replace( '-', '_', $group );

                if ( ! property_exists( $this, $group ) ) {
                    return false;
                }

                return $this->{$group};

            }

        }

        return $this->settings;

    }

    /**
     * Destroys the object in the event of failure.
     *
     * @author Michael Ecklund
     * @author_url https://www.michaelbrentecklund.com
     *
     */
    public function __destruct() {

        $this->general = null;
        $this->display = null;

        unset( $this->default_settings );
        unset( $this->user_settings );

    }

}


/**
 * Wrapper function to retrieve Plugin Settings from Settings Class.
 *
 * @author Michael Ecklund
 * @author_url https://www.michaelbrentecklund.com
 *
 * @param String|null $group
 *
 * @return array|bool
 */
function mbe_get_settings( String $group = null ) {

    $settings = new MBE_Settings();

    return $settings->get_settings( $group );

}


?>

To customize this, you would set each property as you see fit. Think of the property as a “Group Name” or “Section Name”. Then adjust accordingly for: set_default_settings();, set_user_settings();, and set_settings();.

Note: This class only handles retrieving plugin settings. This class doesn’t handle updating the plugin settings.