Skip to:
Content
Pages
Categories
Search
Top
Bottom

How to replace a core function


  • Manu-PB
    Participant

    @manu-pb

    Hi,
    I guess that this question has been posted many time, but I cannot manage to replace a core function by my own one. I have seen bp-custom.php + add action + add filter, but I do not understand.

    For instance, I would like to change the function bp_get_message_thread_to() (from bp-message-template.php). What should I put in the bp-custom.php ?

    Thanks for help.

Viewing 4 replies - 1 through 4 (of 4 total)

  • shanebp
    Moderator

    @shanebp

    You can’t overload a core BP function.

    The function you’re referring to has a filter hook.
    So in bp-custom.php, you could do this:

    function manu_bp_message_thread_to( $recipients ) {
    
       // $recipients is a comma-separated string of user links
       // for example: <a href="http://yoursite/members/manu/" title="Manu">Manu</a>
       // adjust as necessary and return
    
       return $recipients;
    }
    add_filter('bp_message_thread_to', 'manu_bp_message_thread_to', 20, 1);

    Manu-PB
    Participant

    @manu-pb

    @shanebp
    Many thanks, I was able to remove the sender from the list of recipients with this custom function :

    function manu_bp_message_thread_to( $recipients ) {
    	global $messages_template;
    	foreach( (array) $messages_template->thread->recipients as $object ) {
    		if ( (int) $object->user_id !== bp_loggedin_user_id() ) {
    			$sender_links[] = bp_core_get_userlink( $object->user_id );
    		}
    	}
    	$recipients = implode( ', ', (array)$sender_links );
    	return $recipients;
    }
    add_filter('bp_message_thread_to', 'manu_bp_message_thread_to', 20, 1);

    Manu-PB
    Participant

    @manu-pb

    Also now, I better understand the logic, thanks.


    shanebp
    Moderator

    @shanebp

    Congrats on the solution.
    However, rather that recreate $recipients, you should be able to do a simple string operation on it. Something like:

    function manu_bp_message_thread_to( $recipients ) {
        $my_link = bp_core_get_userlink( bp_loggedin_user_id() ) . ',';
        $recipients = str_replace($my_link, '', $recipients);
        return $recipients;
    }
    add_filter('bp_message_thread_to', 'manu_bp_message_thread_to', 20, 1);
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How to replace a core function’ is closed to new replies.
Skip to toolbar