0

I have my wordpress theme setting for different stylesheet selection which is set using if else statement at front end.

my wordpress setting may have one value from following pool of values

red ,green, blue, yellow, white, pink, black, grey ,silver or purple

My template :

<link href="<?php bloginfo("template_url"); ?>/style.css" rel="stylesheet" media="all" />

<?php if (get_option('my_style') == "red"  : ?>
<link href="<?php bloginfo("template_url"); ?>/css/red.css" rel="stylesheet" media="all" />
<?php endif; ?>

<?php if (get_option('my_style') == "green"  : ?>
<link href="<?php bloginfo("template_url"); ?>/css/green.css" rel="stylesheet" media="all" />
<?php endif; ?>

<?php if (get_option('my_style') == "blue"  : ?>
<link href="<?php bloginfo("template_url"); ?>/css/blue.css" rel="stylesheet" media="all" />
<?php endif; ?>

<?php if (get_option('my_style') == "yellow"  : ?>
<link href="<?php bloginfo("template_url"); ?>/css/yellow.css" rel="stylesheet" media="all" />
<?php endif; ?>
.
.
.
.
.
<?php if (get_option('my_style') == "purple"  : ?>
<link href="<?php bloginfo("template_url"); ?>/css/purple.css" rel="stylesheet" media="all" />
<?php endif; ?>

In this way I can get particular stylesheet as per need. But this php code become lengthy if there are more value in the option pool. So is there any way to shorten this using an array?

4 Answers 4

3

May be you can reduce it to

<link href="<?php bloginfo("template_url"); ?>/css/<?php echo get_option('my_style'); ?>.css" rel="stylesheet" media="all" />

I don't think you need an array if the function get_option returns the string same as name of the css file.

Sign up to request clarification or add additional context in comments.

4 Comments

I think yours is the simplest one, But I wanna see how can I use array in it
@galexy Then go for Lobo's approach
If get_option draws from user input at all, then you are right to check it against an array of valid options @galexy. But you could move any such check to the get_option function itself, if it's under your control (not some wordpress thing) and it's not more general purpose.
I thought of that, but didn't see any major problem in it. Ideally, those values (if from user) will be coming from a drop-down, so we have control over the value attribute. Also, worse case, if someone changes the value (which is his fault), then the css won't get loaded, what else could happen?
1

This option:

<?php
$arraystyle=array("red", "green", "blue", "yellow", "white", "pink", "black", "grey", "silver", "purple");

$val=get_option('my_style');
if(!in_array($val, $arraystyle)){
    echo "Style not found";
    return false;
}
?>

<link href="<?php bloginfo("template_url"); ?>/css/<?php echo $arraystyle[$val];?>.css" rel="stylesheet" media="all" />

3 Comments

Thnkas lobo1 I am Checking it
The return false; statement doesn't really make sense without explicitly being in a function or an include file. If you put that on the main script page, then nothing will be executed after it, instead of what the OP has in the initial post, which would be an unstyled but complete page.
There is one default case where initially style.css is set ,which I want to set if no wordpress option is set or setting value = default in my option pool.
0

There is no real need to use an array here. You are making changes to what CSS file you are including according to a certain value.

I think what you are looking for is a switch case command. Here is a simply example of what you may be able to do with it -

<?php

$my_style = get_option('my_style');
switch($my_style){
 case "red":
   echo '<link href="'. bloginfo("template_url"). '/css/red.css" rel="stylesheet" media="all" />';
 break;
 case "green":
   echo '<link href="'. bloginfo("template_url"). '/css/green.css" rel="stylesheet" media="all" />';
 break;
 default :
   echo '<link href="'. bloginfo("template_url"). '/css/default.css" rel="stylesheet" media="all" />';
 break;
}

?>

Using this method, you can include more than one change for each my_style option. Note the use of a default case to handle any unexpected values...

Reference -

3 Comments

I think he specified in the question is there any way to shorten this using an array
Thanks Lix! for this switch ,But its become lengthy too
Yeah according to readability switch is simple to write and modify .
0
<?php
$my_styles = array(
    'red',
    'green',
    'blue',
    'yellow',
    'white',
    'pink',
    'black',
    'grey',
    'silver'
);
?>
<?php if(in_array($my_style = get_option('my_style'),$my_styles)) : ?>
    <link href="<?php echo bloginfo("template_url")."/css/{$my_style}.css"; ?>" rel="stylesheet" media="all" /> 
<?php endif; ?>

You can fill the variable with $my_styles with all the available styles, be it from database or whatever..

1 Comment

this is pretty much Салман answer, with a check to make sure something funky is not going on with the style. Because it could come from user input and could possibly shared with other users of the website... and to make sure there is actually a style selected...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.