I’ve found this helpful snippet to make a delete post link on the front-end:
<?php if (current_user_can('edit_post', $post->ID))
echo "<a href='" . wp_nonce_url("/wp-admin/post.php?action=delete&post=$id", 'delete-post_' . $post->ID) . "'>Delete</a>" ?>
What I am wondering if there is a way to modify this code and create a complementary Publish link, which would publish a pending post, from the front end. I’ve tried the following with no luck…
<?php if (current_user_can('edit_post', $post->ID))
echo "<a href='" . wp_nonce_url("/wp-admin/post.php?action=publish&post=$id", 'publish-post_' . $post->ID) . "'>Publish</a>" ?>
If anyone knows if or how a front-end Publish post link would be possible, and if it could be derived from the delete post link, that would be awesomely helpful.
Thank you!
There isn’t, by default, a ‘publish’ link because of the way WordPress handles publishing posts. ‘publish‘ is not consider an action in the same way ‘delete‘ is, instead you save the post, with all its data, including its post status.
To get round this you can create your own action handler. In order to avoid potential clashes with WP or other plug-ins, you should probably prefix your custom action, or at the very least your nonce.
The following code should work for this link (note prefixed action and nonce):
The following function performs some checks, publishes the post and then re-directs you to the published post.
Edit
As mentioned in the comments, slugs are not set when using this method. WordPress does not automatically give draft posts a slug, but only when you click ‘Publish’. The above does not update the slug, just the status. To get round this you can manually set the slug when you create the draft.
Alternatively, you can replace the
wp_publish_post
call with the following (slightly heavier and a bit uglier) code: