Can I use a space in an option name/key?

Is there any reason at all that I can’t use a space in a WP option name/key? Ex.

update_option( 'my option name', 'abc' );

Read More

I can’t find anything that states I can’t, but every example uses an underscore instead. I want to know if using a space will actually break anything now or in the future.

Related posts

3 comments

  1. Other than general WordPress practice (http://make.wordpress.org/core/handbook/coding-standards/php/) which encourages spaces to always be replaced with – in file names and _ for other purposes, there isn’t a set reason that you can’t use spaces in an option name.

    From my tests, there is no technical reason to limit you from using a space.

    (edit) looking further, the SQL is:
    $row = $wpdb->get_row( $wpdb->prepare( “SELECT autoload FROM $wpdb->options WHERE option_name = %s”, $option ) );

    so any escaped value should work but just to be safe, you should always use esc_sql() as it doesn’t do it specifically.

  2. You can use a space, but it is a little bit annoying to select this key per keyboard. Normally, you can use Ctrl + Shift + Arrow to select a string. Hyphens and spaces are stop points for this kind of selection.

  3. You can actually use any characters in the settings (except maybe quotes).
    These are all valid option names:

    "my option name"
    "my:option name"
    "MY->OPTION_NAME"
    "My/Option/Name"
    "my-option-name"
    

    Important Limitations

    1. Option-Names must be max 64 characters long!

    If you specify a longer name, the option name will be truncated to fit 64 characters in DB (not in code). So this makes it impossible to retrieve the option value again. Always ensure your option name is max 64 characters long.

    1. Option-Names are case insensitive!

    If you save an option APIKEY you can also access it via apikey

    1. Take care to not accidentally use WP core option names! You should always prefix your option names.

    My tipp

    Create a small function that you use to prefix/sanitize your option keys in every plugin/theme, which takes care of the restrictions for you, like this here:

    // Makes sure that all option names have same structure.
    function sanitize_option_key( $name ) {
        $prefix = 'my_';
        // Basic sanitation: Trim and make option name lower case.
        $name = strtolower( trim( $name ) );
        // Ensure the option does not contain spaces
        $name = str_replace( array( '-', ' ' ), '_', $name ); 
        // Prefix prevents any collisions with WP
        $name = $prefix . $name;
        // Also check the max-length
        if ( strlen( $name ) > 64 ) { 
            error_log( 'WARNING - Option name is too long and was truncated: ' . $name );
            $name = substr( $name, 0, 64 ); 
        }
        return $name;
    }
    
    // Using this function:
    
    update_option( sanitize_option_key( 'option value' ), 'test' );
    // option is saved in DB as 'my_option_value'
    
    echo get_option( sanitize_option_key( 'Option_Value' ) ); 
    // will output 'test';
    

Comments are closed.