WP 2.6 user defined constantPlugin conflict

  • wp 2.6 plugin issueThe upcoming WordPress 2.6 introduces new user defined constant for specific critical system directory settings. This new improvement may seem like a small “change” but on plugin developer context it can be an impending disaster.

    wp-config & wp-load files

    Most plugin depend on “absolute path” to wordpress system file (wp-config.php). So the problem is there.

    Lester-chan (GaMerZ) has this to say regarding the new WordPress 2.6 constant change.

    The constant only get loaded when you load WP. Plugins will have problem finding wp-config.php or wp-blog-header.php from the plugin file.

    might be broken in WP 2.6

    require_once('../../../wp-config.php');
    Long story, short

    Below is my workaround for quick plugin activation setup. Its not the best solution as it involved the dirty “write-permission”, it need improvement. If you intent to uses it, there is few preliminary step to be set first:- ↓

    1. browse to your plugin dir
    2. create a blank php file: constant.php
    3. copy paste the below code save it as new file relative to the constant.php
    4. create or set your .htaccess and add Options All -Indexes.
    Custom wp config file
    function my_plugin_write_config()
    {
    	$constant = get_defined_constants(true);
    	$user_defined = $constant['user'];
    	unset($constant);
    
    	$constant = array();
    
    	// assuming that all latest wordpress CONSTANT start with WP_
    	$wp_constant_prefix = "/WP_/";
    
    	foreach($user_defined as $k=>$v){
    		if (preg_match($wp_constant_prefix,$k)){
    			$constant[$k] = $v;
    		}
    	}
    
    	unset($user_defined);
    
    	$constant['ABSPATH'] = (strtr(realpath(ABSPATH), array("\\", DIRECTORY_SEPERATOR)));
    	$constant['WPINC'] 	= WPINC;
    	$constant['WP_VERSION'] = get_bloginfo('version');
    
    	$constant = array_map('json_encode',$constant);
    
    	$my_plugin_config_file 	= dirname(__FILE__).DIRECTORY_SEPARATOR.'constant.php';
    
    	if (is_writeable($my_plugin_config_file))
    	{
    		$content = "<?php if(!defined('MY_PLUGIN_TOKEN')) die('42');\n";
    
    		foreach($constant as $k=>$v){
    			$content .= sprintf('define(\'CONST_%1s\', %2s);',strtoupper($k),$v)."\n";
    		}
    
    		unset($constant,$k,$v);
    
    		$content .= "?>";	
    
    		$fp = false;
    
    		if ( ($fp = fopen($my_plugin_config_file,'w+') ) != false) {
    			stream_set_blocking($fp, TRUE);
    			stream_set_timeout($fp,5);
    			stream_set_write_buffer($fp, 0);
    			fwrite($fp, $content);
    			fclose($fp);
    		}		
    
    		unset($content,$my_plugin_config_file,$fp);
    
    	} else {
    		add_action('admin_notices','my_plugin_notification');
    	}
    }
    
    function my_plugin_notification()
    {
    ?>	<div id="message" class="error">
    	<h3><?php _e('ERR') ?> </h3>
    		<p><a href="http://doc.myplugin.com"><? _e('RTFM!'); ?></a></p>
    	</div>
    <?php
    }
    
    register_activation_hook(__FILE__, 'my_plugin_write_config' );
    add_action('update_option_siteurl','my_plugin_write_config');
    add_action('update_option_home','my_plugin_write_config');
    

    Basically this script will search all user defined constant that start with “WP_” prefix, plus additional wordpress constant. Then it will write all these constant to disk inside “constant.php” (once). It also runs after a WordPress option has been update (active when user update settings for home & siteurl, I add the extra action hook just for example).

    Deprecated Functions

    Out of all this issue I still think WordPress as one of the best “back-compat friendly” CMS. Most of the legacy functions & variables (since version 0.71 >= 2.5) is still available inside WordPress system (wp-includes/deprecated.php). Not sure how long it will stay there..

    Might be interested
    About the Author
     

No Responsesto “WP 2.6 user defined constant”

    • stalker's photo Kaizeku Ban
    • RE: WP 2.6 user defined constant - 'Commenting Guidlines' ↓
      4 months, 2 weeks ago on Wednesday, July 2nd, 2008 at 4:55 pm 5 url
      0%

      If you want to comment, please read the following guidelines.These are designed to protect you and other users of the site.

      1. Be relevant: Your comment should be a thoughtful contribution to the subject of the entry. Keep your comments constructive and polite.
      2. No advertising or spamming: Do not use the comment feature to promote commercial entities/products, affiliates services or websites. You are allowed to post a link as long as it's relevant to the entry.
      3. Keep within the law: Do not link to offensive or illegal content websites. Do not make any defamatory or disparaging comments which might damage the reputation of a person or organisation.
      4. Privacy: Do not post any personal information relating to yourself or anyone else - (ie: address, place of employment, telephone or mobile number or email address).

      In order to keep these experiences enjoyable and interesting for all of our users, we ask that you follow the above guidlines. Feel free to engage, ask questions, and tell us what you are thinking! insightful comments are most welcomed.

      be the first to comment.

"write as if you were talking to a good friend (in front of your mother)."

.haveyoursay

    • Email will not be published.

Disclaimer: For any content that you post, you hereby grant to Kaizeku Ban the royalty-free, irrevocable, perpetual, exclusive and fully sublicensable license to use, reproduce, modify, adapt, publish, translate, create derivative works from, distribute, perform and display such content in whole or in part, world-wide and to incorporate it in other works, in any form, media or technology now known or later developed. Some rights reserved.