March 8th, 2016: Updated snippet to only set future publish date if date field has a value.
With Gravity Forms, allowing users to submit posts to your WordPress website couldn’t be easier. Currently, you can set the default status of the generated WordPress post as “Draft”, “Pending” or “Published”.
This snippet provides the ability to schedule the post to be published at a future date. It can be configured to publish on a user-defined date/time (via submitted form data), a static date/time, or a combination of the two.
<?php | |
/** | |
* Gravity Wiz // Gravity Forms // Schedule a Post by Date Field | |
* | |
* Schedule your Gravity Form generated posts to be published at a future date, specified by the user via GF Date and Time fields. | |
* | |
* @version 1.0 | |
* @author David Smith <david@gravitywiz.com> | |
* @license GPL-2.0+ | |
* @link http://gravitywiz.com/... | |
*/ | |
// CHANGE: "546" to the ID of your form | |
add_filter( 'gform_post_data_546', 'gw_schedule_post_by_date_field', 10, 3 ); | |
function gw_schedule_post_by_date_field( $post_data, $form, $entry ) { | |
$date = $entry['7']; // CHANGE: "7" to the ID of your Date field | |
$time = $entry['8']; // CHANGE: "8" to the ID of your Time field | |
### don't touch the magic below this line ### | |
if( empty( $date ) ) { | |
return $post_data; | |
} | |
if( $time ) { | |
list( $hour, $min, $am_pm ) = array_pad( preg_split( '/[: ]/', $time ), 3, false ); | |
if( strtolower( $am_pm ) == 'pm' ) { | |
$hour += 12; | |
} | |
} else { | |
$hour = $min = '00'; | |
} | |
$schedule_date = date( 'Y-m-d H:i:s', strtotime( sprintf( '%s %s:%s:00', $date, $hour, $min ) ) ); | |
$post_data['post_status'] = 'future'; | |
$post_data['post_date'] = $schedule_date; | |
$post_data['post_date_gmt'] = get_gmt_from_date( $schedule_date ); | |
$post_data['edit_date'] = true; | |
return $post_data; | |
} |
Getting Started
Check requirements
- Make sure you have Gravity Forms installed and activated.
- Already have a license? Download Latest Gravity Forms
- Need a license? Buy Gravity Forms
- Make sure you have Gravity Forms installed and activated.
Install the snippet
- Copy and paste the entire snippet into your theme’s functions.php file.
Configure the snippet
- Change the form ID and the
$date
and$time
variables to match your date and time field.
- Change the form ID and the
Configuration Options
Not sure how to change the variables? Follow the step-by-step instructions below.
Setting the Form ID
add_filter( 'gform_post_data_546', 'gw_schedule_post_by_date_field', 10, 3 ); |
Update 546
to the ID of your form. This will ensure that only the posts created by the form you specify are scheduled for the future.
Setting the Field IDs
$date = $entry['7']; // CHANGE: "7" to the ID of your Date field | |
$time = $entry['8']; // CHANGE: "8" to the ID of your Time field |
Update the 7
on the first line to the ID of your Date field and the 8
on second line to your Time field. This is how the code knows which Date and Time field to use to set the scheduled publish date.
Bonus: Static Time, User-specified Date
$date = $entry['7']; // CHANGE: "7" to the ID of your Date field | |
$time = '09:00 am' |
With this option, the user selects which date the post should be published on; however, you (the admin) would set a static time for it to be published. This is useful when you don’t want to overburden the user with options.
Make sure to include a leading zero if you’re setting a single digit hour and a space between the numbers and the am/pm. The 24-hour time format is also supported.
Bad: | 2:30pm |
Good: | 02:30 pm |
Good: | 14:30 |
FAQ
How do I apply this to multiple forms?
I wrote an alternate version that allows you to configure this for multiple forms. You can view it here.
Was this helpful?
If you’ve found this snippet helpful or have an idea to improve it, let us know in the comments!
Did this resource help you do something awesome with Gravity Forms? Then you'll absolutely love Gravity Perks; a suite of 32+ essential add-ons for Gravity Forms with support you can count on.
Hi, would this also work with a date set in the past?
Hm, I actually don’t know… Let me know what you find. :)
With the date set in the past, it didn’t work for me. I presume it’s down to the fact that the relatively new “Advanced Post Creation” addon controls the post date of the submitted form. The settings allow for “entry date” or a “custom date & time” ; the latter allowing values like “Today” and “next week” apparently. I tried a merge tag from my date field as the value, but that didn’t work.
Any ideas would be much appreciated, thanks!
Hello Brian, this is a strange interaction indeed. We would like to take a closer look into this for you. Could you please reach out to us via our support line found here with all the information you can regarding this issue as well as a form export and we would be more than happy to assist you.
Hi David,
Is there a way to set a duration for the post. So, not only a start date, also an end date?
Thanks Aldo
Hi Aldo, you’ll need something to expire the posts (like this plugin). Then you’ll need to find out where they’re storing the expiration date/time (probably in a custom field) and set that custom field on submission.
A late reply, could help someone else… Yes, there is, and I am and not using the Post-Expirator plugin since it does not let you set the expiration time via custom/meta field by default. What I did was add a Start Time field (actually a Custom Field and selected Time as the type of input) in the form which saves to a custom/meta field, which is just simple text line with a time format to match your needs. Then I also added another time field this one to schedule/expire the post. Make sure to use 24 hour format for this, as I explained in an earlier comment.
Hi, David. That way is very complicated for me, but thanks! anyway.
Hi, About Merge Tags, how can you Displays/insert the entry date of the submission? Many thanks
Hi Susan,
{entry:date_created}
should do the trick.DAVID, Many many thank!!
One more thing, appears like this:
{entry:date_created} / 2017-07-12 18:35:31
Is it posible just with the day?, I mean without the time.
Hi Susanna, check out this snippet which lets you add a date modifier to date-based merge tags:
https://gist.github.com/spivurno/45d589ad4b79ae88ad2ee0f695407ec3
{entry:date_created:m-d-Y}
For those afraid to let their users submit / create posts scheduled to publish at a certain date or time, whether they’ve been reviewed or not, change line 37 in the above sample: $post_data[‘post_status’] = ‘future’; changing future to draft makes the post stay as a draft, whether the date / time passes or not: $post_data[‘post_status’] = ‘draft’;
That way you can still have an admin review the content and hit “schedule”.
Great tip, Craig! :)
Hi David,
Like Ricardo (above) I have a number of forms that I would like to schedule but the link to the pastie that you provided is empty. Can you let me have the snippet please.
Many thanks, Alan
Hi Alan, could you confirm which pastie you’re referring to?
Hi Alan, here’s a more permanent link: https://gist.github.com/spivurno/4dfae7f6dd4bd15b8a6986436989c687 I’ve also updated the article above with this example.
Thank you David,
Not only are you a wizard, you’re a star too
Thanks, Alan
Awesome! Glad to help.
Hi David,
Thanks for your help, I have the wrong field ID’s inserted. All working perfectly now. I have lots of forms and am interested in but your pastie link for multiple forms (above) is empty. Could you repost that code please.
Many thanks, Alan
Hi David, this is the code for use two forms in the same page. http://pastie.org/10820937 I’ve a little problem with this code, when I schedule a post with an image in next month the image not appear…
Hm, not sure why that would happen. Unfortunately, it’s not something we can afford to dig into for our free resource. If you’d like to hire us for some one-off support, we’d be happy to help. Get in touch.
Hello David,
Thanks for all your great work – Super helpful site.
I have added the snippet and have the schedule calendar on the form but when I fill it in and post it gets published anyway.
Am I missing something regarding the publish settings in the post title? I thought this snippet would override that.
The site isn’t live but I could give you access
Thanks in advance, Alan
Hi Alan, if you’d like to include a pastie of your configuration of this snippet, I’ll be happy to take a look and make sure it’s setup correctly.
Hi David, Thanks. I hope i’ve done this right. Not used pastie before
http://pastie.org/10817626.js
I don’t see anything out of sorts there. I just confirmed that this is working for me locally. Not sure what is happening on your end. If you’d like us to install this for you we do offer an installation service for $99. Get in touch if you’re interested,
Great function, but… Can I use current date and time if user let the fields empty? How can I do this?
Hi Claudio, I’ve just updated the snippet to support this scenario.
GREAT!!!! Thankoles!!!
Hi David, I have a little problem with the code for two forms in the same page. When I schedule a post with image in next month the image not appear. Have you any idea? Thanks. Riccardo
This is exactly what I need, but it keeps throwing an error when I update my functions.php.
This is what I am trying to add to the file:
[code removed]
I have also watched the video and removed the extra <?php opening tag
Hi Shana, could you share a pastie with your functions.php code + the snippet? The code you shared here was mostly lost due to auto-formatting.
Hi David, thanks for this code, work perfectly with only one form! I have two forms (with two ID). It’s possible to scheduling the posts created with two different forms with the same code?
Thanks a lot Riccardo
Hi Riccardo, here is an example of setting this for multiple forms. You’ll want to remove the form ID from the filter name and then follow the template in that modified snippet to specify the correct fields for each form ID.
Happy to answer any questions you have on this.
Hi David, thanks al lot. It’s work perfecrly! Riccardo
Happy to help!