124 posts
  • 4 Years of Membership
  • Affiliate Level 1
  • Author Level 3
  • Collector Level 1
+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
1565 posts
  • Affiliate Level 1
  • Author Level 5
  • Collector Level 2
  • Featured Author
+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

3535 posts
  • Power Elite Author
  • Author Level 12
  • Trendsetter
  • United States
+13 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.

1565 posts
  • Affiliate Level 1
  • Author Level 5
  • Collector Level 2
  • Featured Author
+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.

3535 posts
  • Power Elite Author
  • Author Level 12
  • Trendsetter
  • United States
+13 more
sevenspark
Moderator
says

LOL ;)

124 posts
  • 4 Years of Membership
  • Affiliate Level 1
  • Author Level 3
  • Collector Level 1
+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?

1565 posts
  • Affiliate Level 1
  • Author Level 5
  • Collector Level 2
  • Featured Author
+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.)

510 posts Ngoc Viet
  • Affiliate Level 3
  • Author Level 7
  • Collector Level 3
  • Elite Author
+3 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;
}
...
";
?>
124 posts
  • 4 Years of Membership
  • Affiliate Level 1
  • Author Level 3
  • Collector Level 1
+3 more
hainug
says

Thank you very much to all of you :D

316 posts
  • Affiliate Level 2
  • Author Level 5
  • Collector Level 2
  • 4 Years of Membership
+2 more
SyamilMJ
says
by
by
by
by
by
by