Include user defined styles without including wp-load

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:

Read More
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!

Related posts

1 comment

  1. 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.

Comments are closed.