• WP 2.6 is just released, and developers now have a problem. Problem is location of wp-config.php file. Many will say that no one should need to load this file, but there are few situations where you will actually need to load this file.

    If you have CSS or JS external file that needs loading of options from the WP database, you need to load config file. I use this in my Starscape theme because based on the options set by the user in control panel, CSS will change. Since I hate embeding CSS code in HTML/PHP file with <STYLE> tag, I need to do it in CSS file.

    Only way to do this right now is to have a file where user can insert path to the wp-config file, and then PHP in CSS file can load this file to get the location.

    Is there any other way?

    Also, it would be good if the WP developers provide some proper way to load wp-config for use in external resource files like CSS or JS.

    Regards,
    Milan

Viewing 10 replies - 1 through 10 (of 10 total)
  • “Also, it would be good if the WP developers provide some proper way to load wp-config for use in external resource files like CSS or JS.”

    That’s EXACTLY what I’m looking for and haven’t found an efficient way of doing it yet! 🙁

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    If you have CSS or JS external file that needs loading of options from the WP database, you need to load config file.

    No, you don’t. There is absolutely NO case where you *must* load the config file yourself. The only reason to ever do that is simply lazy programming.

    Instead, you need to make your plugin or theme intercept a special query variable to the main blog URL, instead of calling some separate file directly. Then you call the main URL via your AJAX with that special query variable.

    Basically, you add your own query variable, then act on it when it is received, bypassing WordPress’ own output.

    So, to add a query variable:

    add_filter('query_vars', 'add_my_var');
    function add_my_var($public_query_vars) {
    	$public_query_vars[] = 'some_unique_identifier';
    	return $public_query_vars;
    }

    Now, you need to check for that identifier and act on it:

    add_action('template_redirect', 'my_var_output');
    function my_var_output() {
    	$myvalue=get_query_var('some_unique_identifier');
    	if ($myvalue) {
    		// do whatever output you like here
    		// you can even use $myvalue to see what the request is for
    		exit; // this stops WordPress entirely
    	}
    }

    And there you go. Now you can make your javascript/AJAX call the main WordPress site URL with ?some_unique_identifier=myvalue and this will redirect to your plugin/theme and let you make any sort of output you like.

    If you override the header with an application/json content type and throw some JSON back at it, and it’s perfect for an AJAX request. Also, because you’re in the plugin context, you have full access to all of WordPress’ functions.

    Look at the plugin named WP-Microsummary. It does this perfectly and is a very good way to design your plugin. It’s also forward compatible and does not care where the wp-config is.

    I’ve looked at WP-Microsummary and didn’t see anything like that referenced. (but then again not all of your snippet made sense to me)

    Like GDragoN, I am also trying to add options to my theme, but require wp-config to be called (included, whatever). I’m not using ajax or js, just a simple couple of lines pointing to which stylesheet to load for which option the user chooses.

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    WP-Microsummary does just this, by adding a link tag pointing back at the current page, but which has “?microsummary=1” added onto the end of the URL.

    Let me give you an example…

    Let’s say you used this code in a plugin or a theme’s functions.php file:

    add_filter('query_vars', 'add_my_var');
    function add_my_var($public_query_vars) {
    	$public_query_vars[] = 'myoption';
    	return $public_query_vars;
    }
    add_action('template_redirect', 'my_var_output');
    function my_var_output() {
    	$myvalue=get_query_var('myoption');
    	if ($myvalue) {
    		switch ($myvalue) {
    		case '1': echo 'I got a one!';
    			break;
    		case '2': echo 'I got a two!';
    			break;
    		}
    		exit; // this stops WordPress entirely
    	}
    }

    What happens when you go to your blog url + “?myoption=1” ? How about if you set myoption=2 in the URL?

    Do you begin to see how this option can work?

    But, if you’re doing a simple option of which stylesheet to load, then you don’t need to do this sort of thing anyway. Nor do you ever need to load wp-config manually. What exactly are you trying to do? Why do you think that you need to load wp-config? Because I guarantee you that you really don’t, you’re just taking the wrong approach.

    This is my functions.php: http://pastebin.com/mfcf14cb

    And this is my style.php: http://pastebin.com/d43406388

    I have 4 different styles (options) defined in functions for the user to choose from and call the corresponding stylesheet in style.php (in addition to the “default” style contained therein).

    Does that make sense?

    I based this on a how to I found on the web a while back. (here)

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Wow. That is one screwed up backwards way of doing things.

    Here’s where he includes the stylesheet link tag:

    function mytheme_wp_head() { ?>
    <link href="<?php bloginfo('template_directory'); ?>/style.php" rel="stylesheet" type="text/css" />
    ...

    The problem is that the the style.php basically just pulls out the value from the database and does an import of that sheet. Wrong way to do it, really. Instead, the original link tag should have pointed directly at that sheet. Something like this:

    function mytheme_wp_head() {
    global $options;
    foreach ($options as $value) {
        if (get_settings( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; } else { $$value['id'] = get_settings( $value['id'] ); } }
    ?>
    <link href="<?php bloginfo('template_directory'); ?>/<?php echo $tn_body_style; ?>.css" rel="stylesheet" type="text/css" />
    ...

    The remaining stuff in the style.php (the extra CSS) could be added to the main style.css file, naturally included in the header.

    This has the added benefit of not *doubling your CPU load*, which is what that code does on every single page load, since it has to load WordPress twice.

    Otto, if I could reach you, I’d kiss you!

    It worked. Thank you SO MUCH! 🙂

    Believe it or not what I had before wasn’t too much off yours and when I couldn’t get passed an error message, I Googled for a work-around and found the “backwards” method. (Hey, whatever works, right?)

    Thanks so much for setting me straight!

    Otto, if I understand correctly, you are suggesting to include static CSS files based on user preferences?

    If a user chooses a red background or bigger fonts, we would additionally include red.css and bigger-fonts.css?

    Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Otto, if I understand correctly, you are suggesting to include static CSS files based on user preferences?

    If a user chooses a red background or bigger fonts, we would additionally include red.css and bigger-fonts.css?

    That would be one way to do it, of course. I’ve seen themes do that.

    Alternatively, you could hook to the wp_head and manually insert style blocks corresponding to your selections.

    Alternatively to even that, you could define your own action in the header with something like do_action('my_styles'); and then hook to that instead, ensuring that other things that mess with wp_head won’t mess with you.

    Otto,

    You’re my hero. I have been searching for a way to circumvent including wp-config.php. I’ve had multiple nightmares with my AJAX plugin trying to get it to work on various platforms with a wp-config include. The nightmare is over now. Your solution was quick and easy and the processing time is much faster too. Thanks for the help!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Location of wp-config.php in WordPress 2.6’ is closed to new replies.