1

I have built a Wordpress plugin that requires to me to add CSS styles through the plugin's PHP file.

Is there a better way of doing this? I can see this getting out of hand if not done properly.

function flags() {  

 <style type="text/css">

  <?php if(get_option('display')=='Vertical') { ?>
    <?php if (get_option('language_option')=='specific') { ?>
        #flags {display:none !important; }
    <?php } ?>
        p.hello { font-size:12px; color:darkgray; }
  <?php } ?>

 </style>     
<?php } 
}   
4
  • 1
    I think this is missing some echo statements, isn't it? Commented Oct 18, 2013 at 18:41
  • I would move the conditions on the html instead of the CSS. Like having a high level div class="vertical-or-not somewhere. Commented Oct 18, 2013 at 18:47
  • @Sven No it is using templated PHP Commented Oct 18, 2013 at 18:48
  • @Sven why should there be an echo? everything outside the <?php ?> is as if you would echo it at that place. That's why many libs leave out the last ?> in the document and why php documents in utf-8 with BOM cause problems. Commented Oct 18, 2013 at 18:49

1 Answer 1

1

If there isn't much CSS, a better way is to add body classes that allow you to target your selectors only when they are present:

<body class="display-vertical">    
  ...

And move the CSS from inline to the stylesheet:

.display-vertical #flags{
   ...
}
Sign up to request clarification or add additional context in comments.

1 Comment

Just as I thought - simplicity always wins. Thanks for the answer.

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.