Site icon Anthony Carbon

How to add @mention in Buddypress single media page (or rtMedia single page)?

How to add @mention in Buddypress single media page (or rtMedia single page)? Copy the codes below to your functions.php

add_filter( 'bp_get_activity_content', 'filter_bp_get_activity_content', 10, 1 ); 
function filter_bp_get_activity_content( $content ) {
	global $bp, $wpdb;
	$mention_link = '';
	$mention_dummy = '';
	$at_all = array();
	$at_mention = '';
	$at_space = '';
	$at_text = '';
	$mention_dummy = $content;
	if( $bp->current_component == 'media' && is_numeric($bp->current_action) ){
		$at_mention = explode( "@", $content );
		unset($at_mention[0]);
		for( $xmention = 1; $xmention <= count( $at_mention ); $xmention++ ){
			$at_space = explode(" ", $at_mention[$xmention] );
			if( $at_space[0] ){
				$at_space_2 = explode(",", $at_space[0] );
				if( $at_space_2[0] ){
					$at_all[$xmention] = preg_replace( "/\r|\n/", "", $at_space_2[0] );
				}else{
					$at_all[$xmention] = preg_replace( "/\r|\n/", "", $at_space[0] );
				}
			}else{
				$at_all[$xmention] = preg_replace( "/\r|\n/", "", $at_mention[$xmention] );
			}
		}
		if( count( $at_all ) ){
			for( $ymention = 1; $ymention <= count( $at_all ); $ymention++ ){
				$at_text = '@' . $at_all[$ymention];
				$user_id = $wpdb->get_var( "SELECT ID FROM wp_users WHERE user_nicename = '{$at_all[$ymention]}'" );
				if( $at_all[$ymention] && ! empty( $user_id ) ){
					$mention_link = '<a href="' . get_bloginfo( 'url' ) . '/members/' . $at_all[$ymention] . '/" target="_blank">' . $at_text . '</a>';
					$mention_dummy = str_replace($at_text,$mention_link,$mention_dummy);
				}
			}
			 return $mention_dummy; 
		}
	}
    return $content; 
}

Next is override the media-single.php file in your theme folder (wp-content/themes/my-theme/rtmedia/media) . The file path output must be wp-content/themes/my-theme/rtmedia/media/media-single.php. Open the media-single.php in your text editor and find the and replace with the codes below.

if ( is_user_logged_in() ) {
    ?>
    <form method="post" id="rt_media_comment_form" class="rt_media_comment_form" action="<?php echo esc_url( get_rtmedia_permalink( rtmedia_id() ) ); ?>comment/">
        <textarea style="width:100%;" placeholder="<?php _e( 'Type Comment...', 'buddypress-media' ); ?>" name="comment_content" id="comment_content" class="bp-suggestions"><?php if ( isset( $_GET['r'] ) ) : ?>@<?php echo esc_textarea( $_GET['r'] ); ?> <?php endif; ?></textarea>
        <input type="submit" id="rt_media_comment_submit" class="rt_media_comment_submit" value="<?php _e( 'Comment', 'buddypress-media' ); ?>">
        <?php RTMediaComment::comment_nonce_generator(); ?>
    </form>
    <?php
}

and the last thing is enqueue the @mention JS. Copy this code to your functions.php

add_action( 'wp_enqueue_scripts', 'my_stylesheet_injection' );
function my_stylesheet_injection() {
	global $bp;
	if( $bp->current_component == 'media' && is_numeric($bp->current_action) ){
		wp_enqueue_style( 'bp-mentions-css', get_bloginfo( 'url' ) . '/wp-content/plugins/buddypress/bp-activity/css/mentions.min.css' );
		wp_enqueue_script( 'bp-mentions', get_bloginfo( 'url' ) . '/wp-content/plugins/buddypress/bp-activity/js/mentions.min.js', array( 'jquery' ) );
		wp_enqueue_script( 'jquery-atwho' );
		wp_enqueue_script( 'jquery-caret' );
	}
}
Exit mobile version