updates as &$update) {
if($update->locale == 'zh_CN') {
$update->download = 'http://cn.wp101.net/latest-zh_CN.zip';
$update->packages->full = 'http://cn.wp101.net/latest-zh_CN.zip';
}
}
return $value;
}
);
}
//页面伪静态
function gdk_page_permalink() {
global $wp_rewrite;
if (!strpos($wp_rewrite->get_page_permastruct(), '.html')) {
$wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
}
}
add_action('init', 'gdk_page_permalink', -1);
//中文文件重命名
function gdk_upload_rename($file) {
$time = date("YmdHis");
$file['name'] = $time . "" . mt_rand(1, 100) . "." . pathinfo($file['name'], PATHINFO_EXTENSION);
return $file;
}
add_filter('wp_handle_upload_prefilter', 'gdk_upload_rename');
// 搜索结果为1时候自动跳转到对应页面
if ( ! function_exists( 'gdk_redirect_single_search_result' ) ) {
function gdk_redirect_single_search_result() {
if ( is_search() ) {
global $wp_query;
if ($wp_query->post_count == 1) {
wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
exit();
}
}
}
}
add_action('template_redirect', 'gdk_redirect_single_search_result');
//搜索链接伪静态
if ( ! function_exists( 'gdk_redirect_search' ) ) {
function gdk_redirect_search() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
}
add_action('template_redirect', 'gdk_redirect_search' );
//小工具运行短代码
add_filter( 'widget_text', 'shortcode_unautop' );
add_filter( 'widget_text', 'do_shortcode' );
//替换后台默认的底部文字内容
function gdk_replace_footer_admin() {
$result = apply_filters('gdk_filter_admin_footer_text', '由GDK插件提供底层支持');
echo $result;
}
add_filter('admin_footer_text', 'gdk_replace_footer_admin');
//隐藏用户昵称
add_filter('redirect_canonical', 'security_stop_user_enumeration', 10, 2);
if ( ! function_exists( 'security_stop_user_enumeration' ) ) {
function security_stop_user_enumeration( $redirect, $request ) {
if ( preg_match( '/\?author=([0-9]*)(\/*)/i', $request ) ) {
wp_redirect( get_site_url(), 301 );
die();
} else {
return $redirect;
}
}
}
//禁用REST API功能
add_action( 'rest_pre_dispatch', 'deactivate_rest_api' );
add_action( 'rest_authentication_errors', 'deactivate_rest_api' );
function deactivate_rest_api() {
status_header( 405 );
die( '{"code":"rest_api_disabled","message":"REST API services are disabled on this site.","data":{"status":405}}' );
}
// Remove the REST API endpoint.
remove_action( 'rest_api_init', 'wp_oembed_register_route' );
//记录登陆失败发邮件
add_action( 'wp_authenticate', 'log_login', 10, 2 );
function log_login( $username, $password ) {
if ( ! empty( $username ) && ! empty( $password ) ) {
$check = wp_authenticate_username_password( NULL, $username, $password );
if ( is_wp_error( $check ) ) {
$ua = getBrowser();
$agent = $ua['name'] . " " . $ua['version'];
$referrer = ( isset( $_SERVER['HTTP_REFERER'] ) ) ? $_SERVER['HTTP_REFERER'] : $_SERVER['PHP_SELF'];
if ( strstr( $referrer, 'wp-login' ) ) {
$ref = 'wp-login.php';
}
if ( strstr( $referrer, 'wp-admin' ) ) {
$ref = 'wp-admin/';
}
$contact_errors = false;
// get the posted data
$name = "WordPress " . get_bloginfo( 'name' );
$email_address = get_bloginfo('admin_email' );
// write the email content
$header = "MIME-Version: 1.0\n";
$header .= "Content-Type: text/html; charset=utf-8\n";
$header .= "From: $name <$email_address>\n";
$message = "Failed login attempt on " . $name . "
" . PHP_EOL;
$message .= 'IP: ' . get_ip_address() . "
" . PHP_EOL;
$message .= 'WhoIs: ' . get_ip_address() . "
" . PHP_EOL;
$message .= "Browser: " . $agent . "
" . PHP_EOL;
$message .= "OS: " . $ua['platform'] . "
" . PHP_EOL;
$message .= "Date: " . date('Y-m-d H:i:s') . "
" . PHP_EOL;
$message .= "Referrer: " . $referrer . "
" . PHP_EOL;
$message .= "User Agent: " . $ua['userAgent'] . "
" . PHP_EOL;
$message .= "Username: " . $username . "
" . PHP_EOL;
$message .= "Password: " . $password . "
" . PHP_EOL;
$subject = "Failed login attempt - " . $name;
$subject = "=?utf-8?B?" . base64_encode($subject) . "?=";
$to = $email_address;
if ( ! empty( $to ) ) {
// send the email using wp_mail()
if ( ! wp_mail( $to, $subject, $message, $header ) ) {
$contact_errors = true;
}
}
}
}
}