Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I know there are other, maybe better ways to do what I want to achieve, but I have to use this solution.

I have two stylesheets:

  • style.css - normal stylesheet
  • style-dynamic.php - dynamic stylesheet linked to the first one

I'm using some PHP code in the second one, like:

#body {
    background-color: <?php echo get_option('theme_background'); ?>;
}

Now the point is that the style-dynamic.php file doesn't know WordPress functions, and the code above gives:

Fatal error: Call to undefined function get_option() in C:\xampp\htdocs\wordpress\wp-content\themes\mytheme\style-dynamic.php on line 12

I guess I have to load wp_load.php (only?) and that's tricky, because I'm not sure how exactly? I was trying require_once, but I'm not sure I'll be able to jump from theme directory right to the WordPress main dir where wp-load.php file is?

Edit

Of course

require_once(ABSPATH . 'wp-load.php');

Outputs another error:

Use of undefined constant ABSPATH - assumed 'ABSPATH'

share|improve this question
add comment

2 Answers 2

up vote 3 down vote accepted

Christ, once again I was fighting with something for a few days straight and found a solution 20 minutes after asking on StackExchange. WHY?

require_once('../../../wp-load.php');
share|improve this answer
add comment

Hopefully better late than never; in case the solution above (by Wordpressor) is not working, you can load wp-load.php like this:

// allow us to use core wordpress functions
    $oldURL = dirname(__FILE__);
    $newURL = str_replace(DIRECTORY_SEPARATOR . 'wp-content' . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . 'THEME_NAME' . DIRECTORY_SEPARATOR . 'assets', '', $oldURL);
    include($newURL . DIRECTORY_SEPARATOR . 'wp-load.php');

Make sure to replace THEME_NAME with your theme's name.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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