5 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • United States
solotechno says

OK, I got it to work. So apparently the OptionTree plugin “array” always sets the user’s selection to index zero. To get the code to work by color selection I did this and it seems to be working now.

function sltn_theme_color_option() {
    if( function_exists( 'get_option_tree' )) {
    $color = get_option_tree( 'color_theme_option', '', false, true, -1 );
        if ( $color[0] == 'Avocado' ) {
            echo wp_enqueue_style( 'avocado', get_bloginfo( 'stylesheet_directory' ) . '/styles/avocado.css', false, '', 'all' );
        } elseif ( $color[0] == 'Pink' ) {
            echo    wp_enqueue_style( 'pink', get_bloginfo( 'stylesheet_directory' ) . '/styles/pink.css', false, '', 'all' );
        } else {
            return; //Just show the default style
        }
    }
}
add_action( 'wp_print_styles', 'sltn_theme_color_option' );

Thanks for all the help and suggestions. Special thanks to Parker for the print_r() tip, going to file that away for later use.

Regards, Michael

3256 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Exclusive Author
  • Interviewed on the Envato Notes blog
  • Beta Tester
  • Author had a File in an Envato Bundle
  • Author had a Free File of the Month
+4 more
ParkerAndKent says
solotechno said
OK, I got it to work. So apparently the OptionTree plugin “array” always sets the user’s selection to index zero. To get the code to work by color selection I did this and it seems to be working now.
function sltn_theme_color_option() {
    if( function_exists( 'get_option_tree' )) {
    $color = get_option_tree( 'color_theme_option', '', false, true, -1 );
        if ( $color[0] == 'Avocado' ) {
            echo wp_enqueue_style( 'avocado', get_bloginfo( 'stylesheet_directory' ) . '/styles/avocado.css', false, '', 'all' );
        } elseif ( $color[0] == 'Pink' ) {
            echo    wp_enqueue_style( 'pink', get_bloginfo( 'stylesheet_directory' ) . '/styles/pink.css', false, '', 'all' );
        } else {
            return; //Just show the default style
        }
    }
}
add_action( 'wp_print_styles', 'sltn_theme_color_option' );

Thanks for all the help and suggestions. Special thanks to Parker for the print_r() tip, going to file that away for later use.

Regards, Michael

Cool you solved and you’re welcome for the tip. Probably when you call the get_option_tree function there is a parameter to receive a string or array. My guess is that if this parameter is present, also if the value to be returned is only 1, it pushes it into an array.

In any case, now that you know how it works, you shouldn’t have any problem in the future.

Cheers,

Parker

332 posts
  • Exclusive Author
  • Sold between 5 000 and 10 000 dollars
  • Has been a member for 4-5 years
  • Europe
  • Envato Studio (Microlancer) Beta Tester
  • Bought between 1 and 9 items
  • Referred between 1 and 9 users
dekciw says

Actually, you dont need all those if..elseif..else statements. You should do something like this:

$color = get_option_tree( 'color_theme_option', '', false, true, -1 );
wp_enqueue_style( $color[0], get_bloginfo( 'stylesheet_directory' ) . '/styles/'. $color[0] .'.css', false, '', 'all' );
5 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 3-4 years
  • United States
solotechno says

Thank you very much dekciw. I was so excited that this was working after struggling with it for a whole day that I didn’t stop to think that it could be written more effectively. Makes perfect sense, if the index is always zero.

Regards,

-Michael

by
by
by
by
by
by