WordPress:Running a Development Copy of WordPress

来自站长百科
Xxf3325讨论 | 贡献2008年6月20日 (五) 09:38的版本 (新页面: ==How To Run 2 Copies of WordPress From 1 Database== This hack is broken for version 2.3.1 - but you can use the solution at the bottom of the page for [[WordPress:Running a Development ...)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)
跳转至: 导航、​ 搜索

How To Run 2 Copies of WordPress From 1 Database

This hack is broken for version 2.3.1 - but you can use the solution at the bottom of the page for version 2.5

Solution until version 2.3.1

A common problem faced by theme and plugin developers is the desire to test your plugins on live data, but without making messy changes to your live site during development.

For example, I have my live site at http://www.ben-xo.com/v6/

But I keep my development site at http://localhost/~xo/ben-xo.com/dev/v6/

Here's a hack (for WordPress 2 - tested on 2.0.2) which will let you easily run both a local and a remote copy of your WordPress install without having to make any changes to your database or config files!

Please note that with this hack activated, Wordpress will IGNORE what you configure as your siteurl and home in your Options, and work them out for itself.

The hack goes in wp-includes/functions.php

/* Options functions */

function get_settings($setting) {

 global $wpdb;
 $value = wp_cache_get($setting, 'options');

   /* Ben XO's siteurl hack */
   if( 'siteurl' == $setting or 'home' == $setting ) {
   $_REAL_SCRIPT_DIR = realpath(dirname($_SERVER['SCRIPT_FILENAME'])); 
   // filesystem path of this page's directory (index.php or whatever)
   $_REAL_BASE_DIR = realpath(dirname(__FILE__) . 
     DIRECTORY_SEPARATOR . '..'); 
   // filesystem path of this file's parent directory 
   // (that wp-includes is within)
   $_MY_PATH_PART = substr( $_REAL_SCRIPT_DIR, strlen($_REAL_BASE_DIR)); 
   // just the subfolder part between <installation_path> and the page
   $INSTALLATION_PATH = $_MY_PATH_PART
     ? substr( dirname($_SERVER['SCRIPT_NAME']), 0, -strlen($_MY_PATH_PART) )
     : dirname($_SERVER['SCRIPT_NAME'])
   ; 
   // we subtract the subfolder part from the end of <installation_path>, 
   // leaving us with just <installation_path> :)
   $value = 'http' . ($_SERVER['HTTPS'] ? 's' : null) .
            '://' . $_SERVER['HTTP_HOST'] . $INSTALLATION_PATH
   ;
 }
 /* end Ben XO's siteurl hack */

 if ( false === $value ) {
   if ( defined('WP_INSTALLING') )
     $wpdb->hide_errors();

With this hack in place, you can now use exactly the same database and exactly the same files for both your local and remote WordPress install. Possibly the only thing you'd need to do is put different settings in wp-config.php if you want to use a remote database.

This hack is based on my comment on the PHP online documentation.

Please Note: this hack is untested if your server runs PHP in Safe Mode. If you have any problems, please post them on the Discussion page.

Solution for wordpress 2.5

Edit the file wp-includes/options.php

In the function get_option, add Ben's hack immediately after the line which says global $wpdb. It should look like the following:

function get_option( $setting ) {

global $wpdb;

/* Ben XO's siteurl hack */

if( 'siteurl' == $setting or 'home' == $setting )

{

$_REAL_SCRIPT_DIR = realpath(dirname($_SERVER['SCRIPT_FILENAME'])); // filesystem path of this page's directory (index.php or whatever)

$_REAL_BASE_DIR = realpath(dirname(__FILE__) . DIRECTORY_SEPARATOR . '..'); // filesystem path of this file's parent directory // (that wp-includes is within)

$_MY_PATH_PART = substr( $_REAL_SCRIPT_DIR, strlen($_REAL_BASE_DIR)); // just the subfolder part between <installation_path> and the page

$INSTALLATION_PATH = $_MY_PATH_PART ? substr( dirname($_SERVER['SCRIPT_NAME']), 0, -strlen($_MY_PATH_PART) ) : dirname($_SERVER['SCRIPT_NAME']) ; // we subtract the subfolder part from the end of <installation_path>, // leaving us with just <installation_path> :)

$value = 'http' . ($_SERVER['HTTPS'] ? 's' : null) . '://' . $_SERVER['HTTP_HOST'] . $INSTALLATION_PATH ;

return $value;

} /* end Ben XO's siteurl hack */


// Allow plugins to short-circuit options. $pre = apply_filters( 'pre_option_' . $setting, false ); if ( false !== $pre ) return $pre;

// ... rest of the function continues (do not copy this line!)

This page is [[WordPress::Category:Stubs|marked]] as incomplete. You can help Codex by expanding it.