wordpress模板常用的一些技巧

wordpress模板常用的一些技巧

屏蔽文章 Embed 功能:Embed让你可以在任意 WordPress 站点用嵌入的方式插入 WordPress 文章,不需要可在当前主题的 functions.php 文件中添加如下代码来屏蔽该功能:

remove_action('rest_api_init', 'wp_oembed_register_route');
remove_filter('rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4);
remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10 );
remove_filter('oembed_response_data',   'get_oembed_response_data_rich',  10, 4);
remove_action('wp_head', 'wp_oembed_add_discovery_links');
remove_action('wp_head', 'wp_oembed_add_host_js');

删除W3 Total Cache页面尾部插入,不知道这插件怎么想的非要在页面加入这些

add_filter( 'w3tc_can_print_comment', '__return_false', 10, 1 );

关闭 Google 字体

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' );

加速 Gravatar 头像

function my_avatar($avatar) {
  $tmp = strpos($avatar, 'http');
  $g = substr($avatar, $tmp, strpos($avatar, "'", $tmp) - $tmp);
  $tmp = strpos($g, 'avatar/') + 7;
  $f = substr($g, $tmp, strpos($g, "?", $tmp) - $tmp);
  $w = get_bloginfo('wpurl');
  $e = ABSPATH .'avatar/'. $f .'.jpg';
  $t = 1209600; //设定时间为14天,单位为秒
  if ( !is_file($e) || (time() - filemtime($e)) > $t ) { //头像不存在或时间超过14天,重新获取
    copy(htmlspecialchars_decode($g), $e);
  } else  $avatar = strtr($avatar, array($g => $w.'/avatar/'.$f.'.jpg'));
  if (filesize($e) < 500) copy($w.'/avatar/default.jpg', $e);
  return $avatar;
}
add_filter('get_avatar', 'my_avatar');

修改WordPress自动保存草稿的时间,编辑 wp-config.php文件插入,修改下文的秒为你要的数字

define( 'AUTOSAVE_INTERVAL', 秒 ); 

搜索关键字为空跳回主页


add_filter( 'request', 'uctheme_redirect_blank_search' );
function uctheme_redirect_blank_search( $query_variables ) {
 if (isset($_GET['s']) && !is_admin()) {
 if (empty($_GET['s']) || ctype_space($_GET['s'])) {
 wp_redirect( home_url() );
 exit;
 }
 }
 return $query_variables;
}

压缩页面代码一:

//压缩HTML代码
function wpjam_minify_html($html) {
    return preg_replace(
        $search = array(
            '/\>[^\S ]+/s',  // 删除标签后面空格
            '/[^\S ]+\',
            '<',
            '\\1'
        ), 
        $html
    );
}

if(!is_admin()){
	add_action("wp_loaded", 'wp_loaded_minify_html');
	function wp_loaded_minify_html(){
		ob_start('wpjam_minify_html');
	}
}

压缩页面代码二:

//压缩html代码 
function wp_compress_html(){
    function wp_compress_html_main ($buffer){
        $initial=strlen($buffer);
        $buffer=explode("", $buffer);
        $count=count ($buffer);
        for ($i = 0; $i <= $count; $i++){
            if (stristr($buffer[$i], '')) {
                $buffer[$i]=(str_replace("", " ", $buffer[$i]));
            } else {
                $buffer[$i]=(str_replace("\t", " ", $buffer[$i]));
                $buffer[$i]=(str_replace("\n\n", "\n", $buffer[$i]));
                $buffer[$i]=(str_replace("\n", "", $buffer[$i]));
                $buffer[$i]=(str_replace("\r", "", $buffer[$i]));
                while (stristr($buffer[$i], '  ')) {
                    $buffer[$i]=(str_replace("  ", " ", $buffer[$i]));
                }
            }
            $buffer_out.=$buffer[$i];
        }
        $final=strlen($buffer_out);   
        $savings=($initial-$final)/$initial*100;   
        $savings=round($savings, 2);   
        $buffer_out.="\n";   
    return $buffer_out;
}
if ( !is_admin() ) { 
        ob_start("wp_compress_html_main");
    }
}
add_action('init', 'wp_compress_html');

禁止缩略图

 // 禁用自动生成的图片尺寸
function shapeSpace_disable_image_sizes($sizes) {
    
    unset($sizes['thumbnail']);    // disable thumbnail size
    unset($sizes['medium']);       // disable medium size
    unset($sizes['large']);        // disable large size
    unset($sizes['medium_large']); // disable medium-large size
    unset($sizes['1536x1536']);    // disable 2x medium-large size
    unset($sizes['2048x2048']);    // disable 2x large size
    
    return $sizes;
    
}
add_action('intermediate_image_sizes_advanced', 'shapeSpace_disable_image_sizes');
 
// 禁用缩放尺寸
add_filter('big_image_size_threshold', '__return_false');
 
// 禁用其他图片尺寸
function shapeSpace_disable_other_image_sizes() {
    
    remove_image_size('post-thumbnail'); // disable images added via set_post_thumbnail_size() 
    remove_image_size('another-size');   // disable any other added image sizes
    
}
add_action('init', 'shapeSpace_disable_other_image_sizes');
分享到 :
TP-link WR703N
上一篇 2014-12-22
netgear/网件 WNR2000
2014-12-22 下一篇
相关推荐