WordPress Plugin Development: Is there a meaning to pass an empty array to the second parameter of get_option()?
-
I've seen codes passing an empty array to the second parameter of the get_option() function. $data = get_option( 'option_name' , array() ); Is it same as casting to array? Or does it have a special meaning? $data = ( array ) get_option( 'option_name' ); This is the entire get_option() function definition. /** * Retrieve option value based on name of option. * * If the option does not exist or does not have a value, then the return value * will be false. This is useful to check whether you need to install an option * and is commonly used during installation of plugin options and to test * whether upgrading is required. * * If the option was serialized then it will be unserialized when it is returned. * * @since 1.5.0 * @package WordPress * @subpackage Option * @uses apply_filters() Calls 'pre_option_$option' before checking the option. * Any value other than false will "short-circuit" the retrieval of the option * and return the returned value. You should not try to override special options, * but you will not be prevented from doing so. * @uses apply_filters() Calls 'option_$option', after checking the option, with * the option value. * * @param string $option Name of option to retrieve. Expected to not be SQL-escaped. * @param mixed $default Optional. Default value to return if the option does not exist. * @return mixed Value set for the option. */ function get_option( $option, $default = false ) { global $wpdb; $option = trim( $option ); if ( empty( $option ) ) return false; // Allow plugins to short-circuit options. $pre = apply_filters( 'pre_option_' . $option, false ); if ( false !== $pre ) return $pre; if ( defined( 'WP_SETUP_CONFIG' ) ) return false; if ( ! defined( 'WP_INSTALLING' ) ) { // prevent non-existent options from triggering multiple queries $notoptions = wp_cache_get( 'notoptions', 'options' ); if ( isset( $notoptions[$option] ) ) return apply_filters( 'default_option_' . $option, $default ); $alloptions = wp_load_alloptions(); if ( isset( $alloptions[$option] ) ) { $value = $alloptions[$option]; } else { $value = wp_cache_get( $option, 'options' ); if ( false === $value ) { $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); // Has to be get_row instead of get_var because of funkiness with 0, false, null values if ( is_object( $row ) ) { $value = $row->option_value; wp_cache_add( $option, $value, 'options' ); } else { // option does not exist, so we must cache its non-existence $notoptions[$option] = true; wp_cache_set( 'notoptions', $notoptions, 'options' ); return apply_filters( 'default_option_' . $option, $default ); } } } } else { $suppress = $wpdb->suppress_errors(); $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ); $wpdb->suppress_errors( $suppress ); if ( is_object( $row ) ) $value = $row->option_value; else return apply_filters( 'default_option_' . $option, $default ); } // If home is not set use siteurl. if ( 'home' == $option && '' == $value ) return get_option( 'siteurl' ); if ( in_array( $option, array('siteurl', 'home', 'category_base', 'tag_base') ) ) $value = untrailingslashit( $value ); return apply_filters( 'option_' . $option, maybe_unserialize( $value ) ); }
-
Answer:
The second parameter is the default should there be nothing stored in the database. The empty array would be returned if the option was not found allowing for easy testing assuming the option would otherwise have been saved as an array.
Chris Wiegman at Quora Visit the source
Related Q & A:
- Settings options not showing up on Sub Menu page in WordPress plugin?Best solution by WordPress
- how to pass data from table view to second view controller?Best solution by Stack Overflow
- How to open a file in external editor programmatically in eclipse plugin development?Best solution by Stack Overflow
- What would be a good 4-hour development task for a job interview?Best solution by Programmers
- Can the disneyland summer pass be used towards a new disneyland pass?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.