0

Good Day everyone,

I'm trying to create an array from data in the Options table of wordpress.

My data all start the same way plugin_order_ and then name of the field information.

Is there a way to extract all those data and create an array with WordPress command?

Or the only way would be to do it with a proper "SQL Query" which I think many people say not to do in WordPress?

1 Answer 1

0
$all_options = wp_load_alloptions();
$plugin_order_options = array();
foreach( $all_options as $name => $value ) {
    if(stristr($name, 'plugin_order_')) $plugin_order_options[$name] = $value;
}
print_r($plugin_order_options);

In plain English:

We load all WP options in $all_options. We copy options from $all_options to $plugin_order_options if their name contains plugin_order_. We print $plugin_order_options. We scratch our head, unhappy with the size of the customized mono-spaced font of our browser. We should have left it default.

As mentioned by @Rarst, wp_autoload_options() will retrieve all autoload options or all options, if no autoloaded ones exist. So, if the above code doesn't work for you, you might want to try this one:

$options = $wpdb->get_results( "SELECT * FROM $wpdb->options ORDER BY option_name" );

foreach ( (array) $options as $option ) :
    if ( $option->option_name == '' )
        continue;
    $name = esc_attr( $option->option_name );
    if(stristr($name, 'plugin_order_'))
        $plugin_order_options[$name] = is_serialized( $option->option_value ) ? 
            maybe_unserialize( $option->option_value ) :
            $option->option_value;
endforeach;

print_r($plugin_order_options);

It basically fetches all entries from wp_options table, and for those who have plugin_order_ in their name it passes the value to our $plugin_order_options array. This time I also check if data is serialized and attempt to unserialize it.

6
  • As usual Thanks Andrei, I will look into that and come back to you!
    – pSyToR
    Feb 1, 2015 at 13:48
  • Please don't just dump code, edit your answer and add some detail how this bit of code may help solve the issue Feb 1, 2015 at 14:32
  • The code is self explanatory, especially since the variable names are descriptive. Have you read the question before criticizing? It asks for a method to obtain an array. There's no debate, no discussion.
    – tao
    Feb 1, 2015 at 15:02
  • So many people on this board are just trying to complain about what people answers. I asked the question, he gave me the code, and it was clear enough that I didn't had to complain... Choooo Trolls Chooooo
    – pSyToR
    Feb 2, 2015 at 3:51
  • Please note that the standards of this site isn't just to "give you code", it's to collect good answers which will be valuable long term to many people. Also the "self explanatory" code is actually not in this case. Despite its name wp_load_alloptions() will not retrieve all options in typical case, but only those that are set to autoload. It might have worked for you, but it's not a completely valid answer for general case.
    – Rarst
    Feb 2, 2015 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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