Posted on

How To Create A Custom BuddyPress Component In A Plugin

BP_Component

BP_Component is the main class for your plugin to initiate into BuddyPress. You need to extend BP_Component Class to create custom components. Well, the core components extend this class for each core component (members, activity, groups etc.)


class BP_Members_Component extends BP_Component {

   Here you can find details codex how BuddyPress extended the Member Component

   https://codex.buddypress.org/developer/bp_component/

}

 

In this post I will explain how I made BuddyPress Question Answer: Q&A System For BuddyPress Sites.
This plugin integrates BuddyPress and DW Question & Answer.

I will not go into details of the plugin, but will explain the process of adding custom Component only.


// init on buddypress load.
function init_question_answer_component(){
    new BP_Question_Answer_Component();
}

add_action( 'bp_loaded', 'init_question_answer_component', 40 );

Now let’s initiate when BuddyPress is loaded.Here we have used bp_loaded hook so that the init_question_answer_component will be called when BuddyPress is active.

Now we need to write the class BP_Question_Answer_Component. We will extend the BP_Component.
class BP_Question_Answer_Component extends BP_Component {…..}

Let me explain each function.


/**
 * BuddyPress BP_Question_Answer_Component Loader
 *
 * @package BuddyPress
 * @subpackage SettingsLoader
 */

class BP_Question_Answer_Component extends BP_Component {

	/**
	 * Start the Question_Answer component creation process
	 */
	public function __construct() {
		parent::start(
			'question-answer',
			__( 'Question Answer', 'buddypress' ),
			plugin_dir_path( __FILE__ ),
			array(
				'adminbar_myaccount_order' => 100
			)
		);
	}

	/**
	 * Include files
	 *
	 * @global BuddyPress $bp The one true BuddyPress instance
	 */
	public function includes( $includes = array() ) {
		parent::includes( array(
			'actions',
			'screens',
			'template',
			'functions',
		) );
	}

	/**
	 * Setup globals
	 *
	 * The BP_QUESTION_ANSWER_SLUG constant is deprecated, and only used here for
	 * backwards compatibility.
	 *
	 * @since BuddyPress (1.5)
	 */
	public function setup_globals( $args = array() ) {

		// Define a slug, if necessary
		if ( !defined( 'BP_QUESTION_ANSWER_SLUG' ) )
			define( 'BP_QUESTION_ANSWER_SLUG', $this->id );

		// All globals for settings component.
		parent::setup_globals( array(
			'slug'          => BP_QUESTION_ANSWER_SLUG,
			'has_directory' => false,
		) );
	}

	/**
	 * Set up navigation.
	 */
	public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
		
		if ( ! class_exists( 'DWQA_Settings' ) )
			return;

		// Add the Question & Answer navigation item
		$main_nav = array(
			'name'                    => apply_filters( 'bp_question_answer_link_label', __( 'Question & Answer', 'buddypress-question-answer' ) ),
			'slug'                    => $this->slug,
			'position'                => 100,
			'show_for_displayed_user' => bp_core_can_edit_settings(),
			'screen_function'         => 'bp_question_answer_screen_question_answer_dashboard',
			'default_subnav_slug'     => 'question-answer-dashboard'
		);

		// Determine user to use
		if ( bp_displayed_user_domain() ) {
			$user_domain = bp_displayed_user_domain();
		} elseif ( bp_loggedin_user_domain() ) {
			$user_domain = bp_loggedin_user_domain();
		} else {
			return;
		}

		$question_answer_link = trailingslashit( $user_domain . $this->slug );

		// Add Question & Answer Dashboard nav item.
		$sub_nav[] = array(
			'name'            => apply_filters( 'bp_question_answer_dashboard_link_label', __( 'Question & Answer Dashboard', 'buddypress-question-answer' ) ),
			'slug'            => 'question-answer-dashboard',
			'parent_url'      => $question_answer_link,
			'parent_slug'     => $this->slug,
			'screen_function' => 'bp_question_answer_screen_question_answer_dashboard',
			'position'        => 10,
			'user_has_access' => bp_core_can_edit_settings()
		);
		
		// Add Question & Answer nav item
		if ( get_current_user_id() == bp_displayed_user_id() ) {
			$sub_nav[] = array(
				'name'            => apply_filters( 'bp_question_answer_link_label', __( 'Question & Answer', 'buddypress-question-answer' ) ),
				'slug'            => 'questions',
				'parent_url'      => $question_answer_link,
				'parent_slug'     => $this->slug,
				'screen_function' => 'bp_question_answer_screen_question_answer',
				'position'        => 20,
				'user_has_access' => bp_core_can_edit_settings()
			);

			// Add Ask Question nav item.
			$sub_nav[] = array(
				'name'            => apply_filters( 'bp_question_answer_link_label', __( 'Ask Question', 'buddypress-question-answer' ) ),
				'slug'            => 'ask-question',
				'parent_url'      => $question_answer_link,
				'parent_slug'     => $this->slug,
				'screen_function' => 'bp_question_answer_screen_ask_question',
				'position'        => 70,
				'user_has_access' => bp_core_can_edit_settings()
			);
		}
		
		parent::setup_nav( $main_nav, $sub_nav );
	}

	/**
	 * Set up the Toolbar
	 */
	public function setup_admin_bar( $wp_admin_nav = array() ) {
		
		if ( ! class_exists( 'DWQA_Settings' ) )
			return;

		// The instance
		$bp = buddypress();

		// Menus for logged in user
		if ( is_user_logged_in() ) {

			// Setup the logged in user variables
			$user_domain   = bp_loggedin_user_domain();
			$question_answer_link = trailingslashit( $user_domain . $this->slug );

			// Add main Question & Answer menu
			$wp_admin_nav[] = array(
				'parent' => $bp->my_account_menu_id,
				'id'     => 'my-account-' . $this->id,
				'title'  => __( 'Question Answer', 'buddypress-question-answer' ),
				'href'   => trailingslashit( $question_answer_link )
			);
			
			// Question & Answer Dashboard
			$wp_admin_nav[] = array(
				'parent' => 'my-account-' . $this->id,
				'id'     => 'my-account-' . $this->id . '-question-answer-dashboard',
				'title'  => apply_filters( 'bp_question_answer_dashboard_link_label', __( 'Question & Answer Dashboard', 'buddypress-question-answer' ) ),
				'href'   => trailingslashit( $question_answer_link . 'question-answer-dashboard' )
			);
			
			// Question & Answer
			$wp_admin_nav[] = array(
				'parent' => 'my-account-' . $this->id,
				'id'     => 'my-account-' . $this->id . '-questions',
				'title'  => apply_filters( 'bp_question_answer_link_label', __( 'Question & Answer', 'buddypress-question-answer' ) ),
				'href'   => trailingslashit( $question_answer_link . 'questions' )
			);
			
			
			// Ask Question
			$wp_admin_nav[] = array(
				'parent' => 'my-account-' . $this->id,
				'id'     => 'my-account-' . $this->id . '-ask-question',
				'title'  => apply_filters( 'bp_question_answer_link_label', __( 'Ask Question', 'buddypress-question-answer' ) ),
				'href'   => trailingslashit( $question_answer_link . 'ask-question' )
			);
			
		}

		parent::setup_admin_bar( $wp_admin_nav );
	}
}

Now for each action we need to have our own screen function bp_question_answer_screen_question_answer


/**
 * Show the Question Answer Questions template 
 * 
 * @since BuddyPress Question Answer (1.0.0) 
 */ 
function bp_question_answer_screen_question_answer() {

if ( bp_action_variables() ) { 
   bp_do_404(); return; 
} 
   bp_core_load_template( apply_filters( 'bp_question_answer_screen_question_answer', 'members/single/plugins' ) );
   add_action('bp_template_content','load_question_answer_template');
} 
function load_question_answer_template() {
   // include file or echo do_shortcode echo "your output"; 

} 

Leave a Reply

Your email address will not be published. Required fields are marked *