Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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;
}
share|improve this question
    
Do you want the Gravity Forms entry ID associated with the user, or the ID of the page that their form submission creates? –  Pat J Apr 19 '13 at 18:55
    
I want to display the entry ID associated with the current user. The aim is to give that user (when logged in) a page title/link to his created page. –  tobe Apr 19 '13 at 20:30
    
There might be an easier way. If you're using Gravity Forms to generate pages, then there's an ID for the new page/post in the Entry object. I'll add another code segment to my answer to show what I'm talking about. –  Pat J Apr 19 '13 at 20:45
    
See above added function code I use to make Gravityforms to create a page (instead of post) if form ID=2 is used. Maybe it is useful to find out why the function to get the entry-ID is not working? –  tobe Apr 20 '13 at 12:48
add comment

1 Answer 1

up vote 2 down vote accepted

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 using add_user_meta().

<?php
add_action( 'gform_after_submission', 'wpse96468_map_user_to_entry', 10, 2 );
// to tie it to a specific form, use the format below,
// replacing '{$form_id}' with the actual form ID
// add_action( 'gform_after_submission_{$form_id}', 'wpse96468_map_user_to_entry', 10, 2 );

function wpse96468_map_user_to_entry( $entry, $form ) {
    // get your user's ID here
    // EDITED -- this should work, 
    // if only logged-in users can submit the form
    $user_id = $entry['created_by'];
    // set the arguments for the add_user_meta() function
    $meta_key = 'gform_entry_id';
    $meta_value = $entry['id'];
    // if you want to pass both the Entry and Form IDs, you can use an array:
    // $meta_value = array( 'entry_id' => $entry['id'], 'form_id' => $form['id'] );
    $unique = true;
        // optional, but the default is false,
        // and if I understand your question, you want this to be unique
    add_user_meta( $user_id, $meta_key, $meta_value, $unique );
}
?>

A better way (maybe)

If you're using GForms to create a post (or page, presumably), there appears to be an easier way:

<?php
add_action( 'gform_after_submission', 'wpse96480_map_user_to_page', 10, 2);

function wpse96480_map_user_page( $entry, $form ) {
    $user_id = $entry['created_by'];
    $meta_key = 'generated_page_id';
    $meta_value = $entry['post_id']; // see note [2] for a link
    $unique = true;
    add_user_meta( $user_id, $meta_key, $meta_value, $unique );
}
?>

To show a link to the generated page to the user that generated it, you can add the following to functions.php:

<?php
add_filter( 'the_content', 'wpse96480_get_generated_page_for_user' );
function wpse96480_get_generated_page_for_user( $content ) {
    if ( is_user_logged_in() ) {
        global $current_user;
        $current_user = get_currentuserinfo();
        $user_id = $current_user->ID;
        $meta_key = 'generated_page_id';
        $single = true;
        $page_id = get_user_meta( $user_id, $meta_key, $single );
        if( strlen( $page_id ) > 0 && is_numeric( $page_id ) ) {
            $page_link = '<a href="' . get_permalink( $page_id ) . '">' . get_the_title( $page_id ) . '</a>';
            $content .= "Hey {$current_user->display_name}, thank you for submitting the form. View your page here: $page_link";
        } else {
            $content .= "Hey {$current_user->display_name}, please fill in the form below<br />\n";
            $content .= do_shortcode('[gravityform id="2" name="Join the movement of Gooders" title="false" description="false"]');
        }
    }
    return $content;
}
?>

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 your functions.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):

<?php
    if ( is_user_logged_in() ) {
        global $current_user;
        // $current_user = get_currentuserinfo();
        // seems I was clobbering the existing $current_user variable
        $user_id = $current_user->ID;
        $meta_key = 'gform_entry_id';
        $single = true;
        $entry_id = get_user_meta( $user_id, $meta_key, $single );
        if( strlen( $entry_id ) > 0 && is_numeric( $entry_id ) ) {
            // we have an entry ID now
            ?>
            <h2>Hey <?php echo $current_user->display_name ?>, thank you for submitting the form. Visit your page here: www.example.com/<?php echo( $entry_id ); ?></h2>
            <?php
        } else {
            // we don't have an entry ID for this user
            ?>
            <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="Join the movement of Gooders" title="false" description="false"]'); ?>
            <?php
        }
    } else {
        // user is not logged in
        ?>
        <h2><Please log in to create a page <?php do_action( 'wordpress_social_login' ); ?></h2>
        <?php
    }
?>

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.

share|improve this answer
    
The code is working (i.e not giving errors) but it is not working as we wished. I created a fresh user, and when logged in, the form showing (that is OK) but after submission the form is still showing (instead of the first option with the url/entry-ID). I have copy-pasted your first(top) code snipet to my functions.php and added the form ID (2) like this on the first line: add_action( 'gform_after_submission_2', 'wpse96468_map_user_to_entry', 10, 2 ); –  tobe Apr 20 '13 at 7:19
    
I also tried the 'Better way (maybe)' function, but that gives the same result. Looks like it is not successful in getting the entry-ID? –  tobe Apr 20 '13 at 12:51
    
In the page.php file, add this right after the $entry_id = get_user_meta( $user_id, $meta_key, $single ); line: echo( "DEBUG: \$user_id = $user_id; \$entry_id = $entry_id<br />\n" ); What does it print out? –  Pat J Apr 20 '13 at 18:37
    
It shows "DEBUG: $user_id = ; $entry_id =" on the webpage just above the <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="Join the movement of Gooders" title="false" description="false"]'); ?> –  tobe Apr 20 '13 at 19:55
    
I've updated the page.php code, after testing it on my local installation. I've commented out the get_currentuserinfo() call because it looks like it was blanking the $current_user global variable. Hopefully that'll do what you need. –  Pat J Apr 20 '13 at 20:11
show 4 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.