bbp_fix_post_author( array $data = array(), array $postarr = array() )

Fix post author id on post save

Description

When a logged in user changes the status of an anonymous reply or topic, or edits it, the post_author field is set to the logged in user’s id. This function fixes that.

Parameters

$data

(array) (Optional) Post data

Default value: array()

$postarr

(array) (Optional) Original post array (includes post id)

Default value: array()

Return

(array) Data

Source

File: bp-forums/common/functions.php

function bbp_fix_post_author( $data = array(), $postarr = array() ) {

	// Post is not being updated or the post_author is already 0, return
	if ( empty( $postarr['ID'] ) || empty( $data['post_author'] ) )
		return $data;

	// Post is not a topic or reply, return
	if ( !in_array( $data['post_type'], array( bbp_get_topic_post_type(), bbp_get_reply_post_type() ) ) )
		return $data;

	// Is the post by an anonymous user?
	if ( ( bbp_get_topic_post_type() === $data['post_type'] && !bbp_is_topic_anonymous( $postarr['ID'] ) ) ||
	     ( bbp_get_reply_post_type() === $data['post_type'] && !bbp_is_reply_anonymous( $postarr['ID'] ) ) )
		return $data;

	// The post is being updated. It is a topic or a reply and is written by an anonymous user.
	// Set the post_author back to 0
	$data['post_author'] = 0;

	return $data;
}

Changelog

Changelog
Version Description
bbPress (r2734) Introduced.

Questions?

We're always happy to help with code or other questions you might have! Search our developer docs, contact support, or connect with our sales team.