1

I've looked through the answers on here and none quite match my situation. I have custom CSS that a user can edit and it is saved in the options. I use wp_enqueue_style to include a PHP file with a CSS content header and then load the CSS from the options. However, in order to load from the options I must include wp-load.php. I know this isn't the best way of doing things. How exactly should I achieve this instead?

Plugin main file:

add_action('wp_print_styles', 'wp123_load_css');
function wp123_load_css()
{    
    wp_register_style('wp123-custom-css', plugins_url('/include/wp-123-style.php', __FILE__));
    wp_enqueue_style('wp123-custom-css');
}

wp-123-style.php:

<?php
header('Content-type: text/css');
require_once('../../../../wp-load.php');
$options = get_option('wp123_options');
?>

<?php echo $options['custom_css']; ?>

What is the correct way of outputting the CSS file without having to include wp-load.php? Any help and suggestions are much appreciated!

1 Answer 1

3

If you are going to enqueue a stylesheet you should be using wp_enqueue_scripts not wp_print_styles, but you are in a bit of a gray zone between doing things "WordPress-ie" and doing things correctly for performance.

The "WordPress-ie" way is to enqueue a stylesheet, but from a performance standpoint you are loading an additional resource and in your case you also have to boot a significant portion of the WordPress Core to populate that dynamic stylesheet.

I would not do that. If it were me I would echo <style tags into the head. By doing so, you avoid loading an additional resource and you also avoid loading the WordPress twice. This is what the WordPress Core itself does with dynamic CSS created by, for example, the Theme Customization API.

The only case where I would consider loading another dynamic stylesheet would be if you have an enormous amount of CSS to load. I doubt you do.

3
  • If it's good enough for Wordpress Core it's good enough for me :) Thanks for the suggestion.
    – peacemaker
    Commented Nov 14, 2013 at 18:07
  • Just please don't echo script tags that load external static stylesheets. Those should be enqueued.
    – s_ha_dum
    Commented Nov 14, 2013 at 18:13
  • Haha yes of course not!
    – peacemaker
    Commented Nov 15, 2013 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.