bbPress Topic and Reply Author Change / Override

bbPress Topic and Reply Author Override 800x300 - bbPress Topic and Reply Author Change / Override

Do you want to change the author of a topic/reply in your bbPress Forum? Sometimes, you may want to get rid of some forum user or you may need to display a different user name, then the person who added the topic or reply a topic in Forum. In general, you need to copy and paste the entire post with a different user account because there is no easy way to change the author of a topic or reply in bbPress. In this post, you will find a perfect solution to easily change the author of a topic/reply in the bbPress forum. You can change the current author to any user in your bbPress forum.

Topic and Reply Author Override metabox on the right side - bbPress Topic and Reply Author Change / Override

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 = get_post_meta($post->ID, 'post_author_override', true);
        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="">';
        //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;
        }
        $meta_box_text_value = "";
        if(isset($_POST["post_author_override"])) {
            $meta_box_text_value = $_POST["post_author_override"];
        }
        update_post_meta($post_id, "post_author_override", $meta_box_text_value);
    }
    /**
    * 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.

About This Author

My name is Parameshwar Roy (P. Roy), and I am a web developer. This is my personal blog to record my own thoughts. Though I am not a natural writer, I love to share my experiences. Hope my experiences will be useful to you…read more about me

1 Comment

You can post comments in this post.


  • Thank you, works great.

    Clive 2 hours ago Reply


Leave A Reply