Skip to content

Instantly share code, notes, and snippets.

@stephenh1988
Created June 13, 2012 13:53
Show Gist options
  • Save stephenh1988/2924207 to your computer and use it in GitHub Desktop.
Save stephenh1988/2924207 to your computer and use it in GitHub Desktop.
Provides a function to add a 'publish' link to posts for use on the front end
<?php
/*
* Provides a function to add a 'publish' link to posts.
* Use sh_frontend_publish() to display the link
*
*/
function sh_frontend_publish(){
$post_id = get_the_ID();
if( 'draft' == get_post_status($post_id) && current_user_can('edit_post',$post_id) && current_user_can('publish_posts') ){
$url = add_query_arg(array(
'action'=>'sh_frontend_publish',
'post'=>$post_id
),
home_url());
$url = wp_nonce_url($url, 'sh_frontend_publish_'.$post_id);
echo "<a href='{$url}'>Publish</a>";
}
}
//Run the publish function only if the action variable is correct.
if(isset($_GET['action']) && $_GET['action']=='sh_frontend_publish'){
add_action('init','sh_frontend');
}
function sh_frontend_publish_draft(){
//Get the post's ID.
$post_id = (isset($_REQUEST['post']) ? (int) $_REQUEST['post'] : 0);
//No post? Oh well..
if(empty($post_id))
return;
check_admin_referer('sh_frontend_publish_'.$post_id, '_wpnonce');
//Check user permissions
if (!current_user_can('publish_posts') || !current_user_can('edit_post', $post_id) )
wp_die("You can't do that");
//Publish post
$post = get_post($post_id,ARRAY_A);
$post['post_status'] ='publish';
wp_update_post($post)
//Redirect to published post
$redirect = get_permalink($post_id);
wp_redirect($redirect);
exit;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment