Posting via the Front End: Advanced Submission

Today, we will be continuing with our mini-series on inserting posts via the front end, but in this part we will be exclusively looking into how to manage custom meta fields which we may have within our post type. So, let's get ready and begin!


Introduction

We are now in part 3 of our mini-series, and if you have not read the other two parts then I advise you do because we will be picking up from where we left off. Our goal after completing this mini-series should allow the user to submit posts via the front end, along with editing and sending the post to trash; all without being on the WordPress Dashboard. These methods can be used in both a theme or plugin and be very adaptable to achieve very advanced and complex submissions.

The demo and the download files are a stripped down theme which has been created for just the purposes of this tutorial.

So open up your favourite text editor and let’s begin!


Step 1 Inserting Custom Meta Box

Let's begin by first creating a folder called include, in this folder we will create a file called post-meta.php. We will be creating our custom meta box inside of this file. I will be going through this fairly quickly but if you want to read about all the wonders you can achieve with custom meta boxes, Tammy Hart wrote an amazing tutorial series called Reusable Custom Meta Boxes.

Inside of our post-meta.php, we will create a prefix to ensure that we have a unique identifier for all of our fields, also we will begin by creating an array to hold all of our information to create a custom meta box. The following code is for creating an ID for the custom meta box, setting a title, where the meta box will appear (which post type), and the fields which it will have:

Next, we will be creating our meta box and we do this by creating a function. Inside this function we will be using the WordPress function: add_meta_box.

The following code displays how we created our meta box, along with using the action hook add_meta_boxes:

Now that we have created our meta box, along with having all the fields set with the information we want to store, we need to display our meta box. We do this by creating another function with the same name as our third parameter in our add_meta_box function. In our case, we create a function called: vsip_display_post_meta.

The following code gets each field inside of our array that holds all of our information and checks the type of field it is and outputs the correct field type:

We have created our custom meta box and displayed its content, all we have left to do is save the data once the user has inserted content for the fields. We do this by creating one final function which will save our data correctly. The code is as follows:

Okay, I know we went through that very quick but the focus of this tutorial is how to manage the custom meta via the front end. Now that it's all out of the way and we have our custom meta box set up in our posts, we will continue into how to insert the fields into our insert post and to edit the custom meta fields.


Step 2 Editing Our Insert Posts

We will be editing our insert posts page to compensate for our newly added custom fields. Let's go and open up our template-insert-posts.php and insert some extra fields. As our custom meta fields are text fields, we will insert two extra text input fields into our form, as follows:

We need to have it so when the user clicks 'Submit' it will insert the custom meta information into the appropriate fields. To do this, we need to use the WordPress function: update_post_meta, what this function does is allow us to update the post meta, and we can only update the post meta once we have created a post and have an ID.

So, we will go where we are using the function: wp_insert_post and just above our redirect link is where we will be inserting our custom meta.

The following code does our test to check if we have created a post, and then we use the update_post_meta function and pass the post_id to this function, along with the ID of the field we want to update, and finally the value from our form which we will posting to the meta box:

That's it! We have created a custom meta box and we have updated our insert post form to ensure that we target these fields and they get saved correctly. Next, we will be moving onto how to the edit our custom meta.


Step 3 Editing Our Custom Meta

Now we need to be able to edit the custom meta via the front end, so we will begin by opening up our template-edit-posts.php and inserting our two new input fields which we added to our insert post form:

Next, we need to retrieve our custom post meta of the specific post, we do this by using the WordPress function: get_post_meta inside of our WordPress Loop. Scroll up to where we have our WordPress Loop and insert the following code just below where we got the information for our title and content.

Now that we have the information of the custom post meta, we will now output the values of this into our input fields of our form. The following code is the updated version of the two custom meta fields which we inserted:

As you can see from the code we just inserted, the values for both of our inputs are the variables of our custom meta which we created in our WordPress Loop, you may have noticed that it could output our fields but when we make changes no actual updates are made. This is because we haven't inserted the code to update the post once the user has clicked update.

Just the same way we added our post meta when inserting new posts, we will be doing this inside of our edit template, so add the following code:

That's it! We have done it! We are now able to create a custom meta box, insert meta with the insert post via the front end along with editing the meta via the front end.


Conclusion

That's part 3 complete, you have done it! Well done, you are now capable of inserting posts, editing, and deleting posts via the front end, along with handling any custom meta you have.

That's the end of this series on Posting via the Front End, and I'm glad that you stayed with me whilst we went through our journey.

I would like to say a HUGE thank you for spending the time to read my tutorial series, I hope it helped. Please feel free to leave comments and I will try my best to assist and answer them. You can always contact me directly through my website: www.VinnySingh.co or Twitter @VinnySinghUK.

Tags:

Comments

Related Articles