Wr 自留地 Wr 自留地
  • 首页
  • 资源
  • 工具
  • 教程
  • 标签
  • 友链
  • 关于

WordPress 各种自定义 Functions.php 合集

_Wr_ 3年前
WordPress 各种自定义 Functions.php 合集-Wr 自留地

注意:以下内容均可集成到主题和插件。

使用 Gravatar 加速镜像

Gravatar加速镜像由WP-China-Yes提供

// Gravatar 头像加速
add_filter('get_avatar', 'wpcy_get_avatar');
function wpcy_get_avatar($avatar) {
    /*Gravatar加速镜像由WP-China-Yes提供*/
    $gr = 'avatar.ibadboy.net';
    if (strpos($gr, '/avatar') || strpos($gr, '/gravatar')) {
        $avatar = preg_replace("/(www|secure|\d).gravatar.com\/avatar/", $gr, $avatar);
    } else {
        $avatar = preg_replace("/(www|secure|\d).gravatar.com/", $gr, $avatar);
    }

    return $avatar;
}

使用 Memcached 内存缓存优化 WordPress 自动草稿功能

本代码可避免文章 ID 不连续的问题。需要配合 PHP 的 Memcached 拓展一起使用。

代码来自于:https://www.dujin.org/15317.html

//使用 Memcached 内存缓存优化 WordPress 自动草稿功能
add_action('current_screen', function ($current_screen){
	// 只有新建文章的时候才执行
	if($screen_base != 'post' || $current_screen->post_type == 'attachment' || $current_screen->action != 'add'){
		return;
	}

	//如果内存中已有上次创建的自动草稿
	if($last_post_id = wp_cache_get(get_current_user_id(), 'wpjam_'.$current_screen->post_type.'_last_post_id')){
		$post = get_post($last_post_id);
		if($post && $post->post_status == 'auto-draft'){
			wp_redirect(admin_url('post.php?post='.$last_post_id.'&action=edit'));
			exit;
		}
	}

	add_action('admin_footer', function(){
		global $post;
		//将自动草稿ID缓存到内存中
		wp_cache_set(get_current_user_id(), $post->ID, 'wpjam_'.$post->post_type.'_last_post_id', HOUR_IN_SECONDS);
	});
}, 10, 2);

使用 Memcached 内存缓存优化 WordPress 后台媒体库月份获取加载

本代码加快后台媒体库加载速度。需要配合 PHP 的 Memcached 拓展一起使用。

代码来自于:https://www.dujin.org/14221.html

// 使用 Memcached 内存缓存优化 WordPress 后台媒体库月份获取加载
add_filter('media_library_months_with_files', function($months){
	$months	= get_transient('wpjam_media_library_months');

	if($months === false) {
		global $wpdb;

		$months = $wpdb->get_results("SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month FROM $wpdb->posts WHERE post_type = 'attachment' ORDER BY post_date DESC");

		set_transient('wpjam_media_library_months', $months, WEEK_IN_SECONDS);
	}

	return $months;
});

当评论有新回复时发送邮件

当你的主题(比如 PandaPRO 1.1.1)不支持当评论有新回复时发送邮件时,可以使用以下代码。

// 当评论有新回复时发送邮件
function all_mail_notify($all_id) {
  $all = get_all($all_id);
  $parent_id = $all->all_parent ? $all->all_parent : '';
  $spam_confirmed = $all->all_approved;
  if (($parent_id != '') && ($spam_confirmed != 'spam')) {
    $wp_email = 'wrtech@vip.qq.com'; //e-mail 发出点, no-reply 可改为可用的 e-mail.
    $to = trim(get_all($parent_id)->all_author_email);
    $subject = '您在 [' . get_option("blogname") . '] 的留言有了回复';
    $message = '
    <div style="background-color:#eef2fa; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px;">
      <p>' . trim(get_all($parent_id)->all_author) . ', 您好!</p>
      <p>您曾在《' . get_the_title($all->all_post_ID) . '》的留言:<br />'
       . trim(get_all($parent_id)->all_content) . '</p>
      <p>' . trim($all->all_author) . ' 给您的回复:<br />'
       . trim($all->all_content) . '<br /></p>
      <p>欢迎再度光临 ' . get_option('blogname') . '</p>
      <p>(此邮件由系统自动发送,请勿回复.)</p>
    </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 );
    //echo 'mail to ', $to, '<br/> ' , $subject, $message; // for testing
  }
}
add_action('all_post', 'all_mail_notify');

WordPress 评论通过审核后邮件通知评论人

当你的主题(比如 PandaPRO 1.1.1)不支持当评论有新回复时发送邮件时,可以使用以下代码。

本代码来自于:https://www.wpdaxue.com/all-approved-email.html

// WordPress 评论通过审核后邮件通知评论人
add_action('all_unapproved_to_approved', 'wpdx_all_approved');
function wpdx_all_approved($all){
    if (is_email($all->all_author_email)){
        $post_link = get_permalink($all->all_post_ID);
        $title = '您在[' . get_bloginfo('name') . ']的评论已通过审核';
 
        $body = '您在《<a href="' . $post_link . '" target="_blank" >' . get_the_title($all->all_post_ID) . '</a>》中发表的评论已通过审核!<br /><br />';
        $body .= '<strong>您的评论:</strong><br />';
        $body .= strip_tags($all->all_content) . '<br /><br />';
        $body .= '您可以:<a href="' . get_all_link($all->all_ID) . '" target="_blank">查看您的评论</a>  |  <a href="' . $post_link . '#alls" target="_blank">查看其他评论</a>  |  <a href="' . $post_link . '" target="_blank">再次阅读文章</a><br /><br />';
        $body .= '欢迎再次光临[<a href="' . get_bloginfo('url') . '" target="_blank" title="' . get_bloginfo('description') . '">' . get_bloginfo('name') . '</a>]。';
        $body .= '<br /><br />注:此邮件为系统自动发送,请勿直接回复';
 
        @wp_mail($all->all_author_email, $title, $body, "Content-Type: text/html; charset=UTF-8");
    }
}

WordPress外链新窗口打开并使用php页面go跳转

本代码来自于:https://www.dujin.org/12762.html

首先你需要在网站根目录新建一个 index.php 文件

<?php
/**
* WordPress外链go跳转页面 - https://www.dujin.org/12762.html
* @copyright (c) Emlog All Rights Reserved
*/
//自定义跳转地址
$cars = array(
array("wr",'https://wrsblog.cn'),
array("wrsblog",'https://wrsblog.cn'),

);

if(strlen($_SERVER['REQUEST_URI']) > 384 || strpos($_SERVER['REQUEST_URI'], "eval(") || strpos($_SERVER['REQUEST_URI'], "base64")) {
@header("HTTP/1.1 414 Request-URI Too Long");
@header("Status: 414 Request-URI Too Long");
@header("Connection: Close");
@exit;
}
//通过QUERY_STRING取得完整的传入数据,然后取得url=之后的所有值,兼容性更好
$t_url = htmlspecialchars(preg_replace('/^url=(.*)$/i','$1',$_SERVER["QUERY_STRING"]));

//此处可以自定义一些特别的外链,不需要可以删除以下5行
foreach($cars as $k=>$val){
if($t_url==$val[0] ) {
$t_url = $val[1];
$t_vip = 1;
}
}

在 functions.php 输入以下代码:

// WordPress外链新窗口打开并使用php页面go跳转
function the_content_nofollow($content){
preg_match_all('/<a(.*?)href="(.*?)"(.*?)>/',$content,$matches);
if($matches){
foreach($matches[2] as $val){
if(strpos($val,'://')!==false && strpos($val,home_url())===false && !preg_match('/\.(jpg|jepg|png|ico|bmp|gif|tiff)/i',$val)){
$content=str_replace("href=\"$val\"", "href=\"".home_url()."/go/?url=$val\" ",$content);
}
}
}
return $content;
}
add_filter('the_content','the_content_nofollow',999);

未完待续...

WordPress建站美化
相关文章
  • [教程] 腾讯云服务器 Ubuntu 实例开启 root 用户通过密码 SSH 远程登录教程
  • [教程]《索尼克大冒险 2》PC 版 Mod 制作教程 (一): 各文件/目录的详细作用
  • Xiuno 付费插件购买分享
  • WordPress教程:查看当天用户注册数量以及用户注册时间排序
  • 去除 WordPress 管理菜单栏 WordPress logo
_Wr_「作者」
赞赏
_Wr_
评论 (0)
发表评论
Copyright © 2019-2023 Wr 自留地.        
  • 首页
  • 资源
  • 工具
  • 教程
  • 标签
  • 友链
  • 关于