In the previous post, we have created features that allow users to sign up a Guest Author account then submit a post from the frontend. If we stop here, the submitted post will be published right after the Guest Author press the Submit button.

Posts will be published immediately
Posts will be published immediately

That causes uncontrollable post quality. For this reason, we keep on with Meta Box to create extra features to censor and manage all the posts. These also are the 3rd and 4th requirements from my client which I mentioned in part 1.

Change the Default Status of the Guest Author’s Posts

Every post from Guest Author must be reviewed before going on live. It follows that those posts’ status will be “pending” first when their Guest Author submits. To do that, we do a simple action only.

Go to the edit page of the post submission page, add post_status=“Pending” into the shortcode in the content field.

Add a parameter to the shortcode
Add a parameter to the shortcode

Save the page and submit a new post for a test. That post will be pending.

Post is in pending status right after Guest Author’s submission
Post is in pending status right after Guest Author’s submission

Post Management for Guest Author

I’m going to create the post-management page in the My Account page and use the Bootstrap library creating Navs interface with two tabs: Account, and Your Post.

Specimen page from Bootstrap
Specimen page from Bootstrap

The Account tab actually is the My Account page we created in part 1. Now, we are creating Your Post tab.

Use the Bootstrap to create a Table for post listing.

List of posts from the Guest Author
List of posts from the Guest Author

From now on, we will use some codes and available functions to do these actions:

  1. Get the current user information
  2. Get all the post which submit by the current user
  3. Insert URL of the edit page to the pending post

Step 1: Get the Current User Information

Add the wp_get_current_user() function to the page-account.php file:

<?php
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
$user_email = $current_user->user_email;
?>

In there:

  • $current_user: current user information in the type of an Object
  • $user_id: get the ID of the current user
  • $user_email: get the email of the current user

You can add some else to get other information about the current user. It’s up to you.

Step 2: Get the List of Submitted Post by the Current User

As step 1, we add the following function to the page-account.php file:

$args_post_by_author = array(
'author'        => $user_id,
'posts_per_page'=> -1,
'post_status'   => array('publish', 'pending')
);
$posts_author = get_posts($args_post_by_author);

In there:

  • $arg_post_by_author: an array which holds all the essential parameters for information exporting
  • $post_author: List of post exported by the above array

I bound three parameters to export information, which are:

  • author: use $user_id to get the ID of the user who submitted the post;
  • post_per_page: the number of posts displayed on a page. If you want all the post shows on a page, set this parameter’s value is -1;
  • post_status: this is the post status that is published or pending.

For now, you’ve completed about 70% of the task already. Both the admin and Guest Author accounts have the list of submitted posts on the My Account page.

“Your Post” tab of the admin user
“Your Post” tab of the admin user
“Your Post” tab of the Guest Author
“Your Post” tab of the Guest Author

Here is the code for the above steps:

Step 3: Show URL of the Edit Page to Each Post in the List

At this point, Guest Author can not edit the post content although they could see it on the list. The reason is that there is no link to go to the edit page.

With the support from MB Frontend Submission, you will have that link easily. Just add ?rwmb_frontend_field_post_id=123 (id=123 means we are acting on the post which has ID is 123) to the post submission page URL.

For example:

http://yourdomain.com/publish-post/?rwmb_frontend_field_post_id=316

The edit page of a submitted post
The edit page of a submitted post

But even so, everyone can edit whatever post no matter what post’s author and status. It’s so risky and out of control.

To prevent this, we should request users sign in their account (we did it in part 1) and meet some other conditions before going to the edit page.

3.1. Check the Post Information and the Conditions

Use the $_GET[‘param’] function to get the post information (in this case, I got post ID) and assign its value to $post_id:

$post_id = $_GET['rwmb_frontend_field_post_id'];

Then, export name of author and post status by this post_id:

$author_id = get_post_field ('post_author', $post_id);
$post_status = get_post_field ('post_status', $post_id);

Finally, get the ID of the current user:

$current_user = wp_get_current_user();
$user_id = $current_user->ID;

Since we had all the above values, we will check them all whether they fit together:

  • $author_id = $user_id;
  • $post_status = pending;

Guest Author can access the edit page only when both of the above conditions were satisfied. We use the IF ELSE command to check these conditions.

if ($_GET['rwmb_frontend_field_post_id']) {
     echo ‘Yes’;
} else {
     echo ‘No’;
}

Now, if the post is not from the current user or published, Guest Author can not access the edit page.

Guest Author can not edit the submitted post when one of two conditions is not satisfied.
Guest Author can not edit the submitted post when one of the two conditions is not satisfied.

For your quick action, copy these codes to your publish-post.php and do some adjustments if any.

3.2. Show the URL of the Edit Page on the Frontend

Back to the page-account.php file, add a column to the table of the post list. This column is using to show the URL.

<a href="<?php echo get_page_link(268).'?rwmb_frontend_field_post_id='.$post_author->ID; ?>" title="<?php echo $post_author->post_title; ?>">Edit</a>

Pay attention to that 268 is the ID of my post-submission page. You must get the ID of yours and replace this number with it.

Well, My Account page now has all the posts with full information as below:

My Account page of the Guest Author in the frontend when we finished
My Account page when we finished

For your quick reference, this is the source code of my page-account.php file:

Final Words

Now, you’ve completed creating all the features that allow users to sign up a Guest Post account, submit a post, manage, and edit it in the frontend. Exerting Meta Box and its extensions as Meta Box Builder, MB Frontend Submission, and MB User Profile make the execution easier.

We hope that you can do all the above things well then get more useful and suited content for your WordPress website.

Leave a Reply