关于nikbobo

Nikbobo,本名刘永强,记忆空间站长,男,1998 年出生于广东茂名,至今(2022 年)23 岁,目前(2022 年)就读于广州大学华软软件学院,常以“nikbobo”这个网名混迹互联网。如无特殊注明,Nikbobo 在本站发表的文章,遵循 知识共享 署名-非商业性使用-相同方式共享 4.0 国际 许可协议。详情请参阅关于页面的作者介绍。

简单几步将 WordPress 变成 BAE 定制版

这篇文章,起源于想玩一下 BAE。可是,全网搜索,仅仅找到各种高手已经定制好的版本,却没有比较完善的教大家修改的教程(有,但都不完善)。所以,自己下载各种所谓的BAE定制版进行代码对比,去其糟粕,取其精华,舍去那些添加的让人感到不顺眼的东西,做成了这个教程。

其实 BAE 不如 SAE 那样对函数有严格的限制,所以不需要动太多核心代码即可搞掂。所以,大家不必担心教程过于难,大胆动手尝试!

首先,在 BAE 新建一个 PC Ifame 应用,正确建好数据库,建好云存储 BUCKET,建好消息服务(单模式/多模式均可)这些不用我教了吧?

并且,学会用 SVN/GIT 上传代码,这个自己看 BAE 帮助手册,毕竟我不是教大家如何在 BAE 搭建 WordPress,而是教大家如何定制 WordPress BAE 版。

然后,下载一个最新版的 WordPress 下来,这个自己百度。(本教程基于目前最新版 WordPress 3.6.1)

好了,一步步开始动工!

第一步:修改 WP-Config.php

新建一个文件,命名为 wp-config.php,或者直接将 wp-config-sample.php 重命名为 wp-config.php。

然后,写入以下代码:

<?php
/**
* WordPress 基础配置文件。
*
* 本文件包含以下配置选项:MySQL 设置、数据库表名前缀、密钥、
* WordPress 语言设定以及 ABSPATH。如需更多信息,请访问
* {@link http://codex.wordpress.org/zh-cn:%E7%BC%96%E8%BE%91_wp-config.php
* 编辑 wp-config.php} Codex 页面。MySQL 设置具体信息请咨询您的空间提供商。
*
* 这个文件用在于安装程序自动生成 wp-config.php 配置文件,
* 您可以手动复制这个文件,并重命名为“wp-config.php”,然后输入相关信息。
*
* @package WordPress
*/

// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress 数据库的名称 */
define('DB_NAME', 'database_name_here');

/** MySQL 数据库用户名 */
define('DB_USER', getenv('HTTP_BAE_ENV_AK'));

/** MySQL 数据库密码 */
define('DB_PASSWORD', getenv('HTTP_BAE_ENV_SK'));

/** MySQL 主机 */
define('DB_HOST', getenv('HTTP_BAE_ENV_ADDR_SQL_IP') . ":" . getenv('HTTP_BAE_ENV_ADDR_SQL_PORT'));

/** 创建数据表时默认的文字编码 */
define('DB_CHARSET', 'utf8');

/** 数据库整理类型。如不确定请勿更改 */
define('DB_COLLATE', '');

/** BCMS 消息队列的名称 */
define('BCMS_QUEUE', 'bcms_queue_here');

/**#@+
* 身份认证密匙设定。
*
* 您可以随意写一些字符
* 或者直接访问 {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org 私钥生成服务},
* 任何修改都会导致 cookie 失效,所有用户必须重新登录。
*
* @since 2.6.0
*/
define('AUTH_KEY',         'put your unique phrase here');
define('SECURE_AUTH_KEY',  'put your unique phrase here');
define('LOGGED_IN_KEY',    'put your unique phrase here');
define('NONCE_KEY',        'put your unique phrase here');
define('AUTH_SALT',        'put your unique phrase here');
define('SECURE_AUTH_SALT', 'put your unique phrase here');
define('LOGGED_IN_SALT',   'put your unique phrase here');
define('NONCE_SALT',       'put your unique phrase here');

/**#@-*/

/**
* WordPress 数据表前缀。
*
* 如果您有在同一数据库内安装多个 WordPress 的需求,请为每个 WordPress 设置不同的数据表前缀。
* 前缀名只能为数字、字母加下划线。
*/
$table_prefix  = 'wp_';

/**
* WordPress 语言设置,中文版本默认为中文。
*
* 本项设定能够让 WordPress 显示您需要的语言。
* wp-content/languages 内应放置同名的 .mo 语言文件。
* 要使用 WordPress 简体中文界面,只需填入 zh_CN。
*/
define('WPLANG', 'zh_CN');

/**
* 开发者专用:WordPress 调试模式。
*
* 将这个值改为“true”,WordPress 将显示所有用于开发的提示。
* 强烈建议插件开发者在开发环境中启用本功能。
*/
define('WP_DEBUG', false);

/**
* WordPress 多站点模式。
*
* 将这个值改为“true”,WordPress 将启用多站点模式。
*/
define('WP_ALLOW_MULTISITE', false);

/* 好了!请不要再继续编辑。请保存本文件。使用愉快! */

/** WordPress 目录的绝对路径。 */
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');

/** 设置 WordPress 变量和包含文件。 */
require_once(ABSPATH . 'wp-settings.php');

照着里面的中文注释,把相应数据填入。身份密匙的生成,在注释中也有详细的说明,照办即可。

第二步:修改 wp-admin\includes\file.php

在约第 330 行,查找

// Move the file to the uploads dir
$new_file = $uploads['path'] . "/$filename";

替换为

// Fix By BAE
$hiPath = sys_get_temp_dir();
$new_file = $uploads['path'] . "/$filename";

第三步:修改 wp-includes\wp-db.php

在约 1163 行,查找

$this->set_charset( $this->dbh );
$this->ready = true;
$this->select( $this->dbname, $this->dbh );

替换为

//Fix By BAE
$this->ready = true;
$this->select( $this->dbname, $this->dbh );
$this->set_charset( $this->dbh );

第四步:修改 wp-includes\pluggable.php

在约 177 行,查找

if ( !function_exists( 'wp_mail' ) ) :
/**
 * Send mail, similar to PHP's mail
 *
 * A true return value does not automatically mean that the user received the
 * email successfully. It just only means that the method used was able to
 * process the request without any errors.
 *
 * Using the two 'wp_mail_from' and 'wp_mail_from_name' hooks allow from
 * creating a from address like 'Name <email@address.com>' when both are set. If
 * just 'wp_mail_from' is set, then just the email address will be used with no
 * name.
 *
 * The default content type is 'text/plain' which does not allow using HTML.
 * However, you can set the content type of the email by using the
 * 'wp_mail_content_type' filter.
 *
 * The default charset is based on the charset used on the blog. The charset can
 * be set using the 'wp_mail_charset' filter.
 *
 * @since 1.2.1
 * @uses apply_filters() Calls 'wp_mail' hook on an array of all of the parameters.
 * @uses apply_filters() Calls 'wp_mail_from' hook to get the from email address.
 * @uses apply_filters() Calls 'wp_mail_from_name' hook to get the from address name.
 * @uses apply_filters() Calls 'wp_mail_content_type' hook to get the email content type.
 * @uses apply_filters() Calls 'wp_mail_charset' hook to get the email charset
 * @uses do_action_ref_array() Calls 'phpmailer_init' hook on the reference to
 *		phpmailer object.
 * @uses PHPMailer
 *
 * @param string|array $to Array or comma-separated list of email addresses to send message.
 * @param string $subject Email subject
 * @param string $message Message contents
 * @param string|array $headers Optional. Additional headers.
 * @param string|array $attachments Optional. Files to attach.
 * @return bool Whether the email contents were sent successfully.
 */
function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
	// Compact the input, apply the filters, and extract them back out
	extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) );

	if ( !is_array($attachments) )
		$attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );

	global $phpmailer;

	// (Re)create it, if it's gone missing
	if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
		require_once ABSPATH . WPINC . '/class-phpmailer.php';
		require_once ABSPATH . WPINC . '/class-smtp.php';
		$phpmailer = new PHPMailer( true );
	}

	// Headers
	if ( empty( $headers ) ) {
		$headers = array();
	} else {
		if ( !is_array( $headers ) ) {
			// Explode the headers out, so this function can take both
			// string headers and an array of headers.
			$tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
		} else {
			$tempheaders = $headers;
		}
		$headers = array();
		$cc = array();
		$bcc = array();

		// If it's actually got contents
		if ( !empty( $tempheaders ) ) {
			// Iterate through the raw headers
			foreach ( (array) $tempheaders as $header ) {
				if ( strpos($header, ':') === false ) {
					if ( false !== stripos( $header, 'boundary=' ) ) {
						$parts = preg_split('/boundary=/i', trim( $header ) );
						$boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
					}
					continue;
				}
				// Explode them out
				list( $name, $content ) = explode( ':', trim( $header ), 2 );

				// Cleanup crew
				$name    = trim( $name    );
				$content = trim( $content );

				switch ( strtolower( $name ) ) {
					// Mainly for legacy -- process a From: header if it's there
					case 'from':
						if ( strpos($content, '<' ) !== false ) {
							// So... making my life hard again?
							$from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
							$from_name = str_replace( '"', '', $from_name );
							$from_name = trim( $from_name );

							$from_email = substr( $content, strpos( $content, '<' ) + 1 );
							$from_email = str_replace( '>', '', $from_email );
							$from_email = trim( $from_email );
						} else {
							$from_email = trim( $content );
						}
						break;
					case 'content-type':
						if ( strpos( $content, ';' ) !== false ) {
							list( $type, $charset ) = explode( ';', $content );
							$content_type = trim( $type );
							if ( false !== stripos( $charset, 'charset=' ) ) {
								$charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) );
							} elseif ( false !== stripos( $charset, 'boundary=' ) ) {
								$boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset ) );
								$charset = '';
							}
						} else {
							$content_type = trim( $content );
						}
						break;
					case 'cc':
						$cc = array_merge( (array) $cc, explode( ',', $content ) );
						break;
					case 'bcc':
						$bcc = array_merge( (array) $bcc, explode( ',', $content ) );
						break;
					default:
						// Add it to our grand headers array
						$headers[trim( $name )] = trim( $content );
						break;
				}
			}
		}
	}

	// Empty out the values that may be set
	$phpmailer->ClearAddresses();
	$phpmailer->ClearAllRecipients();
	$phpmailer->ClearAttachments();
	$phpmailer->ClearBCCs();
	$phpmailer->ClearCCs();
	$phpmailer->ClearCustomHeaders();
	$phpmailer->ClearReplyTos();

	// From email and name
	// If we don't have a name from the input headers
	if ( !isset( $from_name ) )
		$from_name = 'WordPress';

	/* If we don't have an email from the input headers default to wordpress@$sitename
	 * Some hosts will block outgoing mail from this address if it doesn't exist but
	 * there's no easy alternative. Defaulting to admin_email might appear to be another
	 * option but some hosts may refuse to relay mail from an unknown domain. See
	 * http://trac.wordpress.org/ticket/5007.
	 */

	if ( !isset( $from_email ) ) {
		// Get the site domain and get rid of www.
		$sitename = strtolower( $_SERVER['SERVER_NAME'] );
		if ( substr( $sitename, 0, 4 ) == 'www.' ) {
			$sitename = substr( $sitename, 4 );
		}

		$from_email = 'wordpress@' . $sitename;
	}

	// Plugin authors can override the potentially troublesome default
	$phpmailer->From     = apply_filters( 'wp_mail_from'     , $from_email );
	$phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name  );

	// Set destination addresses
	if ( !is_array( $to ) )
		$to = explode( ',', $to );

	foreach ( (array) $to as $recipient ) {
		try {
			// Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
			$recipient_name = '';
			if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
				if ( count( $matches ) == 3 ) {
					$recipient_name = $matches[1];
					$recipient = $matches[2];
				}
			}
			$phpmailer->AddAddress( $recipient, $recipient_name);
		} catch ( phpmailerException $e ) {
			continue;
		}
	}

	// Set mail's subject and body
	$phpmailer->Subject = $subject;
	$phpmailer->Body    = $message;

	// Add any CC and BCC recipients
	if ( !empty( $cc ) ) {
		foreach ( (array) $cc as $recipient ) {
			try {
				// Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
				$recipient_name = '';
				if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
					if ( count( $matches ) == 3 ) {
						$recipient_name = $matches[1];
						$recipient = $matches[2];
					}
				}
				$phpmailer->AddCc( $recipient, $recipient_name );
			} catch ( phpmailerException $e ) {
				continue;
			}
		}
	}

	if ( !empty( $bcc ) ) {
		foreach ( (array) $bcc as $recipient) {
			try {
				// Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
				$recipient_name = '';
				if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
					if ( count( $matches ) == 3 ) {
						$recipient_name = $matches[1];
						$recipient = $matches[2];
					}
				}
				$phpmailer->AddBcc( $recipient, $recipient_name );
			} catch ( phpmailerException $e ) {
				continue;
			}
		}
	}

	// Set to use PHP's mail()
	$phpmailer->IsMail();

	// Set Content-Type and charset
	// If we don't have a content-type from the input headers
	if ( !isset( $content_type ) )
		$content_type = 'text/plain';

	$content_type = apply_filters( 'wp_mail_content_type', $content_type );

	$phpmailer->ContentType = $content_type;

	// Set whether it's plaintext, depending on $content_type
	if ( 'text/html' == $content_type )
		$phpmailer->IsHTML( true );

	// If we don't have a charset from the input headers
	if ( !isset( $charset ) )
		$charset = get_bloginfo( 'charset' );

	// Set the content-type and charset
	$phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );

	// Set custom headers
	if ( !empty( $headers ) ) {
		foreach( (array) $headers as $name => $content ) {
			$phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
		}

		if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) )
			$phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
	}

	if ( !empty( $attachments ) ) {
		foreach ( $attachments as $attachment ) {
			try {
				$phpmailer->AddAttachment($attachment);
			} catch ( phpmailerException $e ) {
				continue;
			}
		}
	}

	do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );

	// Send!
	try {
		return $phpmailer->Send();
	} catch ( phpmailerException $e ) {
		return false;
	}
}
endif;

替换为

if ( !function_exists( 'wp_mail' ) ) :
/**
 * Send Mail 
 * BCMS 
 */
function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
	// Headers
	if ( empty( $headers ) ) {
		$headers = array();
	} else {
		if ( !is_array( $headers ) ) {
			// Explode the headers out, so this function can take both
			// string headers and an array of headers.
			$tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
		} else {
			$tempheaders = $headers;
		}
		$headers = array();

		// If it's actually got contents
		if ( !empty( $tempheaders ) ) {
			// Iterate through the raw headers
			foreach ( (array) $tempheaders as $header ) {
				// Explode them out
				list( $name, $content ) = explode( ':', trim( $header ), 2 );

				// Cleanup crew
				$name = trim( $name	);
				$content = trim( $content );

				switch ( strtolower( $name ) ) {
					// Mainly for legacy -- process a From: header if it's there
					case 'from':
						if ( strpos($content, '<' ) !== false ) {
							// So... making my life hard again?
							$from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
							$from_name = str_replace( '"', '', $from_name );
							$from_name = trim( $from_name );

							$from_email = substr( $content, strpos( $content, '<' ) + 1 );
							$from_email = str_replace( '>', '', $from_email );
							$from_email = trim( $from_email );
						} else {
							$from_email = trim( $content );
						}
						break;
					case 'content-type':
						if ( strpos( $content, ';' ) !== false ) {
							list( $type, $charset ) = explode( ';', $content );
							$content_type = trim( $type );
						} else {
							$content_type = trim( $content );
						}
						break;

				}
			}
		}
	}

	/* If we don't have an email from the input headers default to wordpress@$sitename
	 * Some hosts will block outgoing mail from this address if it doesn't exist but
	 * there's no easy alternative. Defaulting to admin_email might appear to be another
	 * option but some hosts may refuse to relay mail from an unknown domain. See
	 * http://trac.wordpress.org/ticket/5007.
	 */

	if ( !isset( $from_email ) ) {
		// Get the site domain and get rid of www.
		$sitename = strtolower( $_SERVER['SERVER_NAME'] );
		if ( substr( $sitename, 0, 4 ) == 'www.' ) {
			$sitename = substr( $sitename, 4 );
		}

		$from_email = 'no-reply@' . $sitename;
	}

	// Set Content-Type and charset
	// If we don't have a content-type from the input headers
	if ( !isset( $content_type ) )
		$content_type = 'text/plain';

	$content_type = apply_filters( 'wp_mail_content_type', $content_type );
	if($content_type == 'text/html') {
		$header = "<!--HTML-->";
	}

	// 重复引用,BaeException就是这里报的
	// require_once ABSPATH . WPINC . '/Bcms.class.php';
	$bcms = new Bcms () ;

	// 利用bcms发信
	$ret = $bcms->mail ( BCMS_QUEUE, $header.$message, array($to), array( Bcms::FROM=>$from_email ,Bcms::MAIL_SUBJECT => $subject)) ;

	// 返回值
	if ( false === $ret ) {
		return false;
	} else {
		return true;
	}
}
endif;

if ( !function_exists( 'wp_mail' ) ) :
/**
 * Send mail, similar to PHP's mail
 *
 * A true return value does not automatically mean that the user received the
 * email successfully. It just only means that the method used was able to
 * process the request without any errors.
 *
 * Using the two 'wp_mail_from' and 'wp_mail_from_name' hooks allow from
 * creating a from address like 'Name <email@address.com>' when both are set. If
 * just 'wp_mail_from' is set, then just the email address will be used with no
 * name.
 *
 * The default content type is 'text/plain' which does not allow using HTML.
 * However, you can set the content type of the email by using the
 * 'wp_mail_content_type' filter.
 *
 * The default charset is based on the charset used on the blog. The charset can
 * be set using the 'wp_mail_charset' filter.
 *
 * @since 1.2.1
 * @uses apply_filters() Calls 'wp_mail' hook on an array of all of the parameters.
 * @uses apply_filters() Calls 'wp_mail_from' hook to get the from email address.
 * @uses apply_filters() Calls 'wp_mail_from_name' hook to get the from address name.
 * @uses apply_filters() Calls 'wp_mail_content_type' hook to get the email content type.
 * @uses apply_filters() Calls 'wp_mail_charset' hook to get the email charset
 * @uses do_action_ref_array() Calls 'phpmailer_init' hook on the reference to
 *		phpmailer object.
 * @uses PHPMailer
 *
 * @param string|array $to Array or comma-separated list of email addresses to send message.
 * @param string $subject Email subject
 * @param string $message Message contents
 * @param string|array $headers Optional. Additional headers.
 * @param string|array $attachments Optional. Files to attach.
 * @return bool Whether the email contents were sent successfully.
 */
function wp_mail( $to, $subject, $message, $headers = '', $attachments = array() ) {
	// Compact the input, apply the filters, and extract them back out
	extract( apply_filters( 'wp_mail', compact( 'to', 'subject', 'message', 'headers', 'attachments' ) ) );

	if ( !is_array($attachments) )
		$attachments = explode( "\n", str_replace( "\r\n", "\n", $attachments ) );

	global $phpmailer;

	// (Re)create it, if it's gone missing
	if ( !is_object( $phpmailer ) || !is_a( $phpmailer, 'PHPMailer' ) ) {
		require_once ABSPATH . WPINC . '/class-phpmailer.php';
		require_once ABSPATH . WPINC . '/class-smtp.php';
		$phpmailer = new PHPMailer( true );
	}

	// Headers
	if ( empty( $headers ) ) {
		$headers = array();
	} else {
		if ( !is_array( $headers ) ) {
			// Explode the headers out, so this function can take both
			// string headers and an array of headers.
			$tempheaders = explode( "\n", str_replace( "\r\n", "\n", $headers ) );
		} else {
			$tempheaders = $headers;
		}
		$headers = array();
		$cc = array();
		$bcc = array();

		// If it's actually got contents
		if ( !empty( $tempheaders ) ) {
			// Iterate through the raw headers
			foreach ( (array) $tempheaders as $header ) {
				if ( strpos($header, ':') === false ) {
					if ( false !== stripos( $header, 'boundary=' ) ) {
						$parts = preg_split('/boundary=/i', trim( $header ) );
						$boundary = trim( str_replace( array( "'", '"' ), '', $parts[1] ) );
					}
					continue;
				}
				// Explode them out
				list( $name, $content ) = explode( ':', trim( $header ), 2 );

				// Cleanup crew
				$name    = trim( $name    );
				$content = trim( $content );

				switch ( strtolower( $name ) ) {
					// Mainly for legacy -- process a From: header if it's there
					case 'from':
						if ( strpos($content, '<' ) !== false ) {
							// So... making my life hard again?
							$from_name = substr( $content, 0, strpos( $content, '<' ) - 1 );
							$from_name = str_replace( '"', '', $from_name );
							$from_name = trim( $from_name );

							$from_email = substr( $content, strpos( $content, '<' ) + 1 );
							$from_email = str_replace( '>', '', $from_email );
							$from_email = trim( $from_email );
						} else {
							$from_email = trim( $content );
						}
						break;
					case 'content-type':
						if ( strpos( $content, ';' ) !== false ) {
							list( $type, $charset ) = explode( ';', $content );
							$content_type = trim( $type );
							if ( false !== stripos( $charset, 'charset=' ) ) {
								$charset = trim( str_replace( array( 'charset=', '"' ), '', $charset ) );
							} elseif ( false !== stripos( $charset, 'boundary=' ) ) {
								$boundary = trim( str_replace( array( 'BOUNDARY=', 'boundary=', '"' ), '', $charset ) );
								$charset = '';
							}
						} else {
							$content_type = trim( $content );
						}
						break;
					case 'cc':
						$cc = array_merge( (array) $cc, explode( ',', $content ) );
						break;
					case 'bcc':
						$bcc = array_merge( (array) $bcc, explode( ',', $content ) );
						break;
					default:
						// Add it to our grand headers array
						$headers[trim( $name )] = trim( $content );
						break;
				}
			}
		}
	}

	// Empty out the values that may be set
	$phpmailer->ClearAddresses();
	$phpmailer->ClearAllRecipients();
	$phpmailer->ClearAttachments();
	$phpmailer->ClearBCCs();
	$phpmailer->ClearCCs();
	$phpmailer->ClearCustomHeaders();
	$phpmailer->ClearReplyTos();

	// From email and name
	// If we don't have a name from the input headers
	if ( !isset( $from_name ) )
		$from_name = 'WordPress';

	/* If we don't have an email from the input headers default to wordpress@$sitename
	 * Some hosts will block outgoing mail from this address if it doesn't exist but
	 * there's no easy alternative. Defaulting to admin_email might appear to be another
	 * option but some hosts may refuse to relay mail from an unknown domain. See
	 * http://trac.wordpress.org/ticket/5007.
	 */

	if ( !isset( $from_email ) ) {
		// Get the site domain and get rid of www.
		$sitename = strtolower( $_SERVER['SERVER_NAME'] );
		if ( substr( $sitename, 0, 4 ) == 'www.' ) {
			$sitename = substr( $sitename, 4 );
		}

		$from_email = 'wordpress@' . $sitename;
	}

	// Plugin authors can override the potentially troublesome default
	$phpmailer->From     = apply_filters( 'wp_mail_from'     , $from_email );
	$phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name  );

	// Set destination addresses
	if ( !is_array( $to ) )
		$to = explode( ',', $to );

	foreach ( (array) $to as $recipient ) {
		try {
			// Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
			$recipient_name = '';
			if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
				if ( count( $matches ) == 3 ) {
					$recipient_name = $matches[1];
					$recipient = $matches[2];
				}
			}
			$phpmailer->AddAddress( $recipient, $recipient_name);
		} catch ( phpmailerException $e ) {
			continue;
		}
	}

	// Set mail's subject and body
	$phpmailer->Subject = $subject;
	$phpmailer->Body    = $message;

	// Add any CC and BCC recipients
	if ( !empty( $cc ) ) {
		foreach ( (array) $cc as $recipient ) {
			try {
				// Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
				$recipient_name = '';
				if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
					if ( count( $matches ) == 3 ) {
						$recipient_name = $matches[1];
						$recipient = $matches[2];
					}
				}
				$phpmailer->AddCc( $recipient, $recipient_name );
			} catch ( phpmailerException $e ) {
				continue;
			}
		}
	}

	if ( !empty( $bcc ) ) {
		foreach ( (array) $bcc as $recipient) {
			try {
				// Break $recipient into name and address parts if in the format "Foo <bar@baz.com>"
				$recipient_name = '';
				if( preg_match( '/(.*)<(.+)>/', $recipient, $matches ) ) {
					if ( count( $matches ) == 3 ) {
						$recipient_name = $matches[1];
						$recipient = $matches[2];
					}
				}
				$phpmailer->AddBcc( $recipient, $recipient_name );
			} catch ( phpmailerException $e ) {
				continue;
			}
		}
	}

	// Set to use PHP's mail()
	$phpmailer->IsMail();

	// Set Content-Type and charset
	// If we don't have a content-type from the input headers
	if ( !isset( $content_type ) )
		$content_type = 'text/plain';

	$content_type = apply_filters( 'wp_mail_content_type', $content_type );

	$phpmailer->ContentType = $content_type;

	// Set whether it's plaintext, depending on $content_type
	if ( 'text/html' == $content_type )
		$phpmailer->IsHTML( true );

	// If we don't have a charset from the input headers
	if ( !isset( $charset ) )
		$charset = get_bloginfo( 'charset' );

	// Set the content-type and charset
	$phpmailer->CharSet = apply_filters( 'wp_mail_charset', $charset );

	// Set custom headers
	if ( !empty( $headers ) ) {
		foreach( (array) $headers as $name => $content ) {
			$phpmailer->AddCustomHeader( sprintf( '%1$s: %2$s', $name, $content ) );
		}

		if ( false !== stripos( $content_type, 'multipart' ) && ! empty($boundary) )
			$phpmailer->AddCustomHeader( sprintf( "Content-Type: %s;\n\t boundary=\"%s\"", $content_type, $boundary ) );
	}

	if ( !empty( $attachments ) ) {
		foreach ( $attachments as $attachment ) {
			try {
				$phpmailer->AddAttachment($attachment);
			} catch ( phpmailerException $e ) {
				continue;
			}
		}
	}

	do_action_ref_array( 'phpmailer_init', array( &$phpmailer ) );

	// Send!
	try {
		return $phpmailer->Send();
	} catch ( phpmailerException $e ) {
		return false;
	}
}
endif;

就是在 WP Mail 上加一个函数,以便使用 BCMS 发邮件。

第五步:添加相关 Class

新建一个文件,命名为 Bcms.class.php,放在 wp-includes\Bcms.class.php,写入以下代码:

下载bcms.zip,解压到 wp-includes 目录下,最终处理好的文件结构为:

  • wp-includes\Bcms.class.php
  • wp-includes\lib\BaeBase.class.php
  • wp-includes\lib\BaeException.class.php
  • wp-includes\lib\BcmsException.class.php
  • wp-includes\lib\RequestCore.class.php

目的是为了用 BCMS 发邮件。

第六步:添加 BCS Support 插件

http://wordpress.org/plugins/bcs-support/下载 BCS Support 插件,添加进去。

目的是为了使用百度云存储。

第七步:添加 Sitemap 插件

  1. 新建 SiteMap.xsl,写入以下代码:
    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet version="2.0" 
                    xmlns:html="http://www.w3.org/TR/REC-html40"
                    xmlns:sitemap="http://www.sitemaps.org/schemas/sitemap/0.9"
                    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    	<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
    	<xsl:template match="/">
    		<html xmlns="http://www.w3.org/1999/xhtml">
    			<head>
    				<title>XML Sitemap</title>
    				<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    				<style type="text/css">
    					body {
    						font-family:"Lucida Grande","Lucida Sans Unicode",Tahoma,Verdana;
    						font-size:13px;
    					}
    
    					#intro {
    						background-color:#CFEBF7;
    						border:1px #2580B2 solid;
    						padding:5px 13px 5px 13px;
    						margin:10px;
    					}
    
    					#intro p {
    						line-height:	16.8667px;
    					}
    
    					td {
    						font-size:11px;
    					}
    
    					th {
    						text-align:left;
    						padding-right:30px;
    						font-size:11px;
    					}
    
    					tr.high {
    						background-color:whitesmoke;
    					}
    
    					#footer {
    						padding:2px;
    						margin:10px;
    						font-size:8pt;
    						color:gray;
    					}
    
    					#footer a {
    						color:gray;
    					}
    
    					a {
    						color:black;
    					}
    				</style>
    			</head>
    			<body>
    				<h1>XML Sitemap</h1>
    				<div id="intro">
    					<p>
    						This is a XML Sitemap which is supposed to be processed by search engines like <a href="http://www.google.com">Google</a>, <a href="http://search.msn.com">MSN Search</a> and <a href="http://www.yahoo.com">YAHOO</a>.<br />
    						It was generated using the Blogging-Software <a href="http://wordpress.org/">WordPress</a> and the <a href="http://www.arnebrachhold.de/redir/sitemap-home/" title="Google Sitemap Generator Plugin for WordPress">Google Sitemap Generator Plugin</a> by <a href="http://www.arnebrachhold.de/">Arne Brachhold</a>.<br />
    						You can find more information about XML sitemaps on <a href="http://sitemaps.org">sitemaps.org</a> and Google's <a href="http://code.google.com/sm_thirdparty.html">list of sitemap programs</a>.
    					</p>
    				</div>
    				<div id="content">
    					<table cellpadding="5">
    						<tr style="border-bottom:1px black solid;">
    							<th>URL</th>
    							<th>Priority</th>
    							<th>Change Frequency</th>
    							<th>LastChange (GMT)</th>
    						</tr>
    						<xsl:variable name="lower" select="'abcdefghijklmnopqrstuvwxyz'"/>
    						<xsl:variable name="upper" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
    						<xsl:for-each select="sitemap:urlset/sitemap:url">
    							<tr>
    								<xsl:if test="position() mod 2 != 1">
    									<xsl:attribute  name="class">high</xsl:attribute>
    								</xsl:if>
    								<td>
    									<xsl:variable name="itemURL">
    										<xsl:value-of select="sitemap:loc"/>
    									</xsl:variable>
    									<a href="{$itemURL}">
    										<xsl:value-of select="sitemap:loc"/>
    									</a>
    								</td>
    								<td>
    									<xsl:value-of select="concat(sitemap:priority*100,'%')"/>
    								</td>
    								<td>
    									<xsl:value-of select="concat(translate(substring(sitemap:changefreq, 1, 1),concat($lower, $upper),concat($upper, $lower)),substring(sitemap:changefreq, 2))"/>
    								</td>
    								<td>
    									<xsl:value-of select="concat(substring(sitemap:lastmod,0,11),concat(' ', substring(sitemap:lastmod,12,5)))"/>
    								</td>
    							</tr>
    						</xsl:for-each>
    					</table>
    				</div>
    				<div id="footer">
    					Generated with Google Sitemap Generator Plugin for WordPress by Arne Brachhold. BAE edition Modified by <a href="http://blog.gimhoy.com/" target="_blank">Gimhoy</a>. This XSLT template is released under GPL.
    				</div>
    			</body>
    		</html>
    	</xsl:template>
    </xsl:stylesheet>
  2. 新建 Sitemap.php,写入以下代码:
    <?php
    include 'includes/connection.php';
    require('./wp-blog-header.php');
    header("Content-type: text/xml");
    header('HTTP/1.1 200 OK');
    $posts_to_show = 1000; // 获取文章数量
    echo '<?xml version="1.0" encoding="UTF-8"?>';
    echo '<?xml-stylesheet type="text/xsl" href="/sitemap.xsl"?>'; // XSL地址
    echo '<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
    ?>
    <!-- generated-on=<?php echo get_lastpostdate('blog'); ?> Modified Gimhoy(http://blog.gimhoy.com)-->
      <url>
          <loc><?php echo get_home_url(); ?></loc>
          <lastmod><?php $ltime = get_lastpostmodified(GMT);$ltime = gmdate('Y-m-d\TH:i:s+00:00', strtotime($ltime)); echo $ltime; ?></lastmod>
          <changefreq>daily</changefreq>
          <priority>1.0</priority>
      </url>
    <?php
    header("Content-type: text/xml");
    $myposts = get_posts( "numberposts=" . $posts_to_show );
    foreach( $myposts as $post ) { ?>
      <url>
          <loc><?php the_permalink(); ?></loc>
          <lastmod><?php the_time('c') ?></lastmod>
          <changefreq>monthly</changefreq>
          <priority>0.6</priority>
      </url>
    <?php } // end foreach ?>
    </urlset>

第八步:WordPress 伪静态

修改 App.Conf,用以下代码覆盖:

handlers:
  - url : /
    script: index.php
  - url : /sitemap.xml$
    script : /sitemap.php
  - url : (.*)\.zip$
    script : $1.zip
  - url : (.*)\.txt$
    script : $1.txt
  - url : (.*)\.xsl$
    script : $1.xsl
  - url : (.*)\.css$
    script : $1.css
  - url : (.*)\.js$
    script : $1.js
  - url : (.*)\.jpg$
    script : $1.jpg
  - url : (.*)\.gif$
    script : $1.gif
  - url : (.*)\.png$
    script : $1.png
  - url : (.*)\.jpeg$
    script : $1.jpeg
  - url : (.*)\.php$
    script : $1.php
  - url : /wp\-admin/
    script : /wp-admin/index.php
  - url : /wp\-includes/(.*)
    script : /wp-includes/$1
  - url : /wp\-content/(.*)
    script : /wp-content/$1
  - url : (.*)
    script : /index.php

  - expire : .jpg modify 10 years
  - expire : .swf modify 10 years
  - expire : .png modify 10 years
  - expire : .gif modify 10 years
  - expire : .JPG modify 10 years
  - expire : .ico modify 10 years
  - expire : .txt modify 10 years
  - expire : .xsl modify 10 years

第九步:上传,然后像平时安装 WordPress 一样安装

大功告成!

ProwerV6 主题添加 WordPress PageNavi 教程

之前提到我换了个主题——ProwerV6,但是,这个主题有点不好,它的 PageNavi 样式使用的是“Older Posts”和“Newer Posts”,而不是“PageNavi”,这显然不利于我们翻页查找。所以,我就给它添加了“PageNavi”。

添加方法:

1)修改主题的 Style.css,加入:

.page-numbers {font-weight:700; font-size:1.2em; line-height:34px; border-radius:20px; display:inline-block; width:34px; height:34px; margin:0 0.5em; text-decoration:none;}
.page-numbers {border:1px solid #333; color:#fff; background-color:#444; text-shadow:1px 1px #111;
	background-image:-webkit-linear-gradient(#444,#222);
	background-image:-moz-linear-gradient(#444,#222);
	background-image:-ms-linear-gradient(#444,#222);
	background-image:-o-linear-gradient(#444,#222);
	background-image:linear-gradient(#444,#222);
	box-shadow:0 1px 0 #fff,1px 0 0 #fff,-1px 0 0 #fff,0 -1px 0 #fff,inset 0 1px 0 #888;
}

2)修改主题的 Functions.php,加入:

<?php
	function nikbobo_pagenavi() {
		global $wp_query;   

		$big = 999999999; // need an unlikely integer
    
		$pagination = array(
			'base' => str_replace($big, '%#%', get_pagenum_link( $big )),
			'format' => '',
			'total' => $wp_query->max_num_pages,
			'current' => max(1, get_query_var('paged')),
			'prev_text' => '<',
			'next_text' => '>',
			'show_all' => false,
			'end_size' => 1,
			'mid_size' => 1
		);

		echo paginate_links($pagination); 
	}
?>

3)修改主题的 Index.php,查找:

<?php previous_posts_link(('<')) ?><?php next_posts_link(('>')) ?>

替换为:

<?php nikbobo_pagenavi(); ?>

大功告成!

神秘兮兮的 WPJAM Debug,你也来山寨一个

大家都知道,我爱水煮鱼写了一个“WPJAM DeBug”插件,用于检测 WordPress 的效率。主要是针对“很多同学说 WordPress 的效率很低,但是又说不出哪里效率低的”问题。

可是这个插件竟然要收 ¥4.99,所以,我也折腾了一下,以代码的形势山寨了一个 WPJAM Debug。

功能很简单,在当前页面的 url 后面加上 ?debug,然后页脚就会显示当前页面有多少条 SQL 查询,页面生成花了多少时间,并且把 SQL 按照执行顺序和按照耗时两种方式列出。

其实启用这个功能很简单:

1、编辑 wp-config.php,在合适的地方加上

define('SAVEQUERIES', true);

2、编辑主题的 Functions.php,在末尾加上

<?php
function nikbobo_debug()
{
	if ( isset($_GET['debug']) && current_user_can('level_10') ) {
	echo "Made " . get_num_queries() . " queries in " . timer_stop(0) . " seconds";
	global $wpdb;
	echo "<pre>";
	print_r($wpdb->queries);
	echo "</pre>";
	}
}
add_action('wp_footer', 'nikbobo_debug');
?>

好了,登录试试看吧!在任意页面加上 ?debug,是不是在页脚显示了?各位 WordPresser,大胆根据这个找出令你站点变慢的原因吧!

再次更换主题

因为原主题 Google PageSpeed 得分太低,令我感觉各种不爽,所以,又换了一个主题。

挑来挑去不见喜欢,最终发现。其实 WordPress Twenty Twelve 主题也挺不错的,然后 Copy 一份,动手修修改改,就弄成了现在这副模样。

具体修改有:

  • 更改为 Page Navi(免插件)
  • 去除 CSS 自动大写
  • 静态化侧边栏
  • 添加 Ajax Comments
  • 完善结构化数据
  • 添加版权信息
  • ……

Page Speed  一下,85 分,令我感觉不错了,也许有一段时间不会再换了。

iGoogler 优化教程(四):手写侧边栏提速

这个教程主要是方便和我一样使用 iGoogler 主题的人,能给自己的站点提一下速。因为一个效率优化做得那么好的主题,如果你还觉得速度慢,那真的是你不会使用了。WordPress 小工具其实是速度变慢的根源之一,因为这会引发许许多多的数据查询。所以,尝试自己手写 SideBar 吧,这并不难。而且,这样也有利于我们使用 WordPress Transients APIWordPress Object Cache 等缓存。

如果你不会写,那么可以参考我的:

打开主题的 sidebar_l.php,全部替换为:

<h3 class="lbar_title">
	<a><?php _e('分类目录')?> </a>
</h3>
<div class="lbar_content">
	<ul>
		<?php wp_list_categories('show_count=1&hierarchical=1depth=2&title_li='); ?>
	</ul>
</div>
<h3 class="lbar_title">
	<a><?php _e('时间选择');?> </a>
</h3>
<div class="lbar_content">
	<ul>
		<?php wp_get_archives(); ?>
	</ul>
</div>
<h3 class="lbar_title">
	<a><?php _e('标签云')?> </a>
</h3>
<div class="lbar_content">
	<ul>
		<?php wp_tag_cloud('smallest=10&largest=14&number=20&order=RAND'); ?>
	</ul>
</div>
<h3 class="lbar_title">
	<a class="current"><?php _e('友情链接')?> </a>
</h3>
<div class="lbar_content" style="display:block;">
	<ul>
		<?php wp_list_bookmarks('title_li=&categorize=0');?>
	</ul>
</div>
<div class="line"></div>

打开主题的 sidebar_r.php,全部替换为:

<h3>最新文章</h3>
<ul>
	<?php echo get_latest_posts_igoogler(5); ?>
</ul>
<h3>随便看看</h3>
<ul>
	<?php echo get_random_posts_igoogler(5); ?>
</ul>
<h3>最受欢迎</h3>
<ul>
	<?php
	$most_comments_posts_data = get_transient('most_comments_posts');
	if(false === $most_comments_posts_data){
		$most_comments_posts_data = get_most_comments_posts_igoogler(5);
		set_transient('most_comments_posts', $most_comments_posts_data, 86400);
	}
	echo $most_comments_posts_data;
	?>
</ul>

效果可参考本站。

沁园春 • 放学

今天在网上看到一个很有意思的段子,在接送回家的路上,深有所感,略作修改:

放学路上,千里车流,万里人潮。望大街内外,车行如龟,司机烦躁,一步不动,总是红灯憋出尿。交通如此多焦,引无数学生上公交。惜奥迪A6,慢如蜗牛。奔驰宝马,无处发飙。一代天骄,兰博基尼,泪看电驴子把车超。俱往矣,还数自行车,边蹬边笑。

其实这首诗有点儿万能,很多都能套上去。

iGoogler 优化教程(三):巧用 PostMeta 提速

这个教程主要是方便和我一样使用 iGoogler 主题的人,能给自己的站点提一下速。因为一个效率优化做得那么好的主题,如果你还觉得速度慢,那真的是你不会使用了。

下面我将以 iGoogler 主题的“相关文章”为例,说一下如何巧用 PostMeta 提速。

我们知道 “相关文章”的获取原理是通过文章的 Tags 来获取相关文章,相同的 Tag 越多,相关性越强,这样就需要进行大量的数据查询,大大降低了站点的效率。但是,一篇文章,在短时间内,它的“相关文章”是不会变动的。所以我们就可以使用缓存对“相关文章”进行加速。

第一个方法是使用 WordPress Object Cache,这个方法需要配合服务器设置或插件,,否则基本没用,这也许会令某些人大感不爽。

所以下面我就要讲第二个方法——巧用 PostMeta:

PostMeta 加速原理

我们知道,PostMeta 表是用来存储 Post 其他自定义字段,比如你可以在 WordPress 编辑界面自定义字段那里使用它。那么我可以把当前文章的“相关文章”数据写到 PostMeta 中去,并记录写入的时间。然后再取出,就可以达到加速的效果。

巧用 PostMeta 提速

下面是把“相关文章”的数据写入缓存(PostMeta)的例子,缓存时间为一个小时,即 3600 秒。

打开主题的 loop_single.php,将

<?php echo wp_get_related_posts_igoogler(10); ?>

替换为

<?php
global $id;
$related_posts_data_old = get_post_meta($id, "related_posts", $single = true);
if($related_posts_data_old){
	$time = time();
	if(($time - $related_posts_data_old["time"]) < 3600){
		echo $related_posts_data_old["related_posts"];
	}
}else{
	$related_posts_data = wp_get_related_posts_igoogler(10);
	$related_posts_data_new = array("time"=>time(), "related_posts"=>$related_posts_data);
	if($related_posts_data_old){
		update_post_meta($id, 'related_posts', $related_posts_data_new);
	}else{
		add_post_meta($id, 'related_posts', $related_posts_data_new, true);
	}
	echo $related_posts_data;
}
?>

代码大意是:如果在缓存(PostMeta)中找不到数据,就查询数据库,然后再将数据写入缓存,并设定过期时间为一个小时,即 3600 秒;如果找得到缓存,并且缓存没有过期,就直接使用缓存的数据。

祝大家中秋快乐

祝大家中秋快乐

据说今年中秋最圆月,下一次要等8年,所以我也拍张照,显摆一下,没器材,凑合着吧

中秋圆月

iGoogler 优化教程(二):使用 WordPress Transients API

这个教程主要是方便和我一样使用 iGoogler 主题的人,能给自己的站点提一下速。因为一个效率优化做得那么好的主题,如果你还觉得速度慢,那真的是你不会使用了。

WordPress Transients API 简介

Transients 是瞬时的意思,WordPress 的 Transients API 就是 WordPress 用来存储一些缓存的数据到数据库中最简单也是最标准的方法,Transients API 给这些缓存的数据一个过期时间,并且时间到就会自动删除,所以如果你需要存储一些有生命周期的选项的时候,Transients API 就是最好的选择。

更让人感到欣喜的是,WordPress Transients API 不需要配合任何的服务器设置和插件。

使用 WordPress Transients API

下面我将以 iGoogler 主题的“最多评论的文章”为例,说一下如何使用 WordPress Transients API。

我们知道 “最多评论的文章”的获取需要进行大量的数据查询,大大降低了站点的效率。但是在短时间内,一个博客的“最多评论的文章”是不会变动的,所以我们就可以使用 WordPress Transients API 对“最多评论的文章”进行加速。

下面是把“相关文章”的数据写入缓存的例子,缓存时间为一天,即 86400 秒。

打开主题的 sidebar_r.php,将

<?php echo get_most_comments_posts_igoogler(5);?>

替换为

<?php
$most_comments_posts_data = get_transient('most_comments_posts');
if(false === $most_comments_posts_data){
	$most_comments_posts_data = get_most_comments_posts_igoogler(5);
	set_transient('most_comments_posts', $most_comments_posts_data, 86400);
}
echo $most_comments_posts_data;
?>

代码大意是:如果在缓存中找不到数据,就查询数据库,然后再将数据写入缓存,并设定过期时间为一天,即 86400 秒;如果找得到缓存,并且缓存没有过期,就直接使用缓存的数据。

由于我的博客较小,所以不需要太短的时间(在短时间内不会变动),如果你的博客较大,并且配上了其他缓存插件,你可以尝试使用“我爱水煮鱼”推荐的一个值——12 小时,即 43200 秒。

iGoogler 优化教程(一):使用 WordPress Object Cache

这个教程主要是方便和我一样使用 iGoogler 主题的人,能给自己的站点提一下速。因为一个效率优化做得那么好的主题,如果你还觉得速度慢,那真的是你不会使用了。

WordPress Object Cache 简介

WordPress Object Cache(WordPress 对象缓存)是 WordPress 自身的缓存机制,它的主要功能是把负责查询或者运算的结果按照 Key-Value 这样的方式(WordPress 还支持按照 Group 来划分和避免缓存的内容冲突)存储到对象里面,下次进行同样操作的时候,可以让直接从这个对象中取出数据,不用重复到数据库中或者其他外部网站获取数据。

由于 WordPress Object Cache 的特性,这个自身的缓存机制注定要配合服务器设置(财大气粗呀~)以及插件。如果你和我一样,没办法接触到类似于 Memcached 之类的东西的话,我推荐你使用 WP File Cache,他能帮助你把数据查询缓存到文件中。这样效率和速度都非常快的。如果不配合服务器设置和插件的话,个人感觉是没有什么作用的。

使用 WordPress Object Cache

下面我将以 iGoogler 主题的“相关文章”为例,说一下如何使用 WordPress Object Cache。

我们知道 “相关文章”的获取原理是通过文章的 Tags 来获取相关文章,相同的 Tag 越多,相关性越强,这样就需要进行大量的数据查询,大大降低了站点的效率。但是,一篇文章,在短时间内,它的“相关文章”是不会变动的,所以我们就可以使用 WordPress Object Cache 对“相关文章”进行加速。

下面是把“相关文章”的数据写入缓存的例子,缓存时间为一个小时,即 3600 秒。

打开主题的 loop_single.php,将

<?php echo wp_get_related_posts_igoogler(10); ?>

替换为

<?php
global $post;
$related_posts_data = wp_cache_get($post->ID, 'related_posts');
if(false === $related_posts_data){
	$related_posts_data = wp_get_related_posts_igoogler(10);
	wp_cache_set($post->ID, $related_posts_data, 'related_posts', 3600);
}
echo $related_posts_data;
?>

代码大意是:如果在缓存中找不到数据,就查询数据库,然后再将数据写入缓存,并设定过期时间为一个小时,即 3600 秒;如果找得到缓存,并且缓存没有过期,就直接使用缓存的数据。