How to let Authors delete their own posts?
-
Hi,
we’re running bbpress and our users should be able to delete their own posts.
We’ve blocked the wordpress menu bar on top of the page after a user is logged in.
How is it possible to delete posts in the frontend without switching to the backend of wordpress?Thanks,
Daniel
-
Nobody any idea?
Ridiculous.
insults – yeh that’ll work 🙂
I’ve been doing a lot of research and I am wondering the same thing. The closest I have found was this:
However, that was posted 2 years ago and it seems the code doesn’t work anymore. Ironically, @robin-w it’s your modification xD. Would you mine dropping some insight as to why the code isn’t functioning properly anymore, please. 🙁
Sorry I misspoke. I was unable to run the code through my functions.php file, but it seems to be functioning through a snippet plugin:
My bad.
glad it’s working – I’ll take a look later 🙂
I kicked this code around to improve it and also stop the 404 error when a participant trashes a topic, so latest version is
/*Customize the BBPress roles to allow Participants to trash topics*/ add_filter( 'bbp_get_caps_for_role', 'ST_add_role_caps_filter', 10, 2 ); function ST_add_role_caps_filter( $caps, $role ){ // Only filter for roles we are interested in! if( $role == bbp_get_participant_role() ) { //only change delete topics $caps ['delete_topics']= true ; } return $caps; } /*then only allow participants to trash their own topics*/ add_filter( 'bbp_map_topic_meta_caps', 'ST_tweak_trash_meta_caps', 11, 4 ); function ST_tweak_trash_meta_caps( $caps, $cap, $user_id, $args ){ // apply only to delete_topic if ( $cap == "delete_topic" ){ // Get the post $_post = get_post( $args[0] ); if ( !empty( $_post ) ) { // Get caps for post type object $post_type = get_post_type_object( $_post->post_type ); // Add 'do_not_allow' cap if user is spam or deleted if ( bbp_is_user_inactive( $user_id ) ) { $caps[] = 'do_not_allow'; // Moderators can always edit forum content } elseif ( user_can( $user_id, 'moderate' ) ) { $caps[] = 'moderate'; // User is author so allow edit if not in admin } elseif ( user_can( $user_id, 'participate' ) && ( (int) $user_id === (int) $_post->post_author ) ) { $caps = array(); // Unknown so do not allow } else { $caps[] = 'do_not_allow'; } } } // return the capabilities return $caps; } //then redirect to the forum after trashing topic add_action('bbp_template_redirect', 'ST_trash_topic_check', 8); //check if topic has been trashed by author and show forum if it has function ST_trash_topic_check() { $topic_slug = get_option( '_bbp_topic_slug') ; //quick check if we need to do this function, so bail if not a topic if (strpos($_SERVER['REQUEST_URI'], $topic_slug) == FALSE) return ; $forum_slug = bbp_get_root_slug() ; //if check is set (ie we prefix forums with the forum slug) then part 1 will be forum slug and part 2 will be topic slug, if not part 1 will be topic slug $check = bbp_include_root_slug() ; $link = explode('/',$_SERVER['REQUEST_URI']); //next we need the topic id (post id) of the topic so we need to check if it is a topic and if so, find the topic id if (is_user_logged_in() && $check && $link[1] == $forum_slug && $link[2] == $topic_slug ) { $post = bsp_get_page_by_slug( $link[3], OBJECT, 'topic' ); $login_check=1 ; } elseif (is_user_logged_in() && empty($check) && $link[1] === $topic_slug) { $post = bsp_get_page_by_slug( $link[2], OBJECT, 'topic' ); $login_check=1 ; } //now we need to check if the topic has been trashed by author if (!empty ($login_check) && $post->post_status == 'trash' && $post->post_author == get_current_user_id() ) { $topic_id = $post->ID; //then redirect to the forum we came from $forum = bbp_get_forum_permalink (bbp_get_topic_forum_id ( $topic_id )) ; wp_redirect ($forum) ; exit ; } else return ; }
I’ll add this function into my style pack plugin shortly
- You must be logged in to reply to this topic.