103 posts
  • Has been part of the Envato Community for over 3 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 1+ items on Envato Market
+3 more
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
1504 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+2 more
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

3495 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $750,000+ on Envato Market
  • Located in United States
  • Helps us moderate the forums
+10 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.

1504 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+2 more
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.

3495 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $750,000+ on Envato Market
  • Located in United States
  • Helps us moderate the forums
+10 more
sevenspark Moderator says

LOL ;)

103 posts
  • Has been part of the Envato Community for over 3 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 1+ items on Envato Market
+3 more
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?

1504 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+2 more
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.)

498 posts
  • Has referred 10+ members
  • Has sold $40,000+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Sells items exclusively on Envato Market
+1 more
wopethemes 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;
}
...
";
?>
103 posts
  • Has been part of the Envato Community for over 3 years
  • Has referred 1+ members
  • Has sold $1,000+ on Envato Market
  • Has collected 1+ items on Envato Market
+3 more
hainug says

Thank you very much to all of you :D

316 posts
  • Has referred 10+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Has been part of the Envato Community for over 3 years
+2 more
SyamilMJ says

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