This post was last updated on February 21st, 2020 at 08:43 pm
So this tutorial is basically creating a custom "user" selection meta field under a metabox named "Topic/Reply Author Override" that will be place on the Topic/Reply edit screen.
Anything in this tutorial will work in a theme's functions.php
file.
/** * bbPress Topic and Reply Author Override * https://www.proy.info/bbpress-topic-and-reply-author-change-override/ **/ class bbPress_Topic_and_Reply_Author_Override { /** * Constructor. */ private $match_slugs = array(); public function __construct() { if ( is_admin() ) { add_action( 'load-post.php', array( $this, 'init_metabox' ) ); add_action( 'load-post-new.php', array( $this, 'init_metabox' ) ); $this->match_slugs = array('topic', 'reply'); } } /** * Meta box initialization. */ public function init_metabox() { add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) ); add_action( 'save_post', array( $this, 'save_metabox' ), 10, 2 ); } /** * Adds the meta box. */ public function add_metabox() { add_meta_box( 'bbp_author_metabox', __( 'Topic Author Override', 'textdomain' ), array( $this, 'render_metabox' ), 'topic', 'side', 'high' ); add_meta_box( 'bbp_author_metabox', __( 'Reply Author Override', 'textdomain' ), array( $this, 'render_metabox' ), 'reply', 'side', 'high' ); } /** * Renders the meta box. */ public function render_metabox( $post ) { // Add nonce for security and authentication. wp_nonce_field( 'custom_nonce_action', 'custom_nonce' ); $post_author_override = $post->post_author; if (is_admin() && $this->is_edit_page('new')){ $post_author_override = wp_get_current_user()->ID; } $users = get_users(); $user_select = '<select id="bbp_author_override_metabox" name="post_author_override" class="">'; if($post_author_override ==0){ $_bbp_anonymous_name = get_post_meta($post->ID, '_bbp_anonymous_name', true); $user_select .= ''; } //Leave the admin in the list foreach($users as $user) { //print_r($user); $selected = ($post_author_override == $user->ID)?'selected="selected"':''; $user_select .= '<option value="'.$user->ID.'"'.$selected.'>'.$user->display_name.' ('.$user->user_login.')</option>'; } $user_select .='</select>'; echo $user_select; } /** * Handles saving the meta box. * * @param int $post_id Post ID. * @param WP_Post $post Post object. * @return null */ public function save_metabox( $post_id, $post ) { // Add nonce for security and authentication. $nonce_name = isset( $_POST['custom_nonce'] ) ? $_POST['custom_nonce'] : ''; $nonce_action = 'custom_nonce_action'; // Check if nonce is set. if ( ! isset( $nonce_name ) ) { return; } // Check if nonce is valid. if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) ) { return; } // Check if user has permissions to save data. if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } // Check if not an autosave. if ( wp_is_post_autosave( $post_id ) ) { return; } // Check if not a revision. if ( wp_is_post_revision( $post_id ) ) { return; } // Check to match the slug if(!in_array($post->post_type, $this->match_slugs)){ return; } if ( ! wp_is_post_revision( $post_id ) ){ // unhook this function so it doesn't loop infinitely //remove_action('save_post','change_pos_auth'); if ( isset($_POST['post_author_override']) ) { //$args = array('ID'=>$post_id,'post_author'=>$_POST['post_author_override']); //wp_update_post( $args ); // re-hook this function //add_action('save_post','change_pos_auth'); global $wpdb; $wpdb->query($wpdb->prepare("UPDATE `".$wpdb->prefix . "posts` SET `post_author` = '".$_POST['post_author_override']."' WHERE `ID` = ".$post_id)); } } } /** * is_edit_page * function to check if the current page is a post edit page */ public function is_edit_page($new_edit = null){ global $pagenow; //make sure we are on the backend if (!is_admin()) return false; if($new_edit == "edit") return in_array( $pagenow, array( 'post.php', ) ); elseif($new_edit == "new") //check for new post page return in_array( $pagenow, array( 'post-new.php' ) ); else //check for either new or edit return in_array( $pagenow, array( 'post.php', 'post-new.php' ) ); } } new bbPress_Topic_and_Reply_Author_Override();
I hope this code snippets helped you to create a solution to change the author of a topic/reply in bbPress forum. You may also want to see my other tutorial to create WordPress Login, Logout Menu Link Without a Plugin.
6 Comments
You can post comments in this post.
Thank you, works great.
Clive 7 years ago
Works great for posts of registered users.
Is there also a way to make this work for posts of anonymous users? If I edit a topic created by an anonymous poster on wp-admin/post.php?post=&action=edit, I get the “Topic Author Override” dropdown box and can select a registered author. But after updating the topic page the change is neither shown in the topic list in the backend nor in the forum.
Bernardo 6 years ago
I am having the same problem as Bernardo, often I want to use the guest name, any idea how to fix it ? at the moment I turn off the plugin if I don’t want to change it.
Clive 5 years ago
Sorry it is not the same as Bernardo and seems to have started with the bbpress update, if a guest writes a post and I edit that post before approving it as I normally add tag lines to the post, it now removes the guest name and email. I have to disable the plugin if I do not want to change the username.
Clive 5 years ago
Just wanted to change a reply Author from guest to a user and it will not change it, it just makes it anonymous, I had to go into database to change it.
Could you please say if you are not longer supporting this plugin.
Clive 5 years ago
Works great. Thanks alot!
Alex 5 years ago
Leave A Reply