web-dev-qa-db-ja.com

現在のユーザーのフォーム送信からGravityformエントリIDを取得する方法

ケース: ユーザーが(重力)フォームを送信すると、プラグインは自動的にその特定のフォームに固有のエントリーIDを生成します。私の場合、フォーム設定は、ユーザーが一度だけ、そしてユーザーが登録されている場合にのみフォームを送信することを許可します。ユーザーがフォームを送信すると、フォームの内容を含むページが作成されます。そのページのURLは動的に生成され、entry-ID(www.example.com/entry-ID)に基づいています。

質問: ユーザーがフォームを送信し、ユーザーがログインした後、ユーザーによって作成されたページへのリンクを表示したいのですが。送信直後だけでなく、ユーザーが再度ログインしたときにも毎回。しかし、そのユーザーがログインしている場合、そのユーザーのエントリーIDを取得してページに表示するにはどうすればよいでしょうか。

私はこのコードをpage.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;
}
4
tobe

gform_after_submissionフック [1]を使用すると、 add_user_meta() を使用して、エントリID(および複数のフォームがある場合は混乱を最小限に抑えるためのフォームID)を追加できます。

<?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または同様のコード)に入れることができるコードがほしい場合)、これが私の提案です。

最初のコードスニペット(add_action( 'gform_after_submission', 'wpse96468_map_user_to_entry', 10, 2 );で始まるもの)で説明されているように、エントリIDをユーザーメタ情報に追加します。すべての that コードはあなたのfunctions.phpファイルに入れることができるので、それは常にGravityフォームフォームで利用できるようになります。

次に、次のコードサンプルをページテンプレートファイルに追加します(デフォルトでは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
    }
?>

私はそれがあなたが探していることをするべきだと思います。


[1] Gravity Formsのユーザーマニュアルでは、サイトにログインする必要があります。

[2] http://www.gravityhelp.com/documentation/page/Entry_Object - ページ内でpost_idを探しています。

5
Pat J