#调用文章的第一张图片 function catch_that_image() { global $post, $posts; $first_img = ‘'; ob_start(); ob_end_clean(); $output = preg_match_all('/<img.+src='"['"].*>/i’, $post->post_content, $matches); $first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = '/images/default.jpg';
}
$first_img = '<img width="430" height="200" src="'.$first_img.'">';
return $first_img;
}
此功能我用在文章列表。代替缩略图(设置特色图片很麻烦啊)。
#移除头部多余信息 remove_action(‘wp_head’,‘wp_generator’);//禁止在head泄露wordpress版本号 remove_action(‘wp_head’,‘rsd_link’);//移除head中的rel="EditURI” remove_action(‘wp_head’,‘wlwmanifest_link’);//移除head中的rel="wlwmanifest” remove_action(‘wp_head’, ‘adjacent_posts_rel_link_wp_head’, 10, 0 );//rel=pre remove_action(‘wp_head’, ‘wp_shortlink_wp_head’, 10, 0 );//rel=shortlink remove_action(‘wp_head’, ‘rel_canonical’ ); remove_action(‘wp_head’, ‘alternate’ ); //Removing Emoji code remove_action( ‘wp_head’, ‘print_emoji_detection_script’, 7 ); remove_action( ‘admin_print_scripts’, ‘print_emoji_detection_script’ ); remove_action( ‘wp_print_styles’, ‘print_emoji_styles’ ); remove_action( ‘admin_print_styles’, ‘print_emoji_styles’ );
#禁用REST API/移除wp-json链接的方法(4.4)
add_filter('rest_enabled', '__return_false');
add_filter('rest_jsonp_enabled', '__return_false');
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );
#禁止全英文评论
function scp_comment_post( $incoming_comment ) {
$pattern = '/[一-龥]/u';
// 禁止全英文评论
if(!preg_match($pattern, $incoming_comment['comment_content'])) {
wp_die( "You should type some Chinese word (like \"你好\") in your comment to pass the spam-check, thanks for your patience! 您的评论中必须包含汉字!" );
}
return( $incoming_comment );
}
add_filter('preprocess_comment', 'scp_comment_post');
挺好用的,唯一发现的问题是,当主题用了AJAX-COMMENT,没有正确的提示。暂时还没研究出方案来
#禁用密码找回功能
/*
*禁用密码找回功能
*/
function disable_reset_lost_password()
{
return false;
}
add_filter( ‘allow_password_reset’, ‘disable_reset_lost_password’);
单人博客直接禁用。
#评论回复邮件通知,带简单样式
//评论回复邮件通知,带简单样式
function comment_mail_notify($comment_id) {
$admin_email = get_bloginfo ('admin_email');
$comment = get_comment($comment_id);
$comment_author_email = trim($comment->comment_author_email);
$parent_id = $comment->comment_parent ? $comment->comment_parent : '';
$to = $parent_id ? trim(get_comment($parent_id)->comment_author_email) : '';
$spam_confirmed = $comment->comment_approved;
if (($parent_id != '') && ($spam_confirmed != 'spam') && ($to != $admin_email)) {
$wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
$subject = '您在 [' . get_option("blogname") . '] 的留言有了新回复';
$message = '
<div style="background-color:#fff; border:1px solid #666666; color:#111; -moz-border-radius:8px; -webkit-border-radius:8px; -khtml-border-radius:8px; border-radius:8px; font-size:12px; width:702px; margin:0 auto; margin-top:10px;">
<div style="background:#666666; width:100%; height:60px; color:white; -moz-border-radius:6px 6px 0 0; -webkit-border-radius:6px 6px 0 0; -khtml-border-radius:6px 6px 0 0; border-radius:6px 6px 0 0; ">
<span style="height:60px; line-height:60px; margin-left:30px; font-size:12px;"> 您在<a style="text-decoration:none; color:#ff0;font-weight:600;"> [' . get_option("blogname") . '] </a>上的留言有回复啦!</span></div>
<div style="width:90%; margin:0 auto">
<p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
<p>您在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br />
<p style="background-color: #EEE;border: 1px solid #DDD;padding: 20px;margin: 15px 0;">'. trim(get_comment($parent_id)->comment_content) . '</p>
<p>' . trim($comment->comment_author) . ' 给你的回复:<br />
<p style="background-color: #EEE;border: 1px solid #DDD;padding: 20px;margin: 15px 0;">'. trim($comment->comment_content) . '</p>
<p>你可以点击<a href="' . htmlspecialchars(get_comment_link($parent_id, array('type' => 'comment'))) . '">查看完整内容</a></p>
<p>欢迎再度光临<a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
<p>(此邮件由系统自动发出, 请勿回复。)</p>
</div></div>';
$from = "From: \"" . get_option('blogname') . "\" <$wp_email>";
$headers = "$from\nContent-Type: text/html; charset=" . get_option('blog_charset') . "\n";
wp_mail( $to, $subject, $message, $headers );
}
}
add_action('comment_post', 'comment_mail_notify');
好些博客都在用
#关闭FEED
//关闭FEED
function v7v3_disable_feed() {
wp_die(__('<h1>本博客不再提供 Feed,请访问网站<a href="'.get_bloginfo('url').'">首页</a>!</h1>'));
}
add_action('do_feed', 'v7v3_disable_feed', 1);
add_action('do_feed_rdf', 'v7v3_disable_feed', 1);
add_action('do_feed_rss', 'v7v3_disable_feed', 1);
add_action('do_feed_rss2', 'v7v3_disable_feed', 1);
add_action('do_feed_atom', 'v7v3_disable_feed', 1);
=============================================== #CDN代码 如果使用了wp-super-cache插件。用插件自带的CDN即可~
/* *CDN代码 * */ define(‘FocusCDNHost’,‘http://www.xxx.com’);//wordpress网站网址 define(‘FocusCDNRemote’,‘http://cdn.xxx.com’);//cdn域名 define(‘FocusCDNIncludes’,‘wp-content,wp-includes’);//设置加速目录 define(‘FocusCDNExcludes’,'.php|.xml|.html|.po|.mo’);//设置文件白名单 define(‘FocusCDNRelative’,'');//Check this if you want to have links like <wp-content/abc.png> rewritten - i.e. without your blog’s domain as prefix.
function do_cdnrewrite_ob_start() {
$rewriter = new FocusCDNRewriteWordpress();
$rewriter->register_as_output_buffer();
}
add_action('template_redirect', 'do_cdnrewrite_ob_start');
class FocusCDNRewriteWordpress extends FocusCDNRewrite
{
function __construct() {
$excl_tmp = FocusCDNExcludes;
$excludes = array_map('trim', explode('|', $excl_tmp));
parent::__construct(
FocusCDNHost,
FocusCDNRemote,
FocusCDNIncludes,
$excludes,
!!FocusCDNRelative
);
}
public function register_as_output_buffer() {
if ($this->blog_url != FocusCDNRemote) {
ob_start(array(&$this, 'rewrite'));
}
}
}
class FocusCDNRewrite {
var $blog_url = null;
var $cdn_url = null;
var $include_dirs = null;
var $excludes = array();
var $rootrelative = false;
function __construct($blog_url, $cdn_url, $include_dirs, array $excludes, $root_relative) {
$this->blog_url = $blog_url;
$this->cdn_url = $cdn_url;
$this->include_dirs = $include_dirs;
$this->excludes = $excludes;
$this->rootrelative = $root_relative;
}
protected function exclude_single(&$match) {
foreach ($this->excludes as $badword) {
if (stristr($match, $badword) != false) {
return true;
}
}
return false;
}
protected function rewrite_single(&$match) {
if ($this->exclude_single($match[0])) {
return $match[0];
} else {
if (!$this->rootrelative || strstr($match[0], $this->blog_url)) {
return str_replace($this->blog_url, $this->cdn_url, $match[0]);
} else {
return $this->cdn_url . $match[0];
}
}
}
protected function include_dirs_to_pattern() {
$input = explode(',', $this->include_dirs);
if ($this->include_dirs == '' || count($input) < 1) {
return 'wp\-content|wp\-includes';
} else {
return implode('|', array_map('quotemeta', array_map('trim', $input)));
}
}
public function rewrite(&$content) {
$dirs = $this->include_dirs_to_pattern();
$regex = '#(?<=[(\"\'])';
$regex .= $this->rootrelative
? ('(?:'.quotemeta($this->blog_url).')?')
: quotemeta($this->blog_url);
$regex .= '/(?:((?:'.$dirs.')[^\"\')]+)|([^/\"\']+\.[^/\"\')]+))(?=[\"\')])#';
return preg_replace_callback($regex, array(&$this, 'rewrite_single'), $content);
}
}
用于CDN的代码,这个代码虽然多了点,但是至少能用。
用了这个,把JS文件放到CDN服务器上后,并且你的主题还是AJAX评论的,可能会碰到无法评论的问题。 解决方法传: 打开CDN后,并且JS文件也放到了CDN服务器,原来的AJAX评论会失败。经过排查,发现是comments-ajax.js文件里,调用的地址是根据comments-ajax.js来的。 因为JS文件都转移到了CDN。所以调用PHP文件失败。。
做如下修改即可正常使用:
模版文件目录下comments-ajax.js文件第11行:
//第一条是原来的语句,注视掉,改成下面那句。。直接改成comments-ajax.php的完整地址就行
//ajax_php_url = js_url.replace('-ajax.js','-ajax.php'),
ajax_php_url = 'http://www.xxx.com/wp-content/themes/【模版名称】/comments-ajax.php',
===================== #gravatar加速
function get_ssl_avatar($avatar) {
$avatar = str_replace(array("www.gravatar.com", "0.gravatar.com", "1.gravatar.com", "2.gravatar.com"), "secure.gravatar.com", $avatar);
//解决自定义默认头像
if(strpos($avatar,"gravatar.png")) return '<img src="'.get_bloginfo('template_url') .'/images/gravatar.png" class="avatar avatar-40" height="40" width="40">';
return $avatar;
}
add_filter('get_avatar', 'get_ssl_avatar');
把gravatar地址改成secure.gravatar.com
#调试代码(显示SQL语句)
//调试代码(显示SQL语句)
/*
//config.php添加:define('SAVEQUERIES', true);
//config.php修改:define('WP_DEBUG', true);
function nikbobo_debug()
{
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');
*/
#禁止评论网址自动转换为超链接 //禁止评论网址自动转换为超链接 remove_filter(‘comment_text’, ‘make_clickable’, 9);
//HTML代码直接显示
function plc_comment_post( $incoming_comment ) {
$incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
$incoming_comment['comment_content'] = str_replace( "'", ''', $incoming_comment['comment_content'] );
return( $incoming_comment );
}
function plc_comment_display( $comment_to_display ) {
$comment_to_display = str_replace( ''', "'", $comment_to_display );
return $comment_to_display;
}
add_filter( 'preprocess_comment', 'plc_comment_post', '', 1);
add_filter( 'comment_text', 'plc_comment_display', '', 1);
add_filter( 'comment_text_rss', 'plc_comment_display', '', 1);
add_filter( 'comment_excerpt', 'plc_comment_display', '', 1);
#禁用Wordpress自带的wp-cron 在 wp-config.php 添加下面的代码禁用 WP-Cron:
/* Disable background wp-cron */
define('DISABLE_WP_CRON', true);
服务器设置计划任务
crontab -e
每5分钟访问一次wp-cron.php。
*/5 * * * * /usr/bin/wget -q --post-data '' http://www.abc.com/wp-cron.php -O /dev/null
网上不少教程没有提到 “-O /dev/null”。。那样会在用户目录下载无数的空的wp-cron.php文件。。
#上传文件自动重命名 //15位的MD5码作为文件名
function new_filename($filename) {
$info = pathinfo($filename);
$ext = empty($info['extension']) ? '' : '.' . $info['extension'];
$name = basename($filename, $ext);
return substr(md5($name), 0, 15) . $ext;
}
add_filter('sanitize_file_name', 'new_filename', 10);