• Resolved koertho

    (@koertho)


    I’m using WordPress 4.4.2 with CF7 4.3.1

    I need a custom page with a contact form for each author. I crated a template with a loop to use the [_post_author_email] tag. But the output is not the e-mail, only the tag itself (“[_post_author_email]”). I tried and searched long for a solution, also tried this solution, but still no luck. Where is the problem?

    $query = new WP_Query(array(
    	'posts_per_page' => 1,
    	'author '	=> $user->id
    ));
    if ( $query->have_posts() ) :
    	while ( $query->have_posts() ) :
    	$query->the_post();
    	the_title(); //just to check everthings loads correct
    	the_author(); //just to check everthings loads correct
    	echo do_shortcode( '[contact-form-7 id="72" title="Direktkontakt"]' );;
    	endwhile;
    endif;

    https://wordpress.org/plugins/contact-form-7/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    Where have you put the [_post_author_email] into?

    Thread Starter koertho

    (@koertho)

    Currently in the form-code. I also tried to put it into the receiver Field, but got an error.

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    Thread Starter koertho

    (@koertho)

    Ok, but like I said, I also tried to put it in the TO-field and got an error (I just tried it again). Also also tried to put it in the message body and the resulting mail just shows [_post_author_email].

    EDIT:
    I tried it with an normal page (without custom loop) and shortcode direct in the page-content (without do_shortcode), that works. But I need it more flexible. I have on each author page an contact link which leads to a dedicated contact page. I pass the author-id via get variable, that works. Then I create the loop (the one I included in the first post), to make usage of the [_post_author_email], cause I don’t see any other way to do this (like put the receiver in the shortcode).
    The problem is either the custom loop (on an page with custom page template) or the do_shortcode-function

    Thread Starter koertho

    (@koertho)

    I found the reason for my problem in the source code of the plugin. There were two problems in my case.

    #1:
    When using the _post-special-tag, the plugin searches for the ID of the post in the hidden field (_wpcf7_unit_tag) of the form. When using PHP to display the form (with do_shortcode), the IDs are not inserted correct (wpcf7-f172-o1 instead of wpcf7-f172-p152-o1). The solution is to insert the values:

    echo str_replace('wpcf7-f172-o1','wpcf7-f172-p'.get_the_ID().'-o1',do_shortcode('[contact-form-7 id="172" title="Direct contact"]'));

    [source]

    #2:
    That alone wasn’t enought. The plugin is using the ID to fetch the post data from the database instead of taking it from the current post object. I created a post with the WP_Post-Class without writing it to the database (cause I don’t need it there). But because the plugin fetches the data from the database, this doesn’t work. The solution was to write a custom post type (if you don’t want to create a post or page for each user) and check, if there is already a entry (post) for this user, if not, then create one (with wp_insert_post).

    Here my complete code (improvement suggestions are welcome):

    $query = new WP_Query(array(
        'post_type'		=> 'usercontact',
        'posts_per_page' 	=> 1,
        'author'		=> $user->ID
    ));
    if ($query->post_count < 1) {
        wp_insert_post (array(
            'post_title'   	=> 'Konaktformular',
            'post_author'	=> $user->ID,
            'post_type'	=> 'usercontact',
            'post_status'	=> 'publish'
        ));
        $query = new WP_Query(array(
            'post_type'		=> 'usercontact',
            'posts_per_page' 	=> 1,
            'author'		=> $user->ID
        ));
    };
    
    if ( $query->have_posts() ) :
        while ( $query->have_posts() ) :
            $query->the_post();
            echo str_replace(
                'wpcf7-f172-o1',
                'wpcf7-f172-p' . get_the_ID() . '-o1',
                do_shortcode('[contact-form-7 id="172" title="Direkter Kontakt"]')
            );
        endwhile;
    endif;

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Special Tags not working’ is closed to new replies.