So I have custom posts by various users.
Each post has following meta keys:
post_width
, post_height
along with usual data such as title
, description
etc as shown below:
$tags = $_POST['post_tags'];
$custom_field_1 = $_POST['custom_1'];
$custom_field_2 = $_POST['custom_2'];
$post = array(
'post_title' => $title,
'post_content' => $description,
'post_category' => $_POST['cat'],
'tags_input' => $tags,
'post_status' => 'publish',
'post_type' => $_POST['post_type']
);
$pid = wp_insert_post($post);
add_post_meta($pid, 'post_width', $custom_field_1, true);
add_post_meta($pid, 'post_height', $custom_field_2, true);
Now when these posts are displayed in a loop, they each get post_id
by $id = get_the_ID();
Now the fun part
Now, let say there are 5 posts displayed and each has unique post_id
as shown below in the data-post_id
in a button.
<?php echo '<button type="button" class="contact_button" data-post_id="' .$id. '">' ;?>
Open the contact form
<?php echo '</button>';?>
When the button is clicked, then a below contact form will be shown :
<form action="<?php the_permalink(); ?>" method="post">
<p><label for="name">Name: <span>*</span> <br><input type="text" name="message_name" value="<?php echo esc_attr($_POST['message_name']); ?>"></label></p>
<p><label for="message_email">Email: <span>*</span> <br><input type="text" name="message_email" value="<?php echo esc_attr($_POST['message_email']); ?>"></label></p>
<p><label for="message_text">Message: <span>*</span> <br><textarea type="text" name="message_text"><?php echo esc_textarea($_POST['message_text']); ?></textarea></label></p>
<p><label for="message_human">Human Verification: <span>*</span> <br><input type="text" style="width: 60px;" name="message_human"> + 3 = 5</label></p>
<input type="hidden" name="submitted" value="1">
<p><input type="submit"></p>
</form>
As you can see, the contact form is nothing fancy.
Here is what I am trying to achieve
Since there are 5 posts, I want to make it very specific to each post when the contact form is sent.
Here is what I mean.
Let say there are 5 posts as shown below:
Post # 1:
- Title: What a day
- Post Author: Mike
- Post author’s Email: mike@example.com
- Post descript: It is a sunny day
- Width: 14px
- Height: 15px
- Contact Button
Post # 2:
- Title: This is a post 2
- Post Author: Steve
- Post author’s Email: steve@example.com
- Post descript: It is Sunday
- Width: 1222px
- Height: 1233px
- Contact Button
etc.
Now, each post has contact button
and when the button is clicked and contact form is shown, I want to automatically have these post meta
to be filled in (of course hidden from view). So that when the form is submitted, it will have post information and be able to tell which post the viewer contacted from.
I hope I am making sense.
So, here is my question.
How do I call
or save
post meta (such as title, description, custom meta, etc) based on the post_id
of each post? (since each button will have unique post_id
, that will be a good starting point to call
the rest of the data)
Any help will be much appreciated.
Thanks!
Update 1:
So, I have the contact form in a contact.php
which is called via ajax.
The reason why I am using the ajax to call the contact form is to save the bandwidth. Here is a scenario. Let say there are 50 posts on a single page. Then if the contact form
is generated for all the posts, then that will severely increase the total page size.
In order to avoid it, I am implementing ajax to call the contact form only when the button is clicked.
So, here is a question.
How do I “pass” the post_id
to another php file? (contact_form.php) so that the contact form will be unique?
Thanks!
Have you tried using
Something similar to this:
You could find more info here.
https://developer.wordpress.org/reference/functions/get_post_meta/
As for the title you could just do this
https://codex.wordpress.org/Function_Reference/get_the_title
You could then add hidden fields to your form with that data.