2019年5月

取消Wordpress转义

Function代码

//取消内容转义,by minirizhi.com
remove_filter('the_content', 'wptexturize'); 
//取消摘要转义,by minirizhi.com
remove_filter('the_excerpt', 'wptexturize');
//取消评论转义,by minirizhi.com
remove_filter('comment_text', 'wptexturize');

Wordpress评论添加数学验证码,防止垃圾评论。

Function代码

//数学加法验证码,by minirizhi.com
add_action('comment_form_after_fields', 'spam_protection_math', 1, 1);
function spam_protection_math(){
//获取两个随机数, 范围0~9
$num1=rand(0,9);
$num2=rand(0,9);
//最终网页中的具体内容
echo "<div class='comment-form-url form-group has-feedback'><div class='input-group'><div class='input-group-addon'><i class='fa fa-check'></i></div><input type='text' name='sum' aria-required='true' required class='form-control' value='' size='25' tabindex='4' placeholder='$num1 + $num2 = ?'><span class='form-control-feedback required'>*</span></div></div>"
."<input type='hidden' name='num1' value='$num1'>"
."<input type='hidden' name='num2' value='$num2'>";
}
function spam_protection_pre($commentdata){
$sum=$_POST['sum'];//用户提交的计算结果
switch($sum){
//得到正确的计算结果则直接跳出
case $_POST['num1']+$_POST['num2']:break;
//未填写结果时的错误讯息
case null:wp_die('<p align="center">没有输入验证码! <a href="javascript:history.back(-1)">点击返回</a></p>');break;
//计算错误时的错误讯息
default:wp_die('<p align="center">验证码错误! <a href="javascript:history.back(-1)">点击返回</a></p>');
}
return $commentdata;
}
if($comment_data['comment_type']==''){
add_filter('pre_comment_on_post','spam_protection_pre');
}

Wordpress后台登陆添加数学验证码。

Function代码

//后台登陆数学验证码,by minirizhi.com
function minirizhi_add_login_fields() {
  //获取两个随机数, 范围0~9
  $num1=rand(0,9);
  $num2=rand(0,9);
  //最终网页中的具体内容
      echo "<p><label for='math' class='small'>验证码</label><br /> <input type='text' aria-required='true' required name='sum' placeholder='$num1 + $num2 = ?' class='input' value='' size='25' tabindex='4'>"
  ."<input type='hidden' name='num1' value='$num1'>"
  ."<input type='hidden' name='num2' value='$num2'></p>";
}
add_action('login_form','minirizhi_add_login_fields');
//错误提示,by minirizhi.com
function login_val() {
  $sum=$_POST['sum'];//用户提交的计算结果
  switch($sum){
    //得到正确的计算结果则直接跳出
    case $_POST['num1']+$_POST['num2']:break;
    //未填写结果时的错误讯息
    case null:wp_die('<p align="center">没有输入验证码! <a href="javascript:history.back(-1)">点击返回</a></p>');break;
    //计算错误时的错误讯息
    default:wp_die('<p align="center">验证码错误! <a href="javascript:history.back(-1)">点击返回</a></p>');
  }
}
add_action('login_form_login','login_val');

移除后台隐私相关的页面。

Function代码

//移除后台隐私相关的页面,by minirizhi.com
add_action('admin_menu', function (){
    global $menu, $submenu;

    // 移除设置菜单下的隐私子菜单。
    unset($submenu['options-general.php'][45]);

    // 移除工具彩带下的相关页面
    remove_action( 'admin_menu', '_wp_privacy_hook_requests_page' );

    remove_filter( 'wp_privacy_personal_data_erasure_page', 'wp_privacy_process_personal_data_erasure_page', 10, 5 );
    remove_filter( 'wp_privacy_personal_data_export_page', 'wp_privacy_process_personal_data_export_page', 10, 7 );
    remove_filter( 'wp_privacy_personal_data_export_file', 'wp_privacy_generate_personal_data_export_file', 10 );
    remove_filter( 'wp_privacy_personal_data_erased', '_wp_privacy_send_erasure_fulfillment_notification', 10 );

    // Privacy policy text changes check.
    remove_action( 'admin_init', array( 'WP_Privacy_Policy_Content', 'text_change_check' ), 100 );

    // Show a "postbox" with the text suggestions for a privacy policy.
    remove_action( 'edit_form_after_title', array( 'WP_Privacy_Policy_Content', 'notice' ) );

    // Add the suggested policy text from WordPress.
    remove_action( 'admin_init', array( 'WP_Privacy_Policy_Content', 'add_suggested_content' ), 1 );

    // Update the cached policy info when the policy page is updated.
    remove_action( 'post_updated', array( 'WP_Privacy_Policy_Content', '_policy_page_updated' ) );
},9);

Wordpress去除js、css版本后缀,需要以下代码:

Function代码

//去除js、css版本后缀,by minirizhi.com
if(!function_exists('cwp_remove_script_version')){
    function cwp_remove_script_version( $src ){  return remove_query_arg( 'ver', $src ); }
    add_filter( 'script_loader_src', 'cwp_remove_script_version' );
    add_filter( 'style_loader_src', 'cwp_remove_script_version' );
}

Wordpress搜索静态化,将改变为search模式。

Function代码

//搜索静态化,by minirizhi.com
function minirizhi_search_url_rewrite() {
    if ( is_search() && ! empty( $_GET['s'] ) ) {
        wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
        exit();
    }
}
add_action( 'template_redirect', 'minirizhi_search_url_rewrite' );