As a website owner, it’s essential to have a way for visitors to contact you. Gravity Forms is a powerful plugin for WordPress that allows you to create contact forms quickly and easily and provides a ton of additional functionality. One useful feature of Gravity Forms is the ability to get the post author’s information automatically when the form is submitted on a post page. In this blog post, we’ll show you how to get the post author’s information and submit it with your Gravity Form.
Step 1 - Install and activate Gravity Forms plugin
The first step is to install and activate the Gravity Forms plugin on your WordPress site. Gravity Forms is a paid forms plugin, but it’s worth the price if you want to do more advanced things than just sending simple forms. Also, you will get less spam than with the standard Elementor form without adding any additional security measures.
Once you’ve purchased your package, you can download the plugin in in your account.
Back in WordPress, log in to your backend and go to your Plugin section. Select “Add new” and upload the zipped Gravity Forms plugin that you previously downloaded.” Once you find the plugin, click on “Install Now” and then “Activate.”
Step 2 - Create the gravity form and add your fields
After installing Gravity Forms, you can create a contact form by navigating to Forms > New Form in your WordPress dashboard. Give your form a name and select the fields you want to include, such as Name, Email, Message, etc.
Step 3 - Add a hidden field
To submit the author name with the form submission, you have to create a hidden field to capture that information. To do that, add a simple text field or email field and include a custom CSS class (I call it “author_email” but the naming convention is up to you).
Step 4 - Add a custom parameter
Now enable the option ” Allow field to be populated dynamically.
This allows you to enter a custom default value into the field when the form loads for the user.
Add a custom Parameter name – I used “admin_email”, but again, the naming convention is up to you.
Save the form.
Step 5 - Adjust the notification
Now go to settings and adjust the “Send To” option:
Choose “Select a Field” and choose the custom field you have created in Step 3.
This will pull in the author email that will be added dynamically into this field (see next steps).
Step 6 - Hide the Field with Custom CSS
Add “display: none” to the classname you have set in Step 3 in the CSS editor of your choice (either via the customizer or the theme file editor).
You do not want the user to see the field, as its sole purpose is to dynamically pull in the author email and make it available to the form when its sent.
Note: In Gravity Forms you can now also set a form field as hidden instead, which will save you adding a custom CSS class and this snippet.
.author_email {
display: none;
}
add_filter( 'gform_field_value_author_email', 'populate_post_author_email' );
function populate_post_author_email( $value ) {
global $post;
$author_email = get_the_author_meta( 'email', $post->post_author );
return $author_email;
}
Step 7 - Get the Post Author Email
Under Appearance/Theme File Editor, add the code snippet to get the post author email.
Add the code to the functions.php file.
This snippet gets the author information from the page from which the form is sent and adds it to one form field.
To add the information to the hidden field, adjust the first and second parameter name: “autor_email” should correspond to the field name of the form field created in Step 3.
Step 8 - Add your form to a page and test it
Now add the form to a page. Whenever the form is sent, it will now grab the author info from that particular page and send the form to the post author.
After setting up the post author field, it’s a good idea to test your form to make sure everything is working correctly. Submit the form on a post page and check if the post author’s information is included in the submission.
With Gravity Forms, you can easily get the post author’s information and include it in your contact form submissions. By following the steps outlined in this blog post, you’ll be able to set up your form to automatically capture this information and make it easier for visitors to contact you.