Skip to content

Instantly share code, notes, and snippets.

@unot13
Last active August 29, 2015 13:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save unot13/9919048 to your computer and use it in GitHub Desktop.
Save unot13/9919048 to your computer and use it in GitHub Desktop.
WordPress でHTMLのa mailto:を使った文字列を生成する関数と、管理画面の入力からでも使えるようにshort codeを設定したサンプル。
<?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