Created
December 13, 2015 17:46
-
-
Save kurudrive/5b2e13d5d8bae1096a73 to your computer and use it in GitHub Desktop.
オレオレ多言語サイト対応プログラム
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- [ header.phpなどに記入 ] --> | |
<ul class="lang_list"> | |
<li id="lg_bt_en"><a href="#">ENGLISH</a></li> | |
<li id="lg_bt_ja"><a href="#">JAPAN</a></li> | |
</ul> | |
<?php | |
/* | |
functons.phpなどに記入 | |
*/ | |
/*-------------------------------------------*/ | |
/* ookieを利用するためにjQuery.cookieを読み込み | |
/*-------------------------------------------*/ | |
function load_cookie() { | |
wp_enqueue_script( 'jquery_cookie', get_template_directory_uri(). '/js/jquery.cookie.js', array( 'jquery' ), 'kurudrive_suteki', true ); | |
} | |
add_action( 'wp_enqueue_scripts', 'load_cookie' ); | |
/*-------------------------------------------*/ | |
/* 言語ボタンを押された時に言語のクッキーの入れ替え&再読み込み | |
/*-------------------------------------------*/ | |
function lang_switch_action(){ | |
// 管理画面じゃない場合 | |
if ( !is_admin()){ ?> | |
<script type="text/javascript"> | |
//<![CDATA[ | |
jQuery(document).ready(function($){ | |
// 言語スイッチャーに対して、現在の表示言語にstrongタグを追加 | |
var lang = jQuery.cookie("lang"); | |
if ( lang == 'ja' ){ | |
jQuery('#lg_bt_ja a').wrap('<strong></strong>'); | |
} else { | |
jQuery('#lg_bt_en a').wrap('<strong></strong>'); | |
} | |
jQuery('#lg_bt_ja').click(function(){ | |
var lang = jQuery.cookie("lang"); | |
if ( lang != 'ja' ){ | |
jQuery.cookie("lang", "ja", { expires: 7,path: "/" }); | |
setTimeout("location.reload()",5); | |
lang = jQuery.cookie("lang"); | |
} | |
}); | |
jQuery('#lg_bt_en').click(function(){ | |
var lang = jQuery.cookie("lang"); | |
if ( lang != 'en-US' ){ | |
jQuery.cookie("lang", "en-US", { expires: 7,path: "/" }); | |
setTimeout("location.reload()",5); | |
lang = jQuery.cookie("lang"); | |
} | |
}); | |
}); | |
//]]> | |
</script> | |
<?php } // if ( !is_admin()){ | |
} | |
add_action( 'wp_footer', array( $this, 'lang_switch_action' )); | |
/*-------------------------------------------*/ | |
/* 言語の切り替えを実行 | |
/*-------------------------------------------*/ | |
function locale_custom($locale){ | |
// cookieに保存されている言語情報を取得して書き換え | |
if(isset($_COOKIE['lang'])) { | |
$locale = $_COOKIE['lang']; | |
} | |
return $locale; | |
} | |
add_filter( 'locale', 'locale_custom' ); | |
/*-------------------------------------------*/ | |
/* タイトルを書き換える | |
/*-------------------------------------------*/ | |
function replace_title($title,$id){ | |
$locale = get_locale(); | |
// 日本語だったら | |
if( $locale == 'ja') { | |
// カスタムフィールドに入力された日本語のタイトルを取得 | |
$title_ja = get_post_meta($id,'lang_title_ja',true); | |
// 日本語タイトルが入力されていたら | |
if ( $title_ja ) { | |
// タイトルを日本語に差し替え | |
$title = $title_ja; | |
} | |
} | |
// 値を返す | |
return $title; | |
} | |
add_filter( 'the_title', 'replace_title',10,2 ); | |
/*-------------------------------------------*/ | |
/* 言語によって本文の差し替えを実行 | |
/*-------------------------------------------*/ | |
function replace_content($content){ | |
// 言語を取得 | |
global $post; | |
$locale = get_locale(); | |
// 日本語だったら | |
if( $locale == 'ja') { | |
$content_ja = get_post_meta($post->ID,'lang_content_ja',true); | |
if ( $content_ja ) { | |
$content = $content_ja; | |
} | |
} | |
return $content; | |
} | |
add_filter( 'the_content', 'replace_content',10,2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment