Skip to:
Content
Pages
Categories
Search
Top
Bottom

Create private message button.


  • taskemann
    Participant

    @taskemann

    Hi. I have buddypress + woocommerce + WC marketplace installed on my site running the flatsome theme. My site is a multi-vendor marketplace and I would really like to have a button or something on the single product’s page where logged in users easily can send a message to the product’s particular vendor through Buddypress’ PM system. Could someone please help me creating a shortcode that allows me to do this?

    I’ve tried to search the forum but to no help.

    Thanks.

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

  • Venutius
    Moderator

    @venutius

    Hi there,

    I have a plugin called BP Profile Shortcodes Extra. I’ve added a shortcode to provide you with a configurable Private Message link [bpps_profile_private_message_link] you can use the user_id=”” setting to determine a specific user to private message. Let me know what you think.


    taskemann
    Participant

    @taskemann

    Hi Venutis! Thank you for the tip 🙂

    But I found this code online created by a fellower named Brajesh Singh that worked very well. I just pasted it into my child theme’s functions.php

    //////////////////////////////////////////////////////////////////
    // BuddyPress send private message button
    //////////////////////////////////////////////////////////////////
    
    /**
     * Get a link to send PM to the given User.
     *
     * @param int $user_id user id.
     *
     * @return string
     */
    function buddydev_get_send_private_message_to_user_url( $user_id ) {
        return wp_nonce_url( bp_loggedin_user_domain() . bp_get_messages_slug() . '/compose/?r=' . bp_core_get_username( $user_id ) );
    }
    
    /**
     * Shortcode [bp-pm-button username=optional_some_user_name]
     *
     * @param array $atts shortcode attributes.
     * @param string $content content.
     *
     * @return string
     */
    function buddydev_private_message_button_shortcode( $atts, $content = '' ) {
        // User is not logged in.
        if ( ! is_user_logged_in() ) {
            return '';
        }
     
        $atts = shortcode_atts( array(
            'user_id'   => '',
            'username'  => '',
            'label'     => 'Send Private Message',
        ), $atts );
     
        $user_id = absint( $atts['user_id'] );
        $user_login = $atts['username'];
     
        // if the username is given, override the user id.
        if ( $user_login ) {
            $user = get_user_by( 'login', $user_login );
            if ( ! $user ) {
                return '';
            }
            $user_id = $user->ID;
        }
     
        if ( ! $user_id ) {
            if ( ! in_the_loop() ) {
                return '';
            }
     
            $user_id = get_the_author_meta('ID' );
        }
        // do not show the PM button for the user, if it is aimed at them.
        if ( bp_loggedin_user_id() === $user_id ) {
            return '';
        }
     
        // if we are here, generate the button.
        $button = sprintf('<a href="%1$s">%2$s</a>', buddydev_get_send_private_message_to_user_url( $user_id ), $atts['label'] );
     
        return $button . $content;
    }
     
    add_shortcode( 'bp-pm-button', 'buddydev_private_message_button_shortcode' );

    Venutius
    Moderator

    @venutius

    That’s great, glad you found a solution.

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
Skip to toolbar