Case: When a user submits a (Gravity)form, the plugin automatically generates an unique entry ID for that specific form. In my case the form settings only allow user to submit the form once, and only if user is registered. When user submits the form, a page is created with the content of the form. The URL of that page is dynamically generated and based on the entry-ID (www.example.com/entry-ID).
Question: After user have submitted a form, and user is loged in, I want to show a link to the page that is created by the user. Not only just after submission, but every time user is logged in again. But how to get, and display, that user’s entry ID on a page if that user is logged in?
I have this code on the page.php to identify the user and determine what to display:
<?php if ( is_user_logged_in() ) { ?>
<?php global $wpdb;
$user = wp_get_current_user();
$where = get_posts_by_author_sql( 'page', true, $user->ID );
$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
if ( $count >= 1 ) { ?>
// This is what user see if form is submitted, where '{entry-ID}' should be replaced by the user's entry iD
<h2>Hey <?php echo $current_user->display_name ?>, thank you for submitting the form. Visit your page here: www.example.com/{entry-ID}</h2>
<?php } else { ?>
// If user have not submitted a form, user is shown the info below with the form to submit
<h2>Hey <?php echo $current_user->display_name ?>, Thank you for joining. To create a page please submit the form below:</h2><?php echo do_shortcode('[gravityform id="2" name="just a form" title="false" description="false"]'); ?>
<?php } } else { ?>
// if user is not logged in, user is urged to log in to submit form
<h2><Please log in to create a page <?php do_action( 'wordpress_social_login' ); ?></h2>
<?php } ?>
This is the function I use to make the form (ID=2) to create a ‘page’:
add_filter("gform_post_data", "change_post_type", 10, 2);
function change_post_type($post_data, $form){
//only change post type on form id 1
if($form["id"] != 2)
return $post_data;
$post_data["post_type"] = "page";
return $post_data;
}
You can use the
gform_after_submission
hook[1] to add the Entry ID (and probably the Form ID to minimize confusion if you have multiple forms) to the user’s meta information usingadd_user_meta()
.A better way (maybe)
If you’re using GForms to create a post (or page, presumably), there appears to be an easier way:
To show a link to the generated page to the user that generated it, you can add the following to
functions.php
:In response to your comments
Assuming that your page generation is done as requested in the original question (ie,
example.com/{entry-ID}
, and you just want code you can plunk into a page template file (page.php
or similar), here’s my suggestion:Add the entry ID to the user meta information as detailed in the first code snippet — the one that begins with
add_action( 'gform_after_submission', 'wpse96468_map_user_to_entry', 10, 2 );
. All that code can go in yourfunctions.php
file, so that it’ll always be available to the Gravity Forms form.Then add the following code sample to your page template file (
page.php
by default):I think that should do what you’re looking for.
[1] Gravity Forms user documentation requires you to log in to their site.
[2] http://www.gravityhelp.com/documentation/page/Entry_Object — you’re looking for
post_id
in the page.