【WordPress】前端

调用JavaScript

简述

调用JavaScript

执行政策

/*--------------------------------------------------------
// javascript 呼び出す
--------------------------------------------------------*/

$conditional_scripts = array();
$conditional_scripts['jquery-min'] = get_template_directory_uri() . '/js/jquery.min.js';
$conditional_scripts['html5shiv'] = get_template_directory_uri() . '/js/html5.js';

if(!is_admin()){
    foreach ( $conditional_scripts as $handle => $src ) {
        wp_enqueue_script( $handle, $src, array(), date('Ymd'), false );
    }
}

add_filter( 'script_loader_tag', function( $tag, $handle ) use ( $conditional_scripts ) {
    if ( array_key_exists( $handle, $conditional_scripts ) ) {
        if($handle=='html5shiv'):
            $tag = "<!--[if lt IE 9]>$tag<![endif]-->" ."\n";
        endif;
    }
    return $tag;
}, 10, 2 );

调用样式表

摘要

调用样式表

执行计划

/*--------------------------------------------------------
// style sheet 呼び出す
--------------------------------------------------------*/
$conditional_styles = array();
$conditional_styles['style'] = get_template_directory_uri() . '/css/style.css';
$conditional_styles['print'] = get_template_directory_uri() . '/css/print.css';
$conditional_styles['ie8'] = get_template_directory_uri() . '/css/ie8.css';

if(!is_admin()){
    foreach ( $conditional_styles as $handle => $src ) {
        if($handle=='print'){
            wp_enqueue_style( $handle, $src, array(), date('Ymd'), 'print' );
        }else{
            wp_enqueue_style( $handle, $src, array(), date('Ymd'), false );
        }
    }
}
add_filter( 'style_loader_tag', function( $tag, $handle ) use ( $conditional_styles ) {
    if ( array_key_exists( $handle, $conditional_styles ) ) {
        if($handle=='ie8'):
            $tag = "<!--[if lt IE 9]>$tag<![endif]-->" ."\n";
        endif;
    }
    return $tag;
}, 10, 2 );

删除wp_head中的冗余内容

摘要

删除wp_head中的多余内容

实施政策 (shī cè)

/*-------------------------------------------------------
wp_head の余分なもの削除
--------------------------------------------------------*/
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'wp_shortlink_wp_head');
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');

remove_action( 'wp_head', 'feed_links', 2 ); //サイト全体のフィード
remove_action( 'wp_head', 'feed_links_extra', 3 ); //その他のフィード

function remove_recent_comments_style() {
    global $wp_widget_factory;
    remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style'));
}
add_action('widgets_init', 'remove_recent_comments_style');

add_filter( 'edit_comment_link', '__return_false');///コメントリンク削除
add_filter( 'edit_post_link', '__return_false');///編集リンク削除

从特定的URL重定向

实施措施

//--------------------------------------------------------
// 特定のURLからのリダイレクト
//--------------------------------------------------------
function author_archive_redirect() {

    $url = $_SERVER["REQUEST_URI"];
    preg_match('(/category/([0-9]+).html?$)',$url,$match);
    if($match){
        $id = $match[1];
        wp_redirect( home_url('/category/' . $id . '/') ,301);
        exit;
    }

}
add_action('init', 'author_archive_redirect');

删除标准的JQuery

///WordPress標準のJQuery削除
wp_deregister_script('jquery');

删除表情符号的脚本和样式表。

总结

刪除表情符號的腳本和樣式表

采取措施

///絵文字表示用
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles', 10 );

禁用嵌入功能

总所周知的是

将嵌入功能禁用

实施一项政策

// embed: 埋め込み機能を無効にする
function my_disable_wp_oembed(){
  remove_action('wp_head','rest_output_link_wp_head');
  remove_action('wp_head','wp_oembed_add_discovery_links');
  remove_action('wp_head','wp_oembed_add_host_js');
}
add_action('init', 'my_disable_wp_oembed', 9999);

隐藏DNS预取

概要

隐藏DNS预取

执行政策

function remove_dns_prefetch( $hints, $relation_type ) {
    if ( 'dns-prefetch' === $relation_type ) {
        return array_diff( wp_dependencies_unique_hosts(), $hints );
    }
    return $hints;
}
add_filter( 'wp_resource_hints', 'remove_dns_prefetch', 10, 2 );

从文章的开头提取n个字符。

简而言之

从文章中提取出前n个字符。

实施措施

/*--------------------------------------------------------
記事内のはじめのテキストをn文字取り出す
--------------------------------------------------------*/
function catch_that_text($n) {
    global $post;
    $first_text = '';
    ob_start();
    ob_end_clean();

    $first_text = mb_substr( str_replace(array("\r\n","\r","\n"), '', strip_tags( $post->post_content ) ) , 0, $n,"utf-8");
    if(strlen($first_text)>=$n){
        $first_text = $first_text . '...See More';
    }
    return $first_text;
}

自定义页面标题标签

简而言之

修改标题标签(

广告
将在 10 秒后关闭
bannerAds