Extending contact form 7

My latest client requested a “careers” section on his website, with the possibility to apply to a particular job offer directly from the description page. A form should be displayed on this page, that simply sends an email to the company’s careers department.

As I developed the website using WordPress and a custom theme, I dug in the excellent contact form 7 plugin and created easily my custom forms.

contact form 7 extra option

An obvious requirement: if the careers department receives an email sent from a form on the website, it should contain the job title or reference. However, there is nothing in contact form 7 that allows you to include out of the box such a reference that could be a post custom field or the title of the page.

So I created my own contact form 7 custom input that adds a hidden input field containing the page title – which in my case contains the job description on a position page.

Once you have added the required code, you should see the directly in the generator drop down menu when creating a form, and the syntax to use in your form is as simple as the other tags used by contact form 7:

[pagetitle field_name]

To add support for a hidden page title, simply add a file named pagetitle.php in the contact form 7 modules directory:

contact-form-7 >modules > pagetitle.php

<?php
/**
** A base module for [hidden page title]
**/

// Shortcode handler

wpcf7_add_shortcode( 'pagetitle', 'wpcf7_pagetitle_shortcode_handler', true );

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

    $name = $tag['name'];

    $options = (array) $tag['options'];
    $values = (array) $tag['values'];

    $atts = '';
    $id_att = '';

    $class_att .= ' wpcf7-pagetitle';

    foreach ( $options as $option ) {
        if ( preg_match( '%^id:([-0-9a-zA-Z_]+)$%', $option, $matches ) ) {
            $id_att = $matches[1];
        }
    }

    if ( $id_att )
        $atts .= ' id="' . trim( $id_att ) . '"';

    global $wp_query;
    $thePostTitle = $wp_query->post->post_title;

    $value = $thePostTitle;

    $html = '<input name="'.$name.'" type="hidden" value="' . esc_attr( $value ) . '"' . $atts . ' />';

    return $html;
}

// Tag generator

add_action( 'admin_init', 'wpcf7_add_tag_generator_pagetitle', 50 );

function wpcf7_add_tag_generator_pagetitle() {
    wpcf7_add_tag_generator( 'pagetitle', __( 'Hidden page title', 'wpcf7' ),
        'wpcf7-tg-pane-pagetitle', 'wpcf7_tg_pane_pagetitle' );
}

function wpcf7_tg_pane_pagetitle( &$contact_form ) {
? >
<div id="wpcf7-tg-pane-pagetitle" class="hidden">
<form action="">
<table>
<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>

</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="pagetitle" class="tag" readonly="readonly" onfocus="this.select()" /></div>

<div class="tg-mail-tag"><?php echo esc_html( __( "And, put this code into the File Attachments field below.", 'wpcf7' ) ); ?><br /><span class="arrow">⬇</span> <input type="text" class="mail-tag" readonly="readonly" onfocus="this.select()" /></div>
</form>
</div>
<?php
}

?>

Enjoy!

comments

  • Detronizator Sat, 11 Sep 2010 - 21:52

    OT: You do program Ruby and PHP. What's wrong with Python then? ;)

  • Matt Sun, 12 Sep 2010 - 13:59

    @Detronizator: Did I ever say anything about python being wrong? =D

  • Dan Sun, 31 Oct 2010 - 11:06

    Hi I tried using this addon but it didn't work I added this code in the form area: [pagetitle field_name] and this code in the message area: [field_name] Any ideas what am I doing wrong? Thanks, Dan

  • Matt Sun, 31 Oct 2010 - 11:12

    Dan, the code you wrote and the places you put them seem correct. Did you add the code above in a pagetitle.php file in the contact-form-7 plugin modules directory as explained in the post above? If you did, try deactivating the plugin and reactivating it again.

  • Dan Sun, 31 Oct 2010 - 11:19

    WOW! what a speedy reply. I reactivated the plugin as you said and now it works Thanks alot Dan

  • Matt Sun, 31 Oct 2010 - 11:23

    No probs, glad it works for you!

  • Aure Mon, 01 Aug 2011 - 11:23

    Hi, good job m8. It was fine until I upgraded my CF7 plugin to 2.4.6 version. Now "Hidden page title" gone from dropdown menu :\

    • Matt Thu, 18 Aug 2011 - 11:11

      Hi Aure, The problem you have is that I haven't submitted my change to the official plugin, so when you updated CF7, the change you have done simply disappeared. I'll try to contact the plugin author see if this can be integrated! Until then you will have to apply the change again I'm afraid.

  • Matt Fri, 19 Aug 2011 - 09:58

    Aure, the author of the plugin pointed me towards this http://wordpress.org/extend/plugins/contact-form-7-modules/ I haven't tried it yet, but it certainly looks on paper to do what I needed when I extended CF7 in the first place! You might want to give it a go!

  • Miriam de Paula Wed, 01 Feb 2012 - 21:47

    Hi ... I'm having the same problem as Dan: http://blog.ekynoxe.com/2010/09/06/extending-contact-form-7/#div-comment-762 Please, somebody help me? The extra field data are not sent in the mail .... =(

    • Matt Wed, 01 Feb 2012 - 22:02

      Hi Miriam, which version of WPCF7 are you using? It seems there is support for a hidden field now. If you are still using an older version and try to implement the change documented above, have you uninstalled and re-installed the plugin like Dan did? It solved his problem apparently.

  • Miriam de Paula Wed, 01 Feb 2012 - 22:48

    Hi Matt... Thanks for responding. I created a field that lists posts from custom post type. It is working. But I can not send the selected data in the email. What I did wrong?

  • Miriam de Paula Wed, 01 Feb 2012 - 22:56

    Hi Matt. Very Thank you! I looked at the plugin's version, and there was an update (3.1). After upgrading, I got to the extra field work! Thank you! Thank you! =)

  • Matt Thu, 02 Feb 2012 - 07:08

    That's brilliant!! Glad you sorted out. Al the best!

  • davetiye Sat, 16 Mar 2013 - 08:05

    i want to put title in contact form but i couldnt. i also searching this problem on google and i find this page. thank you a lot i solve the problem.

Comments are disabled temporarily until I find a suitable system, but you can still send a comment by email and I'll add it to this post. Thanks!