Pregunta

I am using Contact form 7, with the Dynamic Text plugin. This works really nicely...

What I'm doing is setting it up so that if the user clicks on a link on a product, it redirects them to an enquiry form, with the product name already input into a field...

This works fine, using the following CF7 code:

<p>[dynamictext enquiry-product "CF7_GET key='product-name'" ]</p>

and then navigating to the enquiry form by:

http://www.myurl.com/contact-form/?product-name=Product%20Name

However, I'm trying to work out how to extend the shortcode so that I can include both a placeholder (sometimes people will visit the form NOT from the product page) AND also add some text before...

So the field would say:

"Enquiry about Product Name" (with 'Product Name' pulling in from the URL).

and if they just goto the form from http://www.myurl.com/contact-form the field would just have a placeholder of "Enquiry subject".

I've tried:

<p>[dynamictext enquiry-product "CF7_GET key='product-name'" placeholder "Enquiry Subject"]</p>

I've also tried:

<p>[dynamictext enquiry-product "Enquiry About CF7_GET key='product-name'"]</p>

and

<p>[dynamictext enquiry-product "Enquiry About "CF7_GET key='product-name'""]</p>

No luck...

Anyone know if this is possible even? If not, any alternative options to pass the product name into the field, whilst also being able to add to the text and have a placeholder.

Thanks!

¿Fue útil?

Solución

the Dynamic Text plugin cannot make what you need, but I use this code to write a new tag that you can use like that :

[dynamictext_placeholder enquiry-product placeholder "placeholder text" "CF7_GET key='product-name'" "before '%s' after"]

for that, create a new plugin with that :

add_action( 'wpcf7_init', function () {


    wpcf7_add_form_tag(
          array( 'dynamictext_placeholder')
        , 'wpcf7dtx_dynamictext_placeholder_shortcode_handler'
        , true
    );


});


function wpcf7dtx_dynamictext_placeholder_shortcode_handler( $tag ) {


    $tag = new \WPCF7_FormTag( $tag );

    if ( empty( $tag->name ) )
        return '';

    $validation_error = wpcf7_get_validation_error( $tag->name );


    $class = wpcf7_form_controls_class( $tag->type, 'wpcf7dtx-dynamictext' );


    if ( $validation_error )
        $class .= ' wpcf7-not-valid';

    $atts = array();

    $atts['size'] = $tag->get_size_option( '40' );
    $atts['maxlength'] = $tag->get_maxlength_option();
    $atts['minlength'] = $tag->get_minlength_option();

    if ( $atts['maxlength'] && $atts['minlength'] && $atts['maxlength'] < $atts['minlength'] ) {
        unset( $atts['maxlength'], $atts['minlength'] );
    }

    $atts['class'] = $tag->get_class_option( $class );
    $atts['id'] = $tag->get_id_option();
    $atts['tabindex'] = $tag->get_option( 'tabindex', 'int', true );


    if ( $tag->has_option( 'readonly' ) )
        $atts['readonly'] = 'readonly';

    if ( $tag->is_required() )
        $atts['aria-required'] = 'true';

    $atts['aria-invalid'] = $validation_error ? 'true' : 'false';


    if ($tag->has_option( 'placeholder' )) {
        $value = $tag->values[1];
    } else {
        $value = (string) reset( $tag->values );
    }

    $value = $tag->get_default_option( $value );

    $value = wpcf7_get_hangover( $tag->name, $value );

    $scval = do_shortcode('['.$value.']');
    if( $scval != '['.$value.']' ){
        $value = esc_attr( $scval );
    }

    $atts['value'] = $value;


    if ("" === $value && $tag->has_option( 'placeholder' )) {

        $atts['placeholder'] = $tag->values[0];

    } elseif (isset($tag->values[2])) {

        $atts['value'] = sprintf($tag->values[2], $atts['value']);

    }


    $atts['type'] = 'text';
    $atts['name'] = $tag->name;

    $atts = wpcf7_format_atts( $atts );


    $html = sprintf(
        '<span class="wpcf7-form-control-wrap %1$s"><input %2$s />%3$s</span>',
        sanitize_html_class( $tag->name ), $atts, $validation_error );

    return $html;

}
Licenciado bajo: CC-BY-SA con atribución
scroll top