Skip to content

Instantly share code, notes, and snippets.

@Nully
Created June 11, 2011 02:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nully/1020169 to your computer and use it in GitHub Desktop.
Save Nully/1020169 to your computer and use it in GitHub Desktop.
WordPressの便利なcomment_formオーバーラップ関数
// comments.php の comment_form 部分を以下に置き換える
<?php
convenience_comment_form(array(
"required" => '<em class="required">*</em>',
// %1$s は required で指定した内容に置き換わる
"required_notes" => '%1$sは必須項目です。',
"fields" => array(
"author" => '<p class="comment-form-author"><label for="hogehoge">お名前</label>%1$s'.
'<input id="hogehoge" type="text" name="author" value="%2$s"%3$s /></p>',
"email" => '<p class="comment-form-email"><label for="email">メールアドレス</label>%1$s'.
'<input type="text" id="email" name="email" value="%2$s"%3$s /></p>',
"url" => '<p class="comment-form-url"><label for="url">Webサイト</label>%1$s'.
'<input type="text" id="url" name="url" value="%2$s"%3$s /></p>',
),
// %1$s の部分は required_notes で指定した内容に置き換わる
'comment_notes_before' => '<p class="comment-notes">メールアドレスは表示されません。%1$s</p>'
));
?>
<?php
if(!function_exists("convenience_comment_form")):
/**
* ちょっとだけ使いやすいコメントフォームを提供する関数
* 引数はもとのcomment_form関数とほぼ同じ。
* --
* 追加した引数
* required
* 必須項目のマークを指定する。 デフォルトで、「<span class="required">*</span>」となる。
* required_notes
* 「*マークは必須項目です」のような表示をするメッセージノート。
* 初期値は「%1$sは必須項目です。」となる。
* %1$sには、上記 required の内容に置き換えられる。
*/
function convenience_comment_form($args = array(), $post_id = null) {
wp_parse_args($args, array(
"required" => '<span class="required">*</span>',
"require_note" => '%1$sは必須項目です。'
));
// コメントユーザーのデフォルト値を取得
$commenter = wp_get_current_commenter();
$req = get_option( 'require_name_email' );
$aria_req = ( $req ? " aria-required='true'" : '' );
$required_notes = null;
if($req) {
$required_notes = sprintf(
$args["required_notes"], $args["required"]
);
}
foreach($args["fields"] as $field_name => $field) {
$args["fields"][$field_name] = sprintf(
$field,
($req ? $args["required"] : ''), // 必須項目の設定
esc_attr($commenter["comment_author". str_replace("author", "", $field_name)]), // 初期値
$aria_req
);
}
// comment_notes_before の設定
if($args["comment_notes_before"]) {
$args["comment_notes_before"] = sprintf(
$args["comment_notes_before"], $required_notes
);
}
comment_form($args, $post_id);
}
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment