Skip to content

Instantly share code, notes, and snippets.

@wokamoto

wokamoto/cf7_shortcode.php

Last active Dec 22, 2015
Embed
What would you like to do?
[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
You can’t perform that action at this time.