add_option [ WordPress Function ]
| Parameters: |
|
| Uses: | |
| Returns: |
|
| Defined at: |
|
Add a new option.
You do not need to serialize values. If the value needs to be serialized, then it will be serialized before it is inserted into the database. Remember, resources can not be serialized or added as an option.
You can create options without values and then update the values later. Existing options will not be updated and checks are performed to ensure that you aren't adding a protected WordPress option. Care should be taken to not name options the same as the ones which are protected.
Source
<?php
function add_option( $option, $value = '', $deprecated = '', $autoload = 'yes' ) {
global $wpdb;
if ( !empty( $deprecated ) )
_deprecated_argument( __FUNCTION__, '2.3' );
$option = trim($option);
if ( empty($option) )
return false;
wp_protect_special_option( $option );
if ( is_object($value) )
$value = clone $value;
$value = sanitize_option( $option, $value );
// Make sure the option doesn't already exist. We can check the 'notoptions' cache before we ask for a db query
$notoptions = wp_cache_get( 'notoptions', 'options' );
if ( !is_array( $notoptions ) || !isset( $notoptions[$option] ) )
if ( false !== get_option( $option ) )
return false;
$_value = $value;
$value = maybe_serialize( $value );
$autoload = ( 'no' === $autoload ) ? 'no' : 'yes';
do_action( 'add_option', $option, $_value );
if ( ! defined( 'WP_INSTALLING' ) ) {
if ( 'yes' == $autoload ) {
$alloptions = wp_load_alloptions();
$alloptions[$option] = $value;
wp_cache_set( 'alloptions', $alloptions, 'options' );
} else {
wp_cache_set( $option, $value, 'options' );
}
}
// This option exists now
$notoptions = wp_cache_get( 'notoptions', 'options' ); // yes, again... we need it to be fresh
if ( is_array( $notoptions ) && isset( $notoptions[$option] ) ) {
unset( $notoptions[$option] );
wp_cache_set( 'notoptions', $notoptions, 'options' );
}
$result = $wpdb->query( $wpdb->prepare( "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)", $option, $value, $autoload ) );
if ( $result ) {
do_action( "add_option_{$option}", $option, $_value );
do_action( 'added_option', $option, $_value );
return true;
}
return false;
}
?>
Examples [ wp-snippets.com ]
Google Arama Sonuçlarý
- add_option - WordPress Codex
Description. A safe way of adding a named option/value pair to the options database table. It does nothing if the option already exists. After the option is saved, ...
codex.wordpress.org - Function Reference/update option « WordPress Codex
This function may be used in place of add_option, although it is not as flexible. update_option will check to see if the option already exists. If it does not, it will be ...
codex.wordpress.org - optparse - Python v2.7.3 documentation
The option strings passed to add_option() are effectively labels for the option defined by that call. For brevity, we will frequently refer to encountering an option ...
docs.python.org - Call to undefined function: add_option() « bbPress Support Forums
I'm using bbPress RC2 integrated with WPMU 2.7.1 (cookie sharing & user table sharing). I recently updated from RC1 to RC2 and since then I ...
bbpress.org
Kullanýcý Tartýþmalarý [ wordpress.org ]
- munkykisser on "[Plugin: WP YouTube Player] Love the player and it works great but I get a lot of errors"
- Nordvind on "add_option not working"
- mamikel on "add_option not working"
- krauter on "What is the maximum size of data for the add_option () function?"
- krauter on "What is the maximum size of data for the add_option () function?"
- hznmoodi on "add/update option not working with Wordpress 3.1"
- sparkweb on "add/delete/update_option() not working in activation hook?"
- Tim Priebe on "Trying to use add_option to add an array, and get_option to access it"
- richarduk on "Trying to use add_option to add an array, and get_option to access it"
- Tim Priebe on "Trying to use add_option to add an array, and get_option to access it"