Skip to:
Content
Pages
Categories
Search
Top
Bottom

Post title in Message Subject


  • ncovello
    Participant

    @ncovello

    Hello,

    I made a button below each post for sending a message to the post author. But I need also that the subject line fills automatically with the post title and ID.

    This is the code I have now:

    /**
     * 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 = '' ) {
    
    $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' );
    }
    
    }
    
    add_shortcode( 'bp-pm-button', 'buddydev_private_message_button_shortcode' );
    
    /**
     * Get the button for the post author.
     *
     * @return string
     */
    function buddydev_get_author_private_message_button() {
        if ( ! in_the_loop()) {
            return '';
        }
     
        $user_id = get_the_author_meta('ID');
        // can't pm to themselves.
        if ( bp_loggedin_user_id() === $user_id ) {
            return '';
        }
     
        return sprintf('<a class="sg-popup-id-11605" id="private-message-bp" href="%1$s">%2$s</a>', buddydev_get_send_private_message_to_user_url( $user_id ), 'Send Private Message' );
    }
    
    /**
     * Do you want to automatically inject the button on each post?
     *
     * @param string $entry_content entry content.
     *
     * @return string
     */
    function buddydev_inject_private_message_button( $entry_content ) {
        // append at the bottom.
        $entry_content = $entry_content . buddydev_get_author_private_message_button();
        // if you want to only do it on single entry pages(single post etc), you can comment the above line
        // and uncomment the block below
        /* if ( is_singular() ) {
            $entry_content = $entry_content . buddydev_get_author_private_message_button();
        } */
     
        // want to test for a post type, you may use is_singular('post_type_name') instead of is_singular()
        return $entry_content;
    }
    // append to the post entry?
    add_filter( 'the_content', 'buddydev_inject_private_message_button' );

    Thank you!

  • You must be logged in to reply to this topic.
Skip to toolbar