精简wordpress不需要的功能

因为经常要用到,所以在这里记录一下。

remove_action( 'wp_head',   'rsd_link' ); //移除离线编辑器开放接口
remove_action( 'wp_head',   'wlwmanifest_link' ); //移除离线编辑器开放接口
//remove_action( 'wp_head',   'index_rel_link' ); //去除本页唯一链接信息
remove_action( 'wp_head',   'wp_generator' ); //移除WordPress版本
remove_action( 'wp_head',   'wp_resource_hints', 2 );//禁止加载s.w.org
remove_action( 'wp_head',   'feed_links', 2 );//移除feed
remove_action( 'wp_head',   'feed_links_extra', 3);//移除feed
remove_action( 'wp_head',   'wp_shortlink_wp_head' );//移除短连接
remove_action( 'wp_head',   'adjacent_posts_rel_link', 10, 0);//相邻的postsrel-link
remove_filter( 'the_content', 'wptexturize');//取消内容中字符转义
//remove_filter('the_excerpt', 'wptexturize');//取消摘要转义
//remove_filter('comment_text', 'wptexturize');//取消评论转义
add_filter( 'run_wptexturize', '__return_false' );//禁止 WordPress 网站title中的 “-” 被转义
add_filter('show_admin_bar', '__return_false', 9999);//删除最顶部的管理横条
//remove_action( 'wp_head', 'wp_enqueue_scripts', 1 ); //Javascript的调用
/** 禁用自动保存 */
add_action('wp_print_scripts', 'qgg_not_autosave');
function qgg_not_autosave() {
    wp_deregister_script('autosave'); 
}
//禁止加载WP自带的jquery.js
if ( !is_admin() ) { // 后台不禁止
function my_init_method() {
wp_deregister_script( 'jquery' ); // 取消原有的 jquery 定义
}
add_action('init', 'my_init_method');
}
wp_deregister_script( 'l10n' );

add_filter( 'wp_revisions_to_keep', 'specs_wp_revisions_to_keep', 10, 2 );//关闭Wordpress中的修订功能
//add_filter('comment_form_field_cookies','__return_false');//移除评论隐私Cookie 确认存储勾选框
add_filter( 'add_image_size', create_function( '', 'return 1;' ) );//彻底禁止缩略图裁剪
remove_action( 'wp_head', 'locale_stylesheet' );
//remove_action('publish_future_post','check_and_publish_future_post',10, 1 );//发布未来的日志,检查并发布未来的日志
//remove_action( 'wp_head', 'noindex', 1 );//暂时不明
//remove_action( 'wp_head', 'wp_print_styles', 8 );//载入css
remove_action( 'wp_head', 'wp_print_head_scripts', 9 );//载入JS
//remove_action( 'wp_head', 'rel_canonical' );//移除Canonical标记
remove_action( 'wp_footer', 'wp_print_footer_scripts' );//强制所有脚本进入WordPress中的wp_footer
remove_action( 'template_redirect', 'wp_shortlink_header', 11, 0 );//屏蔽WP产生的rel=shortlink头部信息
remove_action( 'template_redirect','rest_output_link_header', 11, 0 );//屏蔽WP自带API产生的头部信息
//清除前后文信息
remove_action( 'wp_head', 'parent_post_rel_link', 10, 0 ); // Removes the prev link 
remove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); // Removes the start link 
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 ); // Removes the relational links for the posts adjacent to the current post.
//屏蔽gravatar,调用多说缓存图片
function duoshuo_avatar($avatar) {
$avatar = str_replace(array("www.gravatar.com","0.gravatar.com","1.gravatar.com","2.gravatar.com"),"gravatar.duoshuo.com",$avatar);
return $avatar;
}
add_filter( 'get_avatar', 'duoshuo_avatar', 10, 3 );
//屏蔽谷歌文字代码1
if (!function_exists('remove_wp_open_sans')) :
    function remove_wp_open_sans() {
        wp_deregister_style( 'open-sans' );
        wp_register_style( 'open-sans', false );
    }
    add_action('wp_enqueue_scripts', 'remove_wp_open_sans');
 
    // Uncomment below to remove from admin
    // add_action('admin_enqueue_scripts', 'remove_wp_open_sans');
endif;
//屏蔽谷歌文字代码2
function remove_open_sans() {    
    wp_deregister_style( 'open-sans' );    
    wp_register_style( 'open-sans', false );    
    wp_enqueue_style('open-sans','');    
}    
add_action( 'init', 'remove_open_sans' );
	//移除wp-json链接
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 );
// 移除头部 wp-json 标签和 HTTP header 中的 link
remove_action('template_redirect', 'rest_output_link_header', 11 );
// 屏蔽登录错误提示
function no_errors_please(){
return '账号或者密码不正确,请重新输入!';
}
add_filter( 'login_errors', 'no_errors_please' );
//禁止生成多张微略图
add_filter( 'intermediate_image_sizes', function( $sizes )
{
    return array_filter( $sizes, function( $val )
    {
        return 'medium_large' !== $val; // Filter out 'medium_large'
    } );
} );
//禁止加载wp-embeds.mins.js
function disable_embeds_init() {
    /* @var WP $wp */
    global $wp; 
    // Remove the embed query var.
    $wp->public_query_vars = array_diff( $wp->public_query_vars, array(
        'embed',
    ) ); 
    // Remove the REST API endpoint.
    remove_action( 'rest_api_init', 'wp_oembed_register_route' );
     // Turn off
    add_filter( 'embed_oembed_discover', '__return_false' );
     // Don't filter oEmbed results.
    remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );
     // Remove oEmbed discovery links.
    remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
     // Remove oEmbed-specific JavaScript from the front-end and back-end.
    remove_action( 'wp_head', 'wp_oembed_add_host_js' );
    add_filter( 'tiny_mce_plugins', 'disable_embeds_tiny_mce_plugin' );
     // Remove all embeds rewrite rules.
    add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' );
}
 add_action( 'init', 'disable_embeds_init', 9999 );
 /**
 * Removes the 'wpembed' TinyMCE plugin.
 *
 * @since 1.0.0
 *
 * @param array $plugins List of TinyMCE plugins.
 * @return array The modified list.
 */
function disable_embeds_tiny_mce_plugin( $plugins ) {
    return array_diff( $plugins, array( 'wpembed' ) );
}
/**
 * Remove all rewrite rules related to embeds.
 *
 * @since 1.2.0
 *
 * @param array $rules WordPress rewrite rules.
 * @return array Rewrite rules without embeds rules.
 */
function disable_embeds_rewrites( $rules ) {
    foreach ( $rules as $rule => $rewrite ) {
        if ( false !== strpos( $rewrite, 'embed=true' ) ) {
            unset( $rules[ $rule ] );
        }
    }
 
    return $rules;
}
/**
 * Remove embeds rewrite rules on plugin activation.
 *
 * @since 1.2.0
 */
function disable_embeds_remove_rewrite_rules() {
    add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' );
    flush_rewrite_rules();
}
 register_activation_hook( __FILE__, 'disable_embeds_remove_rewrite_rules' );
/**
 * Flush rewrite rules on plugin deactivation.
 *
 * @since 1.2.0
 */
function disable_embeds_flush_rewrite_rules() {
    remove_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' );
    flush_rewrite_rules();
}
register_deactivation_hook( __FILE__, 'disable_embeds_flush_rewrite_rules' );
//去除分类标志代码
add_action( 'load-themes.php',  'no_category_base_refresh_rules');
add_action('created_category', 'no_category_base_refresh_rules');
add_action('edited_category', 'no_category_base_refresh_rules');
add_action('delete_category', 'no_category_base_refresh_rules');
function no_category_base_refresh_rules() {
    global $wp_rewrite;
    $wp_rewrite -> flush_rules();
}
// register_deactivation_hook(__FILE__, 'no_category_base_deactivate');
// function no_category_base_deactivate() {
//  remove_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
//  // We don't want to insert our custom rules again
//  no_category_base_refresh_rules();
// }
// Remove category base
add_action('init', 'no_category_base_permastruct');
function no_category_base_permastruct() {
    global $wp_rewrite, $wp_version;
    if (version_compare($wp_version, '3.4', '<')) {
        // For pre-3.4 support
        $wp_rewrite -> extra_permastructs['category'][0] = '%category%';
    } else {
        $wp_rewrite -> extra_permastructs['category']['struct'] = '%category%';
    }
}
// Add our custom category rewrite rules
add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
function no_category_base_rewrite_rules($category_rewrite) {
    //var_dump($category_rewrite); // For Debugging
    $category_rewrite = array();
    $categories = get_categories(array('hide_empty' => false));
    foreach ($categories as $category) {
        $category_nicename = $category -> slug;
        if ($category -> parent == $category -> cat_ID)// recursive recursion
            $category -> parent = 0;
        elseif ($category -> parent != 0)
            $category_nicename = get_category_parents($category -> parent, false, '/', true) . $category_nicename;
        $category_rewrite['(' . $category_nicename . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
        $category_rewrite['(' . $category_nicename . ')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
        $category_rewrite['(' . $category_nicename . ')/?$'] = 'index.php?category_name=$matches[1]';
    }
    // Redirect support from Old Category Base
    global $wp_rewrite;
    $old_category_base = get_option('category_base') ? get_option('category_base') : 'category';
    $old_category_base = trim($old_category_base, '/');
    $category_rewrite[$old_category_base . '/(.*)$'] = 'index.php?category_redirect=$matches[1]';
    //var_dump($category_rewrite); // For Debugging
    return $category_rewrite;
}
// Add 'category_redirect' query variable
add_filter('query_vars', 'no_category_base_query_vars');
function no_category_base_query_vars($public_query_vars) {
    $public_query_vars[] = 'category_redirect';
    return $public_query_vars;
}
// Redirect if 'category_redirect' is set
add_filter('request', 'no_category_base_request');
function no_category_base_request($query_vars) {
    //print_r($query_vars); // For Debugging
    if (isset($query_vars['category_redirect'])) {
        $catlink = trailingslashit(get_option('home')) . user_trailingslashit($query_vars['category_redirect'], 'category');
        status_header(301);
        header("Location: $catlink");
        exit();
    }
    return $query_vars;
}
/**
 * WordPress禁止某些用户账号登录插件源码分析,及WordPress用户黑名单插件“盘他”下载
 * https://www.dujin.org/12560.html
 */
//在资料页面添加选项
function dj_rc_admin_init(){ 
    // 编辑用户资料
    add_action( 'edit_user_profile', 'dj_rc_edit_user_profile' );
    add_action( 'edit_user_profile_update', 'dj_rc_edit_user_profile_update' ); 
}
add_action('admin_init', 'dj_rc_admin_init' ); 
//在个人资料页面添加一个复选框
function dj_rc_edit_user_profile() {
    if ( !current_user_can( 'edit_users' ) ) {
        return;
    } 
    global $user_id; 
    // 用户不能禁止自己
    $current_user = wp_get_current_user();
    $current_user_id = $current_user->ID;
    if ( $current_user_id == $user_id ) {
        return;
    }
    ?>
    <h3>权限设置</h3>
    <table class="form-table">
    <tr>
        <th scope="row">禁止用户登录</th>
        <td><label for="dj_rc_ban"><input name="dj_rc_ban" type="checkbox" id="dj_rc_ban" 
        <?php if (dj_rc_is_user_banned( $user_id )){echo 'checked="checked"';} ?> /> 勾起来,盘Ta,什么玩意儿……</label></td>
    </tr>
    </table>
    <?php
} 
//添加一个函数来将这个选项的值保存到数据库中
function dj_rc_edit_user_profile_update() { 
    if ( !current_user_can( 'edit_users' ) ) {
        return;
    } 
    global $user_id; 
    // 用户不能禁止自己
    $current_user    = wp_get_current_user();
    $current_user_id = $current_user->ID;
    if ( $current_user_id == $user_id ) {
        return;
    } 
    // 锁定
    if( isset( $_POST['dj_rc_ban'] ) && $_POST['dj_rc_ban'] = 'on' ) {
        dj_rc_ban_user( $user_id );
    } else { // 解锁
        dj_rc_unban_user( $user_id );
    } 
} 
//禁止用户
function dj_rc_ban_user( $user_id ) { 
    $old_status = dj_rc_is_user_banned( $user_id ); 
    // 更新状态
    if ( !$old_status ) {
        update_user_option( $user_id, 'dj_rc_banned', true, false );
    }
} 
//解禁用户
function dj_rc_unban_user( $user_id ) { 
    $old_status = dj_rc_is_user_banned( $user_id ); 
    // 更新状态
    if ( $old_status ) {
        update_user_option( $user_id, 'dj_rc_banned', false, false );
    }
} 
//判断用户是否被禁止
function dj_rc_is_user_banned( $user_id ) {
    return get_user_option( 'dj_rc_banned', $user_id, false );
} 
//阻止已禁止的用户登录
function dj_rc_authenticate_user( $user ) { 
    if ( is_wp_error( $user ) ) {
        return $user;
    } 
    // 如果用户被禁止,则返回错误提示,这里是显示给登录用户看的,错误提示可根据自己的喜好修改。
    $banned = get_user_option( 'dj_rc_banned', $user->ID, false );
    if ( $banned ) {
        return new WP_Error( 'dj_rc_banned', __('抱歉,您已被禁止登录!请联系站长解禁。', 'rc') );
    } 
    return $user;
}
//将该函数挂载到 wp_authenticate_user 钩子
add_filter( 'wp_authenticate_user', 'dj_rc_authenticate_user', 1 );

/*
*Wordpress文章关键词自动添加内链链接代码
*/
//连接数量
 $match_num_from = 1; //一个关键字少于多少不替换
 $match_num_to = 1; //一个关键字最多替换次数
 //连接到WordPress的模块
 add_filter('the_content','tag_link',1);
 //按长度排序
 function tag_sort($a, $b){
 if ( $a->name == $b->name ) return 0;
 return ( strlen($a->name) > strlen($b->name) ) ? -1 : 1;
 }
 //改变标签关键字
 function tag_link($content){
 global $match_num_from,$match_num_to;
 $posttags = get_the_tags();
 if ($posttags) {
 usort($posttags, "tag_sort");
 foreach($posttags as $tag) {
 $link = get_tag_link($tag->term_id);
 $keyword = $tag->name;
 //连接代码
 $cleankeyword = stripslashes($keyword);
 $url = "<a href=\"$link\" title=\"".str_replace('%s',addcslashes($cleankeyword, '$'),__('查看所有文章关于 %s'))."\"";
 //$url .= 'target="_blank"';
 $url .= ">".addcslashes($cleankeyword, '$')."</a>";
 $limit = rand($match_num_from,$match_num_to);
 //不连接的代码
 $content = preg_replace( '|(<a[^>]+>)(.*)('.$ex_word.')(.*)(</a[^>]*>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
 $content = preg_replace( '|(<img)(.*?)('.$ex_word.')(.*?)(>)|U'.$case, '$1$2%&&&&&%$4$5', $content);
 $cleankeyword = preg_quote($cleankeyword,'\'');
 $regEx = '\'(?!((<.*?)|(<a.*?)))('. $cleankeyword . ')(?!(([^<>]*?)>)|([^>]*?</a>))\'s' . $case;
 $content = preg_replace($regEx,$url,$content,$limit);
 $content = str_replace( '%&&&&&%', stripslashes($ex_word), $content);
 }
 }
 return $content; 
 }
//支持WebP格式上传
function bzg_filter_mime_types( $array ) {
	$array['webp'] = 'image/webp';
	return $array; 
}
add_filter( 'mime_types', 'bzg_filter_mime_types', 10, 1 );
//支持WebP格式媒体库显示
function bzg_file_is_displayable_image($result, $path) {
	$info = @getimagesize( $path );
	if($info['mime'] == 'image/webp') {
		$result = true;
	}
	return $result;
}
add_filter( 'file_is_displayable_image', 'bzg_file_is_displayable_image', 10, 2 );
//自定义上传
if(get_option('upload_path')=='wp-content/uploads' || get_option('upload_path')==null) {
    update_option('upload_path',WP_CONTENT_DIR.'/uploads');
}
//上传文件重命名
add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );
function custom_upload_filter( $file ){
    $info = pathinfo($file['name']);
    $ext = '.' . $info['extension'];
    $md5 = md5($file['name']);
    $file['name'] = $md5.$ext;
    return $file;
}

共有 0 条评论

You must be logged in to post a comment.

Top