WordPress.org

Ready to get started?Download WordPress

Forums

[Plugin: Contact Form 7] Include originating page, or custom field from originating page? (30 posts)

  1. Casemon
    Member
    Posted 4 years ago #

    Have successfully embedded Custom Form 7 on various pages, and it's great!

    Is there a means to including the page from which the form was submitted from in the email that is sent?

    Better, can I grab a custom field from the originating page and send that?

    Can think of how to do it in PHP, but cannot see how to get Custom Form 7 to process the PHP prior to sending.

    In any case, great plugin!

    http://wordpress.org/extend/plugins/contact-form-7/

  2. Takayuki Miyoshi
    Member
    Posted 4 years ago #

  3. bhomatude
    Member
    Posted 4 years ago #

    Hi -- I've been trying all of the special mail tags and none of them seem to be working. Note that I am using contact form 8 variation. I just get back the mail tag ie: [_url] or [_post_title] or [_post_id]. I've tried putting the tags into the message body on the email notification - everywhere. And yes, the contact form is in a post.

  4. Takayuki Miyoshi
    Member
    Posted 4 years ago #

    bhomatude, I don't recommend the "contact form 8" as it's just a bad copy of Contact Form 7. Of course you can't use the special mail tags with the "contact form 8" because they are new features only Contact Form 7 supports. I suggest you to switch it to Contact Form 7 as soon as possible as Contact Form 7 has included many security fixes as well.

  5. bhomatude
    Member
    Posted 4 years ago #

    Update: problem is in the contact form 8. I uninstalled and went back to 7. Special mail tags work perfect in 7. Thanks Tak!

  6. Casemon
    Member
    Posted 4 years ago #

    Thank you Takayukister for the link & info! Will integrate this straight away!

  7. Casemon
    Member
    Posted 4 years ago #

    Actually, is there a way to get a page's custom field info and put it as the TO: field?

    Have need for this.

  8. Takayuki Miyoshi
    Member
    Posted 4 years ago #

    No. If you want to get custom field value you need to develop an add-on module yourself. Search 'wpcf7_special_mail_tags' in includes/classes.php.

  9. Casemon
    Member
    Posted 4 years ago #

    Ahh ok, will look into this.

    thanks for the info!

  10. Casemon
    Member
    Posted 4 years ago #

    Is there any more documentation on creating an add-on module?

  11. Juergen Schulze
    Member
    Posted 4 years ago #

    Maybe this will help.
    http://www.seodenver.com/contact-form-7-hidden-fields/
    It's a module to use hidden fields.
    Maybe you could fill those hidden fields with the values of custom field.
    I am also very desperate for custom fields

  12. Casemon
    Member
    Posted 4 years ago #

    @1manfactory
    Thanks for this, it's a good start.

    Am stuck on 1 thing:

    1) getting the page ID into my custom shortcode function; you cannot pass [_post_id] within a shortcode as far as I can tell, and $post->ID doesn't work in the shortcode function (not sure why).

    Am happy to extend CF7, though really wish there was some documentation on all this, can't seem to find API info on CF7.

  13. Casemon
    Member
    Posted 4 years ago #

    Ok, have managed to get this working partially. Ended up making a franken-add-on based on the various other modules.

    The good:
    Am seeing the value of the custom field if I include [customfield some-custom-field-name] in the form. Great.

    The bad / not working:
    If I add the customfield tag name to the Additional headers: area (the desired custom field happens to be an email) as in CC: [some-custom-field-name] <some-custom-field-email]>

    then the CC field is blank! The email has a blank entry for CC, as shown:
    Blank CC entry in resulting mail...

    Anyone know why this is?

    Here's the module code if it helps:

    <?php
    /**
    ** contact form 7 module, which enables you to get the result of a custom field from the page that the form is on
    ** [customfield name] -> returns the value of a custom field on that post/page for [name]
    **/
    /* Shortcode handler */
    
    /* Shortcode handler */
    
    wpcf7_add_shortcode( 'customfield', 'wpcf7_customfield_shortcode_handler', true );
    function wpcf7_customfield_shortcode_handler($field) {
    	global $wpcf7_contact_form;
    	global $wp_query;
    	$thePostID = $wp_query->post->ID;
    	$somefield = get_post_meta($thePostID, $field[name], true);
    	if (empty($somefield)) return '';
    	return $somefield;
    }
    
    add_filter( 'wpcf7_validate_customfield', 'wpcf7_customfield_validation_filter', 10, 2 );
    function wpcf7_customfield_validation_filter( $result, $tag ) {
    	global $wpcf7_contact_form;
    
    	// $type = $tag['type'];
    	$name = $tag['name'];
    
    	$_POST[$name] = trim( strtr( (string) $_POST[$name], "\n", " " ) );
    
    	return $result;
    }
    
    /* Tag generator */
    
    add_action( 'admin_init', 'wpcf7_add_tag_generator_customfield', 15 );
    function wpcf7_add_tag_generator_customfield() {
    	wpcf7_add_tag_generator( 'customfield', __( 'Custom Field', 'wpcf7' ),
    		'wpcf7-tg-pane-customfield', 'wpcf7_tg_pane_customfield' );
    
    }
    
    function wpcf7_tg_pane_customfield( $type = 'customfield' ) {
    	$type = 'customfield';
    
    ?>
    <div id="wpcf7-tg-pane-<?php echo $type; ?>" class="hidden">
    <form action="">
    <table>
    <tr><td><input type="checkbox" name="required" />&nbsp;<?php echo esc_html( __( 'Required field?', 'wpcf7' ) ); ?></td></tr>
    <tr><td><?php echo esc_html( __( 'Name', 'wpcf7' ) ); ?><br /><input type="text" name="name" class="tg-name oneline" /></td><td></td></tr>
    </table>
    
    <table>
    <tr>
    <td><code>id</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
    <input type="text" name="id" class="idvalue oneline option" /></td>
    
    <td><code>class</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
    <input type="text" name="class" class="classvalue oneline option" /></td>
    </tr>
    
    <tr>
    <td><code>size</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
    <input type="text" name="size" class="numeric oneline option" /></td>
    
    <td><code>maxlength</code> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
    <input type="text" name="maxlength" class="numeric oneline option" /></td>
    </tr>
    
    <tr>
    <td><?php echo esc_html( __( 'Akismet', 'wpcf7' ) ); ?> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br />
    <?php if ( 'customfield' == $type ) : ?>
    <input type="checkbox" name="akismet:author" class="exclusive option" />&nbsp;<?php echo esc_html( __( "This field requires author's name", 'wpcf7' ) ); ?><br />
    <input type="checkbox" name="akismet:author_url" class="exclusive option" />&nbsp;<?php echo esc_html( __( "This field requires author's URL", 'wpcf7' ) ); ?>
    <?php endif; ?>
    </td>
    
    <td><?php echo esc_html( __( 'Default value', 'wpcf7' ) ); ?> (<?php echo esc_html( __( 'optional', 'wpcf7' ) ); ?>)<br /><input type="text" name="values" class="oneline" /></td>
    </tr>
    </table>
    
    <div class="tg-tag"><?php echo esc_html( __( "Copy this code and paste it into the form left.", 'wpcf7' ) ); ?><br /><input type="text" name="<?php echo $type; ?>" class="tag" readonly="readonly" onfocus="this.select()" /></div>
    
    <div class="tg-mail-tag"><?php echo esc_html( __( "And, put this code into the Mail fields below.", 'wpcf7' ) ); ?><br /><span class="arrow">⬇</span>&nbsp;<input type="text" class="mail-tag" readonly="readonly" onfocus="this.select()" /></div>
    </form>
    </div>
    <?php
    }
    
    ?>
  14. Takayuki Miyoshi
    Member
    Posted 4 years ago #

    wpcf7_add_shortcode( 'customfield', 'wpcf7_customfield_shortcode_handler', true );
    function wpcf7_customfield_shortcode_handler($field) {
    	global $wpcf7_contact_form;
    	global $wp_query;
    	$thePostID = $wp_query->post->ID;
    	$somefield = get_post_meta($thePostID, $field[name], true);
    	if (empty($somefield)) return '';
    	return $somefield;
    }

    I think this can't work. How do you try to post its value?

  15. Casemon
    Member
    Posted 4 years ago #

    Actually it does work, but only when the shortcode appears in the form; the shortcode doesn't work in the mail fields...

    Any idea why?

  16. Takayuki Miyoshi
    Member
    Posted 4 years ago #

    See shortcode handler functions in other modules, then you'll find what they are supposed to do.

  17. Casemon
    Member
    Posted 4 years ago #

    More specifically please?

    What am I looking for here? And what more, how do I get what I don't know I'm looking for to result in the desired behavior?

  18. Takayuki Miyoshi
    Member
    Posted 4 years ago #

    As you can see in this wpcf7_text_shortcode_handler() in modules/text.php, shortcode handler function should output a form input element. Your wpcf7_customfield_shortcode_handler() doesn't, right?

  19. Casemon
    Member
    Posted 4 years ago #

    It would seem so, I'm not so well versed in all this.

  20. Takayuki Miyoshi
    Member
    Posted 4 years ago #

    Try this

    function wpcf7_customfield_shortcode_handler($field) {
    	global $wpcf7_contact_form;
    	global $wp_query;
    	$thePostID = $wp_query->post->ID;
    	$somefield = get_post_meta($thePostID, $field[name], true);
    	if (empty($somefield)) return '';
    	//return $somefield;
    
    	$html = '<input type="hidden" name="' . esc_attr( $field[name] ) . '" value="' . esc_attr( $somefield ) . '" />';
    	return $html;
    }
  21. jomrenang
    Member
    Posted 4 years ago #

    somebody can help me???
    i just install contact for 7.thats very nice. but the problem is, upload tag for upload not working very well. image not upload as we want.
    any advice?

  22. grafixidea.com
    Member
    Posted 4 years ago #

    I'm just need to add ( onFocus="this.value=' ';this.onfocus=' ';" ) to "textarea" to clear the default value. I'm not a PHP guru to do that my self, Any idea :( Great plugin by the way.

  23. grafixidea.com
    Member
    Posted 4 years ago #

    OK I figured that...

    I just modified (../modules/textarea.php ) in the line that says (line #85 I guess):

    $html = '<textarea name="' . $name . '"' . $atts . '>' . esc_html( $value ) . '</textarea>';

    To be:

    $html = '<textarea name="' . $name . '"' . $atts . ' onFocus="this.value=\'\';this.onfocus=\'\';" >' . esc_html( $value ) . '</textarea>';

    and it works :)

  24. grafixidea.com
    Member
    Posted 4 years ago #

    By the way, I'm trying to find better way. Because I guess you will need to repeat this modification with every update for the plugin.

  25. grafixidea.com
    Member
    Posted 4 years ago #

    Hey, just did that using jQuery. You didn't have to modify any thing, just add the following code to your theme's index header. And the good news: it works for both INPUT and TEXTAREA tags :)

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
    <script  type="text/javascript">
    $(function() {
    	$('input, textarea').each(function() {
    		var field = $(this);
    		var default_value = field.val();
    
    		field.focus(function() {
    			if (field.val() == default_value) {
    				field.val('');
    			}
    		});
    
    		field.blur(function() {
    			if (field.val() == '') {
    				field.val(default_value);
    			}
    		});
    	});
    });
    </script>

    Hope that I helped
    Enjoy ;)

  26. grafixidea.com
    Member
    Posted 4 years ago #


  27. JonasVorwerk
    Member
    Posted 3 years ago #

    @Casemon: Did you find a solution? Looking for the same here.. want to add a wp_custom_field to the CC contactform7 field and can't get it working

  28. one3rdnerd
    Member
    Posted 3 years ago #

    Id like to do the same Jonas. Anyone?

  29. one3rdnerd
    Member
    Posted 3 years ago #

    Casemon is secretive huh

  30. phycel
    Member
    Posted 3 years ago #

    If you want to pass a custom field through the contact form, you need to create a custom module for such, or just edit the special-mail-tags.php file in /wp-content/plugins/contact-form-7/modules/. The problem with editing that file is your edits will be lost when you update. But just to give you a quick solution, here's how you can do it...

    in the special-mail-tags.php file, add this line of code just before the return $output on line 69:

    elseif ( '_post_customfieldname' == $name )
    	$output = get_post_meta($post->ID, 'customfieldname', true);

    Making 'customfieldname' whatever you want the name to be. Make sure you add the custom field to your post with the name you specified and your value.

    Then in your form settings, add [_post_customfieldname] wherever you want to output that custom field.

    I would highly recommend creating a separate module php file for such things, that way when you upgrade, you can just re-upload your custom module files easily to keep your customizations. But this should help you understand how it can be done.

    Good Luck!

Topic Closed

This topic has been closed to new replies.

About this Topic