hooks - Best possible way to get all options

admin2025-01-07  3

I need to provide a list of all options set via plugins etc to remote calls. There is a plugin WP API Options but it hardly does the job.

There are two possible ways (that I know of) to get a list of all options set in the blog/site:

  • Use $wpdb to run a query like so:

    $option_names = $wpdb->get_col( "SELECT DISTINCT option_name FROM $wpdb->options WHERE option_name NOT LIKE '_transient_%'" );

  • Use wp_load_alloptions()

Which one of these two would be the best way to retrieve all the options? Is there a cleaner way than either of these two?

Edit: In a perfect world, I would like to use an inbuilt function for this, instead of writing MySql queries. Also, by all options I am implying any option set by a plugin using add_option or update_option functions.

I need to provide a list of all options set via plugins etc to remote calls. There is a plugin WP API Options but it hardly does the job.

There are two possible ways (that I know of) to get a list of all options set in the blog/site:

  • Use $wpdb to run a query like so:

    $option_names = $wpdb->get_col( "SELECT DISTINCT option_name FROM $wpdb->options WHERE option_name NOT LIKE '_transient_%'" );

  • Use wp_load_alloptions()

Which one of these two would be the best way to retrieve all the options? Is there a cleaner way than either of these two?

Edit: In a perfect world, I would like to use an inbuilt function for this, instead of writing MySql queries. Also, by all options I am implying any option set by a plugin using add_option or update_option functions.

Share Improve this question edited Jun 20, 2016 at 13:57 CandyCoated asked Jun 20, 2016 at 10:47 CandyCoatedCandyCoated 652 silver badges7 bronze badges 4
  • How do you define "best" and "all" ? – birgire Commented Jun 20, 2016 at 11:25
  • Does the edit make sense @birgire? – CandyCoated Commented Jun 20, 2016 at 11:35
  • It's better, because what's "best" can be very opinion-based ;-) It's usually better to include as much relevant information in the question to increase the change of someone pointing you in the right direction. (It looks like you're avoiding transients.. Also it looks like you're not only looking for autoloaded options. There are also add/update hooks available when writing options, if you need a logger.) – birgire Commented Jun 20, 2016 at 11:47
  • The problem with wp_load_alloptions() is it will only return auto loaded options if there are any and not all of them. So you will want to use your own $wpdb query/ – WPExplorer Commented Aug 20, 2024 at 0:01
Add a comment  | 

2 Answers 2

Reset to default 0

I think it is always better to use the API, the functions of WP in this context. The function wp_load_alloptions() is under maintain and a custom select must always tested and maintain. Also the function use the Cache, if the installation support this. A good point for performance. The function supports also the Multisite installation, maybe this also a point for maintain effort.

But the function have no filters to enhance the sql statement or output. That's the point, that you must filter after the returned values.

There is a solution to query all the options that can be set by the plugin, if these are registered settings.

I assume you register the settings as follows:

register_setting( 'plugin-settings-group', 'plugin_option_1' );
register_setting( 'plugin-settings-group', 'plugin_option_2' );

Then you can use the following function:

/**
 * Get all plugin option name
 * @return array
 */
function wp_nq2vjrn_get_all_plugin_option_name() {
    global $wp_registered_settings;

    $plugin_settings = array_filter( $wp_registered_settings, function ( $setting ) {
        return 'plugin-settings-group' == $setting['group'];
    } );

    return array_keys( $plugin_settings );
}

The function returns the names of the options, which are stored in the array id, so I use the array_keys() function.

The big advantage of this type of query is that you can not only query the name of the option, but also access other parameters of the setting, such as type or description. In this case, use return $plugin_settings;, for example:

/**
 * Get all plugin settings
 * @return array
 */
function wp_nq2vjrn_get_all_plugin_settings() {
    global $wp_registered_settings;

    $plugin_settings = array_filter( $wp_registered_settings, function ( $setting ) {
        return 'plugin-settings-group' == $setting['group'];
    } );

    return $plugin_settings;
}
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736258772a539.html

最新回复(0)