Support » Developing with WordPress » Create “message user” button on non-plugin page

  • taskemann

    (@taskemann)



    Hi! I’m out of solutions, so I hope someone could help me out.

    I’m developing a global scrap trading marketplace where users will have the ability to create marketplace listings. The site also has a community where I run the two plugins wpForo, which is a forum plugin + wpForo Private Messages, which is an add-on plugin to send private messages on the forum. I previously had buddyPress, but decided to switch to wpForo since buddyPress loaded an incredible amount of resources.

    I was wondering if someone could help me with a hook + shortcode for functions.php to allow me to create a “message user” link/button on a non-wpforo marketplace page so users can send messages directly to other members via wpForo Private Messages without having them to leave the other user’s marketplace listing to look them up at the forum to send messages?

    I asked the plugin developer about a solution for this, and he only gave me the function located in the plugin folder.

    Hi Jimmy,

    You can find the privet message function, that can be useful for you in wp-content\plugins\wpforo-private-messages\wpforopm.php the function is add_message_button.

    .

    The function he referred to located in wp-content\plugins\wpforo-private-messages\wpforopm.php is this one:

    public function add_message_button( $current_object ){
                if( isset(WPF()->current_userid) && isset($current_object['userid']) && in_array($current_object['user']['groupid'], $this->allowed_groupids) ){
                    if( WPF()->current_userid == $current_object['userid'] || $current_object['template'] == 'messages' ) return false;
                    if( WPF()->perm->usergroup_can('vwpm') ) {
                        $url = $this->get_conversation_url($current_object['userid']);
                        echo '<div class="wpfpm-message-div"><a href="' . esc_url($url) . '" class="wpfpm-message-button wpf-button">'.wpforo_phrase('Send a Message', false).'</a></div>';
                    }
                }
            }

    When I used buddypress, I got a hook + shortcode made that worked very well that displayed different text on the message button if the user was logged in or not, or if the marketplace listing had the visitor’s ID as author. Perhaps it’s possible to replace/implement some of the code above to into it to make it function?

    Previous buddypress hook + shortcode I used:

    //////////////////////////////////////////////////////////////////
    // 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' );

    Thank you so much.

    Jimmy

Viewing 2 replies - 1 through 2 (of 2 total)
  • romanapeqaix74

    (@romanapeqaix74)

    Well. You need to ask the developer who created the plugin to help you. I usually do that, it cost a little money but it’s worth it.

    By the looks of it you can use the function the plugin author has given you and it’ll do most, if not all of the work for you by generating the correct button for the user to click to send a message.

    You just need to add it in your theme. No shortcode necessary.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Create “message user” button on non-plugin page’ is closed to new replies.