Skip to content

Instantly share code, notes, and snippets.

@bainternet
Created August 9, 2011 06:25
Show Gist options
  • Save bainternet/1133507 to your computer and use it in GitHub Desktop.
Save bainternet/1133507 to your computer and use it in GitHub Desktop.
add current page url tag to content form 7
<?php
/*
* add this to your theme's functions.php file
*/
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;
}
/*
* open your contact form and in the form part add:
"[sourceurl thesource]"
*
* and in the email part add:
"Source URL: <a href="[thesource]">[thesource]</a><br />"
*/
and you are done
@danstramer
Copy link

Just a heads up, because of recent changes in wpcf7 to version 4.8, please update
is_array > is_object
and
wpcf7_add_shortcode > wpcf7_add_form_tag
More here:
https://wordpress.org/support/topic/wpcf7_add_form_tag-not-working/

Thanks
Dan

@danstramer
Copy link

Also update on row #9:
$name = $tag->name;
instead of
$name = $tag['name'];

@mathanhcong
Copy link

Thanks a lot

@jemoreto
Copy link

With @danstramer reviews this code works. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment