ThemeForest

Wordpress dynamic css

36 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 1-2 years
  • Indonesia
  • Most Wanted Bounty Winner
  • Sold between 1 000 and 5 000 dollars
hainug says
Does anyone could let me know, how is the right way to use dynamic css on wordpress theme? Previously i add
require_once('../../../../wp-load.php');
on my-dinamic-css.php file but another article said, it will be make wordpress load twice.
Any advice would be appreciated, thanks in advance :D
671 posts
  • Sold between 5 000 and 10 000 dollars
  • Exclusive Author
  • Croatia
  • Bought between 10 and 49 items
  • Has been a member for 1-2 years
OriginalEXE says

You have three options in my opinion that would be valid and ok.

1.) Echo css directly inside of <head> from options (make sure to sanitize data)
2.) Use http://codex.wordpress.org/Function_Reference/wp_add_inline_style
3.) Use WP FileSystem API to create and update custom.css file on user’s server

2844 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred between 1000 and 1999 users
  • Has been a member for 3-4 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
+4 more
sevenspark moderator says

What do you mean by “dynamic”? If you just mean it is user-created, then you should write any custom-generated CSS to a static file using file_put_contents and then load it via wp_enqueue_style().

You shouldn’t serve front-end assets like CSS via PHP, it’s a waste of server resources.

EDIT – oh cool, I like OriginalEXE’s suggestion of the FileSystem API for writing the static file – didn’t know that existed! :) That’s even better then.

If by dynamic you mean the CSS actually changes every page load, it might make more sense to write it in a style block in the header. That’d be pretty unusual, though.

671 posts
  • Sold between 5 000 and 10 000 dollars
  • Exclusive Author
  • Croatia
  • Bought between 10 and 49 items
  • Has been a member for 1-2 years
OriginalEXE says

What do you mean by “dynamic”? If you just mean it is user-created, then you should write any custom-generated CSS to a static file using file_put_contents and then load it via wp_enqueue_style().

You shouldn’t serve front-end assets like CSS via PHP, it’s a waste of server resources.

If by dynamic you mean the CSS actually changes every page load, it might make more sense to write it in a style block in the header. That’d be pretty unusual, though.
Isn’t it a bad practice to use php functions for file management and not the WP FileSystem API?

Because, if you use native php functions than the server owns the files, and not user, which might cause problems on shared host where everyone would be able to edit your own files.

EDIT: I’ts not every day I get to teach an elite author something :P I can die happy now.

2844 posts
  • Elite Author
  • Sold between 250 000 and 1 000 000 dollars
  • Community Moderator
  • Bought between 50 and 99 items
  • Referred between 1000 and 1999 users
  • Has been a member for 3-4 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
+4 more
sevenspark moderator says

LOL ;)

36 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 1-2 years
  • Indonesia
  • Most Wanted Bounty Winner
  • Sold between 1 000 and 5 000 dollars
hainug says

Great, thanks for reply,

Actually i want to use it with theme option, so users can set their own font, color or content easily, any suggestion which one is best to use?

671 posts
  • Sold between 5 000 and 10 000 dollars
  • Exclusive Author
  • Croatia
  • Bought between 10 and 49 items
  • Has been a member for 1-2 years
OriginalEXE says

Well,

I would say that 1.) or 2.) would be the easiest for you as WP FileSystem API can sometimes cause problems on bad hosts, and if the user does not know his ftp details, it could be unnecessary pain in the a**.

i would go with number 2.)

372 posts
  • Bought between 10 and 49 items
  • Exclusive Author
  • Has been a member for 2-3 years
  • Referred between 1 and 9 users
  • Sold between 5 000 and 10 000 dollars
  • Vietnam
rongcon says
I use php to get data from inputs then replace css file. So if you have font,color options , you should have color.css and font.css :D
//add new color css file
        $color_options = get_option('wope-color');
        $current_color = $color_options['current-color'];
        $parent_dir = dirname(dirname(__FILE__));
        $color_file = $parent_dir.DS.'color-scheme'.DS."color.css";
        include("color_pattern.php");
        file_put_contents($color_file,$color_text);

abd the color_pattern.php file simple like that , no need for any hard code :D

<?php
$color_text = " 

::selection{
    background-color:#".$current_color.";
}

a,a:link , a:visited{
    color:#".$current_color.";
}

a:hover{
    color:#666666;
}
...
";
?>
36 posts
  • Bought between 1 and 9 items
  • Exclusive Author
  • Has been a member for 1-2 years
  • Indonesia
  • Most Wanted Bounty Winner
  • Sold between 1 000 and 5 000 dollars
hainug says

Thank you very much to all of you :D

282 posts
  • Attended a Community Meetup
  • Sold between 10 000 and 50 000 dollars
  • Exclusive Author
  • Bought between 10 and 49 items
  • Has been a member for 1-2 years
  • Referred between 1 and 9 users
SyamilMJ says
by
by
by
by
by