wordpress 常见代码文章外链加上nofollow移除多余加载JS和CSS代码等

//文章外链加上nofollow
add_filter('the_content','link_jump',999);
function link_jump($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) && !preg_match('/(ed2k|thunder|Flashget|flashget|qqdl):\/\//i',$val)){
            $content=str_replace("href=\"$val\"", "href=\"$val\" rel=\"external nofollow\"  target=\"blank\"",$content);
            }
        }
    }
    return $content;
}

/* 移除 Wp 加载的JS和CSS链接中的版本号*/
function wpdaxue_remove_cssjs_ver( $src ) {
    if( strpos( $src, 'ver='. get_bloginfo( 'version' ) ) )
        $src = remove_query_arg( 'ver', $src );
    return $src;
}
add_filter( 'style_loader_src', 'wpdaxue_remove_cssjs_ver', 999 );
add_filter( 'script_loader_src', 'wpdaxue_remove_cssjs_ver', 999 );

/* 移除 wp_headd多余加载*/
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0 );//rel=shortlink  rel=canonical
remove_action('wp_head','adjacent_posts_rel_link_wp_head', 10, 0 );// 上、下篇. 
remove_action('wp_head', 'wp_resource_hints', 2 );//禁止WordPress头部加载 s.w.org

/* 移除 oembed引用功能 相关JS 及功能*/
function stop_loading_wp_embed_and_jquery() {
    if (!is_admin()) {
        wp_deregister_script('wp-embed');
    }
}
remove_action( 'rest_api_init', 'wp_oembed_register_route' );
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );//rel="alternate
remove_action( 'wp_head', 'wp_oembed_add_host_js' );
remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );
add_action('init', 'stop_loading_wp_embed_and_jquery');
add_filter( 'embed_oembed_discover', '__return_false' );

// 移除头部 wp-json 标签和 HTTP header 中的 link 
remove_action('wp_head', 'rest_output_link_wp_head', 10 );
remove_action('template_redirect', 'rest_output_link_header', 11 );

/* 移除菜单的多余CSS选择器*/
add_filter('nav_menu_css_class', 'my_css_attributes_filter', 100, 1);
add_filter('page_css_class', 'my_css_attributes_filter', 100, 1);
function my_css_attributes_filter($var) {
    return is_array($var) ? array_intersect($var, array('current-menu-item','menu-item-has-children','xuan','xuan2','page_item')) : '';
}//留5个样式-其中xuan样式是菜单图标是旋转,这个要在菜单添加CSS类

/* WordPress 5.0+移除 block-library CSS*/
add_action( 'wp_enqueue_scripts', 'fanly_remove_block_library_css', 100 );
function fanly_remove_block_library_css() {
    wp_dequeue_style( 'wp-block-library' );
}

//显示页面查询次数、加载时间和内存占用 正式上线可删除
function performance( $visible = false ) {
    $stat = sprintf(  '%d 次查询 耗时 %.3f seconds, 使用 %.2fMB 内存',
        get_num_queries(),
        timer_stop( 0, 3 ),
        memory_get_peak_usage() / 1024 / 1024
    );
    echo $visible ? $stat : "" ;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值