[WordPress] Contact Form 7 に独自ショートコードを追加


<?php
add_action('init', 'my_wpcf7_shortcode');
function my_wpcf7_shortcode(){
wpcf7_add_shortcode('my_shortcode', 'my_wpcf7_shortcode_handler', true);
}
function my_wpcf7_shortcode_handler( $tag ) {
$tag = new WPCF7_Shortcode( $tag );
if ( empty( $tag->name ) )
return '';
$args = array(
'size' => $tag->get_size_option( '40' ),
'maxlength' => $tag->get_maxlength_option(),
'class' => $tag->get_class_option( wpcf7_form_controls_class( $tag->type, $tag->name ) ),
'id' => $tag->get_option( 'id', 'id', true ),
'tabindex' => $tag->get_option( 'tabindex', 'int', true ),
'pre' => $tag->get_option( 'pre', '.*', true ),
'post' => $tag->get_option( 'post', '.*', true ),
);
$defaults = array(
'size' => '40',
'maxlength' => '40',
'class' => $tag->name,
'id' => $tag->name,
'tabindex' => 0,
'pre' => '<span class="wpcf7-form-control-wrap">',
'post' => '</span>',
);
wp_parse_args( $args, $defaults );
$value = '';
if ( wpcf7_is_posted() && isset($_POST[$tag->name]) )
$value = stripslashes_deep( $_POST[$tag->name] );
return sprintf(
$args['pre'] . '<input type="%2$s" name="%1$s" value="%3$s" />' . $args['post'],
$tag->name,
$tag->type,
esc_attr($value)
);
}

バリデーションとかは入れてません


Posted

in

,

by

Comments

コメントを残す

以下に詳細を記入するか、アイコンをクリックしてログインしてください。

WordPress.com ロゴ

WordPress.com アカウントを使ってコメントしています。 ログアウト /  変更 )

Facebook の写真

Facebook アカウントを使ってコメントしています。 ログアウト /  変更 )

%s と連携中

%d人のブロガーが「いいね」をつけました。