• It’s still not clear to me whether this plug-in is the one for the goal I’m trying to achieve. I’ll try to keep it simple, as I’m building a plug-in with multiple moving parts.

    I’m using Contact Form 7 to submit email name and address and add it to the meta values of particular posts (so users can subscribe on a particular post’s mailing list).

    So, ideally, there will be a list of posts, each with its own pop-up contact form: The user fills in name and email, and values are added to post meta.

    In order to find the right post for add_post_meta, we need the particular post ID. The following code works for adding the values to a particular post identified by ID (post “54768” in the sample below:

    function add_subscriber_email($contact_form) { 
    
    	/*get submitted data */
    	$title = $contact_form->title;
    	$submission = WPCF7_Submission::get_instance();
    
    	if ($submission) {
    	$posted_data = $submission->get_posted_data();
    		}
    /*Use only for the Contact Form
    entitled "Post Subscriber" */
    if ('PostSubscriber' == $title ) {
    			$new_subscriber = $posted_data['your-name'];
    			$new_email = $posted_data['your-email'];
    			add_post_meta(54768, 'subscriber_name', $new_subscriber);
    			add_post_meta(54768, 'subscriber_email', $new_email);
    	}
    }
    
    add_action('wpcf7_before_send_mail','add_subscriber_email',10,2);

    The question is how to get CF7 when it is invoked to grab the Post ID from the associated post in a list of posts that have already been grabbed via wp_get_recent_posts() or other post listing function for use in a widget, post, or page.

    That last part is pretty straightforward, though I can produce the sample code here (accessing the post data via setup_postdata() ) if someone would find it helpful.

    The key at this point is getting CF7 to send the particular Post ID (and eventually other data or values) along with subscriber_name and subscriber_email, which are already included in the cf7 array as “posted data” .

    I’ve had difficulty even getting the code above to work just sending the original Post ID (i.e., the page where the Contact Form is located): global $post and wpcf7_special_mail_tag_for_post_data either don’t work or throw errors (respectively).

    In the final implementation, it will have to be the Post ID for the listed post, in one contact form among others in a series: Click on the icon or link, pop-up the form, fill in name and email, goes to post_meta.

    I want to use CF7 because I’m used to it, and I want to use akismet spam filtering, but at this point I’m wondering if it would be necessary or simply easier to write a form from scratch, and add spam filtering and other functions later. Maybe CF7 DTE is the key… or maybe not, but, even if it isn’t, I think others would also benefit from examples of grabbing CF7 variables for use in PHP functions (if appropriate).

    https://www.ads-software.com/plugins/contact-form-7-dynamic-text-extension/

  • The topic ‘Get ID or other variable for us in function’ is closed to new replies.