devinsays/options-framework-theme

of_get_option( 'my_field' ) always returns false

Closed this issue · 4 comments

In this part:

if ( '' == $option_name ) {
    $option_name = get_option( 'stylesheet' );
    $option_name = preg_replace( "/\W/", "_", strtolower( $name ) ); // this is the name of the field of array options not the option field saved in the database
}

Change to $option_name and get work.

Did you read through this ticket? #179

Oh thanks, but if you remove this function from the theme

function optionsframework_option_name() {
       // Change this to use your theme slug
       return 'options-framework-theme';
}

of_get_option( 'my_field' ) always returns false as you pass the field name of the array data and not the name of the theme you get to be saved in the database with the name of the option.

for example, I have this code:

$body_font = of_get_option( 'body_font' );

This return false, because 'body_font' is a field from the serialize data.

See the run:

function of_get_option( 'body_font', $default = false ) {
    $option_name = '';

    // I remove this function in my theme because I want the name of the option is the same of the  theme.
    if ( function_exists( 'optionsframework_option_name' ) ) {
        $option_name = optionsframework_option_name();
    }

   // Fallback option name
   // this is true optionsframework_option_name() don't exists
   if ( '' == $option_name ) {
       $option_name = get_option( 'stylesheet' ); // my-theme-slug
       $option_name = preg_replace( "/\W/", "_", strtolower( 'body_font' ) ); // this is the error
    }

     // Get option settings from database
     $options = get_option( 'body_font'  ); // this is field has option name don't exists
     // Return specific option
     if ( isset( $options[$name] ) ) {
         return $options[$name];
     }

    return $default; // false
}

My English is bad, and I hope you understood.

Got it.

$option_name = preg_replace( "/\W/", "_", strtolower( $name ) );

Needed to be replaced with:

$option_name = preg_replace( "/\W/", "_", strtolower( $option_name ) );

Test it again and let me know if it is working for you.

Work perfect thanks.