1

I have a general question about the use of PHP in CSS. I am working right now on a Wordpress Theme and would like to make it as customizable as possible. I therefore sent the CSS Properties I would like to change via PHP to a JS file that changed them for me. But I am unsure if this is the best way to do it.

Would you guys recommend it and what are the pros and cons?

1

2 Answers 2

1

Let me tell you how I implemented customisable themes on my website, and see if it's of any help to you.

To start with, I use LESS. Wherever I have something customisable (usually colours, but sometimes background images too) I drop in a variable.

Then I have a _colours.less file, which simply defines all the variables, and gets imported at the start of every other .less file.

Finally, I present the users with an interface to edit these variables in a user-friendly manner.

At run-time, the PHP checks:

  • Does the main core.css file exist?
  • Is the core.css's modification time more recent than the _colours.less modification time?
  • Are the modification times of all .css file being used in this page more recent than the corresponding .less file's?

If any of the above result in the answer "no", then the given .css file is recompiled on-the-fly using the PHP lessc class (that basically implements the LESS compiler).

In this way, users can easily create their own custom themes, but it doesn't interfere in any way with my ability to update the site's layout.

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

2 Comments

Do you use any plugins to edit the LESS file or did you code it yourself?
I now use the exact same approach. Thanks alot. Eventhough I am not using the same PHP Compiler.
0

sending php value in js may create trouble sometime.

suppose your user install some plugin which js may conflict with your theme's js. It is possible it stops parsing further js and your changes may not apply.

So according to me setting php value in css is good method.

Comments

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.