0

In theme and plugin development I like to completely run through everything to make sure there are no issues and I'm not getting any errors. ONLY in development do I add the following in my footer.php to see what is going on:

if (is_user_logged_in()) :
    // GET POST META
    // https://developer.wordpress.org/reference/functions/get_post_meta/
    global $wp_query;
    $post_meta = get_post_meta($post->ID);
    echo '<h1>Post Meta</h1>';
    echo '<pre>';
    var_dump($post_meta);
    echo '</pre>';
    wp_reset_query();


    // SEE WHAT IS ASSIGNED
    $queried_object = get_queried_object();
    echo '<h1>Quered Object</h1>';
    echo '<pre>';
    var_dump($queried_object);
    echo '</pre>';
    wp_reset_query();
endif;

However since I've been trying to implement more settings pages instead of widgets I'd like to test what I'm getting when I build a settings page so instead of calling each get_option('whatever'); I'd like to dump everything so I can see what is being collected and if any errors arise. However, when I do:

echo '<h1>Get Options</h1>';
echo '<pre>';
var_dump(get_option());
echo '</pre>';
wp_reset_query();

I'm thrown an error. When I research get_option() I do not see anything discussing a dump and I'm not finding a solution when I view . Is there a way I can dump everything in get_option()?

EDIT:

To clarify, this would be useful in setting page development so is there a way to dump the options from the settings? Such as:

function wpse_267354() { 
    add_submenu_page( 
        'options-general.php',
        'Foobar Page', // $page_title
        'PAGE NAME', // $menu_title
        'manage_options', // $capability
        'wpse_267354_slug', // $menu_slug
        'wpse_267354_function' // $function
    ); 
}
add_action( 'admin_menu', 'wpse_267354' );
  • 1
    are you sure about that? this can be thousands of entries if you have somewhere a bad transients code – Mark Kaplun May 18 '17 at 15:18
  • would there be a way to scope it to the settings page menu slug? – DᴀʀᴛʜVᴀᴅᴇʀ May 18 '17 at 15:27
  • 1
    Option names are arbitrary. Since you are the developer of the plugin, you should be aware of all the option names required by the plugin. Make an array of option names [$option_names = array();], loop the array [foreach( $option_names as $option_name )] and [var_dump( get_option( $option_name ) );] for each array item. – Michael Ecklund May 18 '17 at 15:35
  • 2
    in addition to what @MichaelEcklund said, If possible I would just cram all your settings in one option so there is only one option to dump. – Mark Kaplun May 18 '17 at 15:57
2

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.

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.