Last active
August 29, 2015 13:57
-
-
Save unot13/9919048 to your computer and use it in GitHub Desktop.
WordPress でHTMLのa mailto:を使った文字列を生成する関数と、管理画面の入力からでも使えるようにshort codeを設定したサンプル。
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
<?php | |
/** | |
* <a href="mailto:xxx"> の mailto:xxx 部分を作成する | |
* ファイルの文字コードはUTF-8 | |
*/ | |
function createMailLink($to_address, $subject = null, $body = null) | |
{ | |
$mail_html = ""; | |
$str_encode = 'UTF-8'; | |
// ブラウザ判定 | |
if (stristr($_SERVER['HTTP_USER_AGENT'], "msie")) { | |
// IE | |
$str_encode = 'SJIS'; | |
$subject = mb_convert_encoding($subject, $str_encode); | |
$body = mb_convert_encoding($body, $str_encode); | |
} | |
if (empty($to_address)) { | |
return null; | |
} else { | |
$to_address = htmlentities($to_address, ENT_HTML5, $str_encode); | |
} | |
if (!empty($subject)) { | |
$mail_html .= 'subject=' . urlencode($subject) . "&"; | |
} | |
if (!empty($body)) { | |
$body = urlencode($body); | |
$mail_html .= 'body=' . $body; | |
} | |
$mail_html = "mailto:" . $to_address. '?' . $mail_html; | |
return $mail_html; | |
} | |
/** | |
* WordPress のshort codeの設定 | |
* Handler関数で管理画面から入力されてパラメータを | |
* createMailLink()関数へ受け渡しするための処理 | |
*/ | |
function create_mailto_handler($atts, $content=null) | |
{ | |
extract( shortcode_atts( array( | |
'to_address' => 'info@test.com', | |
'subject' => '', | |
'body' => '', | |
), $atts ) ); | |
return createMailLink($to_address, $subject, $body); | |
} | |
// short code 関数として登録する | |
add_shortcode('create_mailto', 'create_mailto_handler'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment