• Resolved dinhuakt

    (@dinhuakt)


    Hi !

    I have created a contact form and don’t figured out how to insert the post title in the sent email content.

    Could u help me?

    Ty in advance ! ??

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author fabianlindfors

    (@fabianlindfors)

    Hello! I’m certain I can help you out. ??

    Not sure I understand which post title you are referring to. Is it the title of the page the form is displayed on or something else?

    Thanks for creating a ticket!

    Thread Starter dinhuakt

    (@dinhuakt)

    Yes, it is the title of the page the form is displayed.

    Thread Starter dinhuakt

    (@dinhuakt)

    Any clue?

    Plugin Author fabianlindfors

    (@fabianlindfors)

    Hi!

    You could use the af/form/email/content (documentation) filter to set your email content programmatically. If you hook into this filter I believe you should be able to use get_the_title() to get the title of the page the form was submitted from.

    Was this helpful?

    Thread Starter dinhuakt

    (@dinhuakt)

    That’s the way!

    I just not figured out how to insert post title.

    <?php
    
    function filter_email_subject( $subject, $email, $form, $fields ) {
    	// Alter the subject line
    	$subject = get_the_title();
        
        return $subject;
    }
    add_filter( 'af/form/email/subject/key=MY_FORM_KEY', 'filter_email_subject', 10, 4 );

    Not working ??

    • This reply was modified 7 years, 7 months ago by dinhuakt.
    Plugin Author fabianlindfors

    (@fabianlindfors)

    Looks alright except for the last line. Did you replace “MY_FORM_KEY” with the unique key of your form? ??

    You can find the key underneath the title field when editing a form in WordPress.

    Thread Starter dinhuakt

    (@dinhuakt)

    Yes, I have used the unique key.

    function filter_email_subject( $subject, $email, $form, $fields ) {
    	// Alter the subject line
    	$subject = get_the_title();
        
        return $subject;
    }
    add_filter( 'af/form/email/subject/key=form_596e935ba3e3b', 'filter_email_subject', 10, 4 );

    It sent me an email with a “no subject”. It seems not getting the title value from get_the_title().

    Plugin Author fabianlindfors

    (@fabianlindfors)

    I’m sorry. I incorrectly assumed that the global $post object would be set and that get_the_title() would work as expected but that was not the case.

    This makes it a little bit harder but still quite easy if you ask me. We need to add another function which adds the current page ID to the form args before it’s rendered:

    
    function add_page_to_args( $args, $form ) {
    	$args['page_id'] = get_the_id();
    	
    	return $args;
    }
    add_filter( 'af/form/args/key=form_596e935ba3e3b', 'add_page_to_args', 10, 2 );
    

    And then we can adjust your email subject filter a bit:
    `
    function filter_email_subject( $subject, $email, $form, $fields ) {
    $page_id = AF()->submission[‘args’][‘page_id’];

    return get_the_title( $page_id );
    }
    add_filter( ‘af/form/email/subject/key=form_596e935ba3e3b’, ‘filter_email_subject’, 10, 4 );
    `

    Now instead of picking up the current page ID from the global $post object we instead get it from the form args which we set in the first filter. I tried this code in my local environment and it worked as expected. Try it out! ??

    Thread Starter dinhuakt

    (@dinhuakt)

    Worked like a charm!

    Very thank you for your support Fabian.

    Nice plugin!

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Post Title’ is closed to new replies.