I need to see if an option, and if does, get the value. If not, I need to add it.

The Codex provides:

$option_name = 'myhack_extraction_length' ;
$new_value = '255' ;

if ( get_option( $option_name ) != $new_value ) {
    update_option( $option_name, $new_value );
} else {
    $deprecated = ' ';
    $autoload = 'no';
    add_option( $option_name, $new_value, $deprecated, $autoload );

Which supposedly updates the option name myhack_extraction_length with the value 255. If the option does not exist then use add_option and set autoload to no.

However, it seems to me that the second half could be called in two ways, if the option does not exist OR if the new value==the option

Is this correct?

6 Answers 6


The logic on the IF THEN ELSE does seem a bit wonky. If I'm reading it correctly...

The call to get_option( $option_name ) will return FALSE if the option does not exist or if it has no value.

So the IF would be executed:

  1. when the option doesn't exist and $new_value != FALSE
  2. the option has no value and $new_value != FALSE
  3. the option exists and has a value that is != $new value

The update_option() call would then update the value or create the option with $new_value.

The ELSE would be called if:

  1. the option exists and has the same value as $new_value
  2. the option does not exist and the $new_value == FALSE

I don't recommend to use if (!(get_option("XXXX")) approach to check for existence, as it fails with false/null/0/empty legitimate value.

I suggest:

if (!option_exists("XXXX")) {
     add_option("XXXX", "valueee");


public function option_exists($name, $site_wide=false){
    global $wpdb; return $wpdb->query("SELECT * FROM ". ($site_wide ? $wpdb->base_prefix : $wpdb->prefix). "options WHERE option_name ='$name' LIMIT 1");

(I had also submitted this feature as CORE request)

  • 1
    Please add an explanation to your answer: why could that solve the problem?
    – fuxia
    Commented Mar 13, 2014 at 19:57
  • 1
    When using update_option : "If the option does not exist, then the option will be added with the option value, with an $autoload value of ‘yes’." - developer.wordpress.org/reference/functions/update_option
    – JDandChips
    Commented Feb 25, 2019 at 9:53
  • As I've just commented on the trac there are caches you should use for this too, and you should definitely update the caches after making the database query.
    – Rup
    Commented Jul 28, 2021 at 10:05

I believe that the best approach to this issue is to create IF-ELSE logic like this one:

         update_option('some_option', 'value_we_want_to_add');
    else {
      add_option('some_option', 'value_we_want_to_add');

Logic: get_option() will return TRUE if that option does exist. In this case we can simply update that option with some value we want to add. This is done by update_option('some_option','value_we_want_to_add'). On the other hand, if some_option doesn't exist, get_option will return FALSE and activate ELSE block. Inside else block we gave instruction to create this "some_option" and assign value we want to it.

Optionally, this logic can be changed to match more advanced requirements.


Is this correct?

If you mean "when does the 'if' execute and when does the 'else' execute, then "No". Look at the condition: if ( get_option( $option_name ) != $new_value ) {

If the option does not exist-- false != '255' is true-- then the first part executes. It also executes if the option exists and the values don't match --'123' != '255' is true.

The else executes only if the values do match-- '255' != '255' is false, so the else part runs. They could match on anything, numbers, strings, booleans-- aka true or false


I struggled a bit with these kind of options management and did it with a different approach...

// create or update the token in DB

    switch ( get_option( 'ngitc_token' ) ) {
        case false: 
            echo "<p>Added token to DB</p>";
            delete_option( 'ngitc_token' ); //get_option() will send false if option doesn't exist OR if it's empty. So I take no chances, I delete it before adding it.
            add_option( 'ngitc_token', $token->token );
        case $token->token:
            echo "<p>No change to DB</p>";
        case true:
            echo "<p>Updated token to DB</p>";
            update_option( 'ngitc_token', $token->token );  


@Steve is answering you perfectly, I'm just adding a possibility on how to approach options managing.


Try This

  1. Function checking if $arg / string / exist in mySQL.

  2. $arg must be a string / the name of your option /

  3. If the function returns true -> option exist; returns false -> there is no option with this name.

/** * Check If Option Exist; * @param String * @return boolean; */

function exist_option( $arg ) {

    global $wpdb;
    $prefix = $wpdb->prefix;
    $db_options = $prefix.'options';
    $sql_query = 'SELECT * FROM ' . $db_options . ' WHERE option_name LIKE "' . $arg . '"';

    $results = $wpdb->get_results( $sql_query, OBJECT );

    if ( count( $results ) === 0 ) {
        return false;
    } else {
        return true;


var_dump( exist_option( 'your_option' ) );


true / false

Example: ( create option if not exist ):

if( exist_option( 'your_option' ) == false ) {
    add_option( 'your_option', '255', '', true );

And your answer may be is:

$new_value = '255';

if( exist_option( 'myhack_extraction_length' ) == false ) {
    add_option( 'myhack_extraction_length', '255', '', true );
} else {
    if ( get_option( 'myhack_extraction_length' ) == $new_value ) {
        // Your Code Here: or
    } else {
        update_option( 'myhack_extraction_length', $new_value );
  • 2
    Please do not just dump code, but explain what you are doing and what it is good for - thanks. Commented Jan 20, 2016 at 15:06
  • 2
    downvoted as there is no reason at all to directly query the DB. In any case update_option will add the option if it do not exist so the whole code is pointless. Commented Jan 21, 2016 at 13:51

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.