Post Title
-
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 ! π
-
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!
Yes, it is the title of the page the form is displayed.
Any clue?
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 useget_the_title()to get the title of the page the form was submitted from.Was this helpful?
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 8 years, 10 months ago by
dinhuakt.
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.
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().
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! π
Worked like a charm!
Very thank you for your support Fabian.
Nice plugin!
-
This reply was modified 8 years, 10 months ago by
The topic ‘Post Title’ is closed to new replies.