Hello Brajesh,
Please ill like to humbly ask for your assistance with a buddypress followers notification plugin im trying to create which is similar to this plugin at https://wordpress.org/plugins/you-have-a-new-message/
What i am trying to achieve is when
1. logged in users receives only 1 new follower then display the text/message (You have a new follower)
2. when logged in users receive more than 1 new followers then display the text/message (You have [follower_count] new followers)
3. Link the text/message to user followers tab with
I want to make buddypress more lightweight by disabling the buddypress notification component and use this for a casual notification for new followers gained
This is the rough work I have
<?php /* Plugin Name: You Have a New (BuddyPress) Follower Description: Widget and Shortcode to notify users about new followers Version: 1.0 Author: Buddydev License: GPL2 */ class New_Followers_Notification_Widget extends WP_Widget { public function __construct() { parent::__construct( 'you-have-a-new-follower-widget', 'New Followers Notification', array('description' => 'Notifies you about new followers') ); } public function widget( $args, $instance ) { $single = ( ! empty( $instance['single'] ) ) ? $instance['single'] : ''; $multiple = ( ! empty( $instance['multiple'] ) ) ? $instance['multiple'] : ''; echo yhanf_get_notice( $single, $multiple ); } public function form( $instance ) { $single = ! empty( $instance['single'] ) ? $instance['single'] : esc_html__( 'You have a new follower', 'you-have-a-new-follower' ); ?> <p> <label for="<?php echo esc_attr( $this->get_field_id( 'single' ) ); ?>"><?php esc_attr_e( 'Single Message:', 'you-have-a-new-follower' ); ?></label> <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'single' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'single' ) ); ?>" type="text" value="<?php echo esc_attr( $single ); ?>"> </p> <?php $multiple = ! empty( $instance['multiple'] ) ? $instance['multiple'] : esc_html__( 'You have %s new followers', 'you-have-a-new-follower' ); ?> <p> <label for="<?php echo esc_attr( $this->get_field_id( 'multiple' ) ); ?>"><?php esc_attr_e( 'Multiple Messages: (%s = follower count)', 'you-have-a-new-follower' ); ?></label> <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'multiple' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'multiple' ) ); ?>" type="text" value="<?php echo esc_attr( $multiple ); ?>"> </p> <?php } public function update( $new_instance, $old_instance ) { $instance = array(); $instance['single'] = ! empty( $new_instance['single'] ) ? strip_tags( $new_instance['single'] ) : ''; $instance['multiple'] = ! empty( $new_instance['multiple'] ) ? strip_tags( $new_instance['multiple'] ) : ''; return $instance; } } function yhanf_register_widget() { register_widget( 'New_Followers_Notification_Widget' ); } add_action( 'widgets_init', 'yhanf_register_widget' ); function yhanf_get_new_followers( $atts, $content = null ) { extract(shortcode_atts(array( "single" => '', "multiple" => '', ), $atts, 'you-have-a-new-follower')); return yhanf_get_notice( $single, $multiple ); } add_shortcode( 'you-have-a-new-follower', 'yhanf_get_new_followers' ); function yhanf_get_notice( $single = '', $multiple = '' ) { if ( !is_user_logged_in() ) { return; } $current_user = wp_get_current_user(); $user_id = $current_user->ID; $args = array( 'following_id' => $user_id, 'orderby' => 'date_followed', 'order' => 'DESC', 'per_page' => 1 ); $followers = BP_Follow::get_followers( $args ); if ( empty( $followers['followers'] ) ) { return; } $count = count( $followers['followers'] ); $out = '<a class="yhanf" href="'. bp_loggedin_user_domain() .'followers/">'; if ( $count === 1 ) { $out .= $single ? $single : __( 'You have a new follower', 'you-have-a-new-follower' ); } else { $out .= sprintf( $multiple ? $multiple : __( 'You have %s new followers', 'you-have-a-new-follower' ), $count ); } $out .= '</a>'; return $out; } ?>
Thanks
You must be logged in to reply to this topic.