bbp_insert_forum( array $forum_data = array(), arrap $forum_meta = array() )

A wrapper for wp_insert_post() that also includes the necessary meta values for the forum to function properly.


Description Description


Parameters Parameters

$forum_data

(Optional) Forum post data

Default value: array()

$forum_meta

(Optional) Forum meta data

Default value: array()


Top ↑

Source Source

File: includes/forums/functions.php

function bbp_insert_forum( $forum_data = array(), $forum_meta = array() ) {

	// Forum
	$forum_data = bbp_parse_args( $forum_data, array(
		'post_parent'    => 0, // forum ID
		'post_status'    => bbp_get_public_status_id(),
		'post_type'      => bbp_get_forum_post_type(),
		'post_author'    => bbp_get_current_user_id(),
		'post_password'  => '',
		'post_content'   => '',
		'post_title'     => '',
		'menu_order'     => 0,
		'comment_status' => 'closed'
	), 'insert_forum' );

	// Insert forum
	$forum_id = wp_insert_post( $forum_data, false );

	// Bail if no forum was added
	if ( empty( $forum_id ) ) {
		return false;
	}

	// Forum meta
	$forum_meta = bbp_parse_args( $forum_meta, array(
		'forum_type'           => 'forum',
		'status'               => 'open',
		'reply_count'          => 0,
		'topic_count'          => 0,
		'topic_count_hidden'   => 0,
		'total_reply_count'    => 0,
		'total_topic_count'    => 0,
		'last_topic_id'        => 0,
		'last_reply_id'        => 0,
		'last_active_id'       => 0,
		'last_active_time'     => 0,
		'forum_subforum_count' => 0,
	), 'insert_forum_meta' );

	// Insert forum meta
	foreach ( $forum_meta as $meta_key => $meta_value ) {

		// Prefix if not prefixed
		if ( '_bbp_' !== substr( $meta_key, 0, 5 ) ) {
			$meta_key = '_bbp_' . $meta_key;
		}

		// Update the meta
		update_post_meta( $forum_id, $meta_key, $meta_value );
	}

	// Update the forum and hierarchy
	bbp_update_forum( array(
		'forum_id'    => $forum_id,
		'post_parent' => $forum_data['post_parent']
	) );

	// Maybe make private
	if ( bbp_is_forum_private( $forum_id, false ) ) {
		bbp_privatize_forum( $forum_id );

	// Maybe make hidden
	} elseif ( bbp_is_forum_hidden( $forum_id, false ) ) {
		bbp_hide_forum( $forum_id );

	// Publicize
	} else {
		bbp_publicize_forum( $forum_id );
	}

	/**
	 * Fires after forum has been inserted via `bbp_insert_forum`.
	 *
	 * @since 2.6.0 bbPress (r6036)
	 *
	 * @param int $forum_id The forum id.
	 */
	do_action( 'bbp_insert_forum', (int) $forum_id );

	// Bump the last changed cache
	wp_cache_set( 'last_changed', microtime(), 'bbpress_posts' );

	// Return forum_id
	return $forum_id;
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.