Skip to content

Instantly share code, notes, and snippets.

@kimcoleman
Last active June 9, 2022 08:28
Show Gist options
  • Save kimcoleman/1b2b759dfdd99fedd0b7377f8874c958 to your computer and use it in GitHub Desktop.
Save kimcoleman/1b2b759dfdd99fedd0b7377f8874c958 to your computer and use it in GitHub Desktop.
Add Contact Form 7 to the Profile page when using the Member Directory and Profiles Add On for Paid Memberships Pro.
<?php
/**
* Add Contact Form 7 to the Profile page when using the Member Directory and Profiles Add On for Paid Memberships Pro.
* Update line 36 with the correct CF7 shortcode for your desired form to display.
* Add a hidden field to your form: "[hidden send-to-email default:shortcode_attr]".
* Set the "To" field of the Contact Form to "[send-to-email]".
*
*/
// Allow custom shortcode attribute for "send-to-email".
function custom_shortcode_atts_wpcf7_filter( $out, $pairs, $atts ) {
$my_attr = 'send-to-email';
if ( isset( $atts[$my_attr] ) ) {
$out[$my_attr] = $atts[$my_attr];
}
return $out;
}
add_filter( 'shortcode_atts_wpcf7', 'custom_shortcode_atts_wpcf7_filter', 10, 3 );
// Add the contact form to the profile page using Contact Form 7.
function append_profile_page_with_cf7( $content ) {
global $pmpro_pages;
//Get the profile user
if ( isset( $_REQUEST['pu'] ) ) {
if ( is_numeric($_REQUEST['pu'] ) ) {
$pu = get_user_by( 'id', $_REQUEST['pu'] );
} elseif( ! empty( $_REQUEST['pu'] ) ) {
$pu = get_user_by( 'slug', $_REQUEST['pu'] );
} else {
$pu = false;
}
}
if ( ! empty( $pu ) && shortcode_exists( 'contact-form-7' ) && is_page( $pmpro_pages['profile'] ) ) {
$content .= do_shortcode( '[contact-form-7 id="271" title="Contact form 1" send-to-email="' . $pu->user_email . '"]' );
}
return $content;
}
add_filter( 'the_content', 'append_profile_page_with_cf7' );
@kimwhite
Copy link

Here is the hidden field you need to add to your form: [hidden send-to-email default:shortcode_attr]

@kimwhite
Copy link

Add to form "To" field [send-to-email] (you will get an error message)

@laurenhagan0306
Copy link

This recipe is included in the blog post on "How to show a dynamic “Contact Member” form on your directory profile pages." at Paid Memberships Pro here: https://www.paidmembershipspro.com/how-to-show-a-dynamic-contact-member-form-on-your-directory-profile-pages/

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