5 posts
  • Has been part of the Envato Community for over 3 years
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Located in 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
  • Has referred 100+ members
  • Has sold $250,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Elite Author: Sold more than $75,000 on Envato Market
+9 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
  • Has referred 1+ members
  • Has sold $5,000+ on Envato Market
  • Has been a beta tester for an Envato feature
  • Has collected 1+ items on Envato Market
+3 more
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
  • Has been part of the Envato Community for over 3 years
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
  • Located in 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

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by