如何从当前用户提交的表单中获取GravityForm条目ID?

时间:2013-04-19 作者:tobe

Case: 当用户提交(重力)表单时,插件会自动为该特定表单生成唯一的条目ID。在我的情况下,表单设置只允许用户提交表单一次,并且只有在用户已注册的情况下。当用户提交表单时,将创建一个包含表单内容的页面。该页面的URL是根据条目ID(www.example.com/entry-ID)动态生成的。

Question: 在用户提交表单并登录后,我想显示一个指向用户创建的页面的链接。不仅是在提交之后,而且每次用户再次登录时。但是,如果该用户已登录,如何在页面上获取并显示该用户的条目ID呢?

我在页面上有这个代码。php来识别用户并确定要显示的内容:

<?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 } ?>
这是我用来创建表单(ID=2)以创建“页面”的函数:

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;
}

1 个回复
最合适的回答,由SO网友:Pat J 整理而成

您可以使用gform_after_submission hook[1] 要向用户的元信息中添加条目ID(如果有多个表单,可能还有表单ID,以尽量减少混淆),请使用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 );
}
?>
如果您使用GForms创建帖子(或页面,大概是这样),那么似乎有一种更简单的方法:

<?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 );
}
?>
要向生成该页面的用户显示指向该页面的链接,可以将以下内容添加到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;
}
?>
根据您的评论,假设您的页面生成是按照原始问题中的要求完成的(即,example.com/{entry-ID}, 您只需要可以插入页面模板文件的代码(page.php 或类似),我的建议如下:

将条目ID添加到用户元信息中,如第一个代码段中所述,该代码段以add_action( \'gform_after_submission\', \'wpse96468_map_user_to_entry\', 10, 2 );. 所有代码都可以放入functions.php 文件,以便重力窗体始终可以使用该文件。

然后将以下代码示例添加到页面模板文件中(page.php 默认情况下):

<?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
    }
?>
我想这应该符合你的要求。

重力表单用户文档要求您登录其网站。

[2]http://www.gravityhelp.com/documentation/page/Entry_Object -- 你在寻找post_id 在页面中。

结束

相关推荐

我可以将两个角色传递给GET_USERS函数吗?

我想获得角色学术和学生的用户。我已经尝试使用下面的代码(传递了一系列角色,但显示了所有用户,因此显然不起作用。$roles=array(\'academic\',\'student\'); $args =array(\'role\'=>$roles); $users=get_users( $args ); foreach ($users as $user) { echo \'<li>\' . $user->user_email .