0

I'm developing a Wordpress theme and I added some options in order to change the font-family using a page of options. What I did is to add this link in the header.php:

<link rel="stylesheet" type="text/css" media="screen" href="<?php echo get_stylesheet_directory_uri(); ?>/custom-style.php" />

This file contains this code:

<?php
require_once( dirname(__FILE__) . '../../../../wp-config.php');
require_once( dirname(__FILE__) . '/functions.php');
header('Content-type: text/css');  
header('Expires: ' . gmdate( "D, d M Y H:i:s", time() + $expires_offset ) . ' GMT');
header("Cache-Control: public, max-age=$expires_offset");
?>
body{font-family:"<?php echo get_option('szt_fonts_body', 'Open Sans'); ?>"}
h1,h2,h3,h4,h5,h6,.hdunderline,#comments,.headlinepg,.headline,.headlinesitemap,.headline404,.headlinecontact{font-family:"<?php echo get_option('szt_fonts_heading', 'Oswald'); ?>"}
#logo .headertext a{font:<?php echo get_option('szt_fsize_logo', '2em'); ?> "<?php echo get_option('szt_fonts_logo', 'Michroma'); ?>"}
#logo .headertext{margin-top:<?php echo get_option('szt_margin_logo', '3px'); ?>}

The problem is this method reduces the load speed. With this code, the load speed increases from 600 miliseconds to 2.5-3 seconds. My question is: WHY? It's not a lot of code, so I don't understand the reason for that.

I'd like to find a solution on it. I know I can add the CSS code in the header.php as an alternative, but I would like to have a solution with separated code, CSS in one side and HTML in another side. What should I do?


Solved: It seems it was loading the Wordpress core twice. So I decided to don't use this file. I added the dinamic CSS in the header.php file. Anyway, I wonder if it'd be better to put that code in the header.php or in functions.php as a function (it's a code used in all pages).

2
  • Well, you are including Wordpress and Wordpress is probably communicating with a database as well. Commented Nov 21, 2013 at 20:37
  • By including wordpress, you're including everything it runs as well. This makes it a lot more than the few function calls you have inline. Commented Nov 21, 2013 at 21:06

1 Answer 1

2

Consider using APC or similar op-code cache. This will help a CMS/framework like WP which has to load all of its PHP includes at run-time (some other frameworks use autoloading).

In this example (CSS file with only a few config based method) there will be minimal processing required, so the slow speed is likely indicative of the disk IO relating to loading the entire WP code (every include) each time.

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

1 Comment

Yes, but I wonder if the increment of the load time is due to these 2 requires at the beginning. Consider that this theme (is my creation) has a big CSS file without problems, even jQuery and the Wordpress core, and the load time by default is 600 ms. Then, with the same things, and the same content (it's relevant to say it), and only adding this piece of code, the load time increases to 2.5-3 seconds. Is it for the requires? Btw, I'm using a plugin for the cache, etc., but it doesn't matter because what I try is to optimize the theme, first of all. What should I do? Ty.

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.