以下代码是JV必备的。对于优化网站各方面都非常有效:
- //add1-七牛或又拍云CDN加速/////////////////////////////////////////////////
- define('FocusCDNHost','http://www.inlojv.com');//wordpress网站网址
- define('FocusCDNRemote','http://inlowp.qiniudn.com');//cdn域名
- define('FocusCDNIncludes','wp-content,wp-includes,avatar');//设置加速目录
- 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);
- }
- }
- //add2/////////////////////////////////////////
- remove_filter( 'the_content', array( $GLOBALS['wp_embed'], 'autoembed' ), 8 );
- //add3//////////////
- add_filter('xmlrpc_enabled', '__return_false');
- //add4//////////////
- add_action( 'template_redirect', 'rw_relative_urls' );
- function rw_relative_urls() {
- if ( is_feed() || get_query_var( 'sitemap' ) ) //判断是否为feed页面或者sitemap页面
- return;
- $filters = array(
- 'post_link',
- 'post_type_link',
- 'page_link',
- 'attachment_link',
- 'get_shortlink',
- 'post_type_archive_link',
- 'get_pagenum_link',
- 'get_comments_pagenum_link',
- 'term_link',
- 'search_link',
- 'day_link',
- 'month_link',
- 'year_link',
- );
- foreach ( $filters as $filter )
- {
- add_filter( $filter, 'wp_make_link_relative' );
- }
- }
- //add5-强制js底部加载//////////////////
- function ds_print_jquery_in_footer( &$scripts) {
- if ( ! is_admin() )
- $scripts->add_data( 'jquery', 'group', 1 );
- }
- add_action( 'wp_default_scripts', 'ds_print_jquery_in_footer' );
- //add7-优化header///////////////////////
- remove_action( 'wp_head', 'feed_links', 2 ); //移除feed
- remove_action( 'wp_head', 'feed_links_extra', 3 ); //移除feed
- remove_action( 'wp_head', 'rsd_link' ); //移除离线编辑器开放接口
- remove_action( 'wp_head', 'wlwmanifest_link' ); //移除离线编辑器开放接口
- remove_action( 'wp_head', 'index_rel_link' );//去除本页唯一链接信息
- remove_action('wp_head', 'parent_post_rel_link', 10, 0 );//清除前后文信息
- remove_action('wp_head', 'start_post_rel_link', 10, 0 );//清除前后文信息
- remove_action( 'wp_head', 'wp_generator' ); //移除WordPress版本
- remove_action( 'wp_head', 'rel_canonical' );
- remove_action( 'wp_footer', 'wp_print_footer_scripts' );
- remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
- remove_action( 'template_redirect', 'wp_shortlink_header', 11, 0 );
- add_action('widgets_init', 'my_remove_recent_comments_style');
- function my_remove_recent_comments_style() {
- global $wp_widget_factory;
- remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'] ,'recent_comments_style'));
- }
- //add8-异常登录后邮件通知//////////////////////////////////////////
- /*****************************************************
- 函数名称:wp_login_failed_notify v1.0 by DH.huahua.
- 函数作用:有错误登录wp后台就会email通知博主
- ******************************************************/
- function wp_login_failed_notify()
- {
- date_default_timezone_set('PRC');
- $admin_email = get_bloginfo ('admin_email');
- $to = $admin_email;
- $subject = '网站登录错误警告';
- $message = '<p>网站(' . get_option("blogname") . ')有异常登录!</p>' .
- '<p>请确定是您自己的登录失误,以防别人攻击!登录信息如下:</p>' .
- '<p>登录名:' . $_POST['log'] . '<p>' .
- '<p>登录密码:' . $_POST['pwd'] . '<p>' .
- '<p>登录时间:' . date("Y-m-d H:i:s") . '<p>' .
- '<p>登录IP:' . $_SERVER['REMOTE_ADDR'] . '<p>';
- $wp_email = 'no-reply@' . preg_replace('#^www\.#', '', strtolower($_SERVER['SERVER_NAME']));
- $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('wp_login_failed', 'wp_login_failed_notify');
- //add9-屏蔽全英文评论/////////////////////////
- function refused_spam_comments( $comment_data ){
- $pattern = '/[一-龥]/u';
- if(!preg_match($pattern,$comment_data['comment_content'])){
- err( "You should type some Chinese word (like \"你好\") in your comment to pass the spam-check, thanks for your patience!" );
- }
- return( $comment_data );
- }
- add_filter('preprocess_comment','refused_spam_comments');
- ///add10-防止暴力破解//////////////////////////
- ////修改后需要用这个地址才能打开 http://www.inlojv.com/wp-login.php?access=您的密码/////////
- add_action('login_enqueue_scripts','cracker');
- function cracker(){
- if($_GET['access']!= '这里填你的密码') header('Location:http://这里改为你的域名/');
- }
本站文章除注明转载/出处外,均为本站原创或翻译。若要转载但请务必注明出处,尊重他人劳动成果。
转载请注明出处链接 : https://www.inlojv.com/1363.html