6

I'm using Contact Form 7 in my wordpress installation. I need to somehow pass a hidden field with the current page url in the contact form hidden field. I tried their custom function and tried the short code, no luck.

wpcf7_add_shortcode('sourceurl', 'wpcf7_sourceurl_shortcode_handler', true);

function wpcf7_sourceurl_shortcode_handler($tag) {
    if (!is_array($tag)) return '';

    $name = $tag['name'];
    if (empty($name)) return '';

    $html = '<input type="hidden" name="' . $name . '" value="http://' . $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"] . '" />';
    return $html;
}

In the form edition I tried [sourceurl thesource]

4 Answers 4

8

See this example on how to create and parse the shortcode in the contact form 7 to use it add [current_url]

add_action( 'wpcf7_init', 'wpcf7_add_form_tag_current_url' );
function wpcf7_add_form_tag_current_url() {
    // Add shortcode for the form [current_url]
    wpcf7_add_form_tag( 'current_url',
        'wpcf7_current_url_form_tag_handler',
        array(
            'name-attr' => true
        )
    );
}

// Parse the shortcode in the frontend
function wpcf7_current_url_form_tag_handler( $tag ) {
    global $wp;
    $url = home_url( $wp->request );
    return '<input type="hidden" name="'.$tag['name'].'" value="'.$url.'" />';
}

Also if its all about to get the URL in the email you dont need all of this. there is a special tag for the email [_url] CF7 Special mail tags

1
  • It forks for me if replace $tag['name'] with $tag['type']: return '<input type="hidden" name="'.$tag['type'].'" value="'.$url.'" />';
    – userlond
    Commented Mar 25, 2022 at 7:53
8

according to Contact Form 7 documentation you need this:

[_url] — This tag is replaced by the URL of the page in which the contact form is placed.

reference: https://contactform7.com/special-mail-tags/

1
  • 3
    +1 for the common sense to read the docs :) Commented Jul 8, 2021 at 12:57
4

You can use cf7 hidden field shortcodes + get parameters: https://contactform7.com/hidden-field/ https://contactform7.com/getting-default-values-from-the-context/

In the mail form I use such shortcodes to get these slugs

[hidden utm_source default:get]
[hidden utm_medium default:get]
[hidden utm_campaign default:get]
[hidden utm_content default:get]
[hidden gclid default:get]

It also works with their flamingo plugin. And for mail template [_url] works very well.

2

the form submit already send the container post then you just need to retrive it in and put it in the e-mail.
For that, try this code :

add_filter("wpcf7_posted_data", function ($wpcf7_posted_data) {

    $post = get_post($wpcf7_posted_data["_wpcf7_container_post"]);

    $wpcf7_posted_data["containerURL"] = get_permalink($post);


    return $wpcf7_posted_data;

});

And in the edition of the sent e-mail, you retrieve the URL with [containerURL]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.