Reputation: 68
i'm created a costum post type
i created a button to delete the post from frontend but it's redirecting me to the home page without deleteing the post (the post can be deleted only when i have admin role) i want to allow to the post owner to delete it too
this is my code
<?php if( is_user_logged_in() && is_author(get_current_user_id()) )
echo "<a href='" . wp_nonce_url("/wp-admin/post.php?action=delete&post=$id", 'delete-post_' . $post->ID) . "'><i class=\"fa fa-fw fa-times\"></i>حذف </a>";?>
Upvotes: 1
Views: 1961
Reputation: 1
We solved your problem. The plugin developers at Business Entourage launched a plugin called Delete Post, which allows post author (and only post author) to delete post on the front end. It is great for blog/site owners and works incredibly. Click here to check it out -> Delete Post
Upvotes: 0
Reputation: 68
done,
i did some researche i found something like this
//function to print publish button
function show_publish_button(){
Global $post;
//only print fi admin
echo '<form id="myForm" name="front_end_publish" method="POST" action="">
<input type="hidden" name="pid" id="pid" value="'.$post->ID.'">
<button type="submit" name="submit" id="submit" value="حذف" class="btn" style="margin-left:2px;background:#f5f5f5;"><i class="fa fa-fw fa-times"></i>حذف</button>
</form>';}
then :
//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);
}
if (isset($_POST['pid']) && !empty($_POST['pid'])){
change_post_status((int)$_POST['pid'],'trash');
}
past all code above in functions.php
then in the template file call the function show_publish_button(); to show the delete button
Upvotes: 1
Reputation: 1810
Adding the following code will let you delete post from front end in WordPress.
Method 1:-
<?php
$url = get_bloginfo('url');
if(is_user_logged_in() && is_author(get_current_user_id() && current_user_can('edit_post', $post->ID))){
echo '<a class="delete-post" href="';
echo wp_nonce_url("$url/wp-admin/post.php?action=trash&post=$id", 'delete-post_' . $post->ID);
echo '"><i class=\"fa fa-fw fa-times\"></i>حذف</a>';
}
?>
Method 2:-
<?php
if(is_user_logged_in() && is_author(get_current_user_id())){
echo '<a href="'.get_delete_post_link($post->ID).'"><i class=\"fa fa-fw fa-times\"></i>حذف</a>';
}
?>
Upvotes: 4