Is it possible to take the blue publish button in back end of the wordpress and place it in the frontend near the post in my index.php. I need a way to publish the pending article without going to dashboard and only admin can see the publish button?
<?php
$args=array(
'post_type' => 'post',
'post_status' => 'pending',
'order' => 'ASC',
'caller_get_posts' =>1,
'paged' =>$paged,
);
Thank You all in advance for help 😀
First create a function that will print the publish button :
//function to print publish button
function show_publish_button(){
Global $post;
//only print fi admin
if (current_user_can('manage_options')){
echo '<form name="front_end_publish" method="POST" action="">
<input type="hidden" name="pid" id="pid" value="'.$post->ID.'">
<input type="hidden" name="FE_PUBLISH" id="FE_PUBLISH" value="FE_PUBLISH">
<input type="submit" name="submit" id="submit" value="Publish">
</form>';
}
}
Next create a function to change the post status:
//function to update post status
function change_post_status($post_id,$status){
$current_post = get_post( $post_id, 'ARRAY_A' );
$current_post['post_status'] = $status;
wp_update_post($current_post);
}
Then make sure to catch the submit of the button:
if (isset($_POST['FE_PUBLISH']) && $_POST['FE_PUBLISH'] == 'FE_PUBLISH'){
if (isset($_POST['pid']) && !empty($_POST['pid'])){
change_post_status((int)$_POST['pid'],'publish');
}
}
Now all of the above code can go in your theme’s functions.php file and all you have to do is add show_publish_button();
in your loop of pending posts after each post.