Skip to content

Instantly share code, notes, and snippets.

@wokamoto
Last active December 22, 2015 06:38
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 wokamoto/6432246 to your computer and use it in GitHub Desktop.
Save wokamoto/6432246 to your computer and use it in GitHub Desktop.
[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)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment