Skip to content

Instantly share code, notes, and snippets.

@bentasm1
Created March 21, 2014 18:23
Show Gist options
  • Save bentasm1/9692548 to your computer and use it in GitHub Desktop.
Save bentasm1/9692548 to your computer and use it in GitHub Desktop.
BuddyPress Private Messaging with Matt Gates Product Vendors for WooCommerce
This will allow your customers to send your vendors private messages from the single product view. It will not reveal their email. It is free and requires no purchase.
It requires BuddyPress with the component "Private Messaging" to be enabled in order to work. You dont need any of the other components enabled, just this one. And this component comes with BuddyPress for free, as well.
-----
*Note* I'm an amateur. I'm not a coding pro. This code was hacked and put together from a dozen different websites offering snippets that eventually brought me the solution. If it doesnt work for you, I really probably have no idea why. As of writing, this works with mgates Product Vendor 1.5.0.2, WooCommerce 2.1.5, and WordPress 3.8.1.
There are 2 files to edit. Your themes functions.php (first). Next, is creating a file called bp-custom.php in your plugins directory (NOT THE BUDDYPRESS DIRECTORY, IN THE PLUGIN DIRECTORY WHICH LOOKS OUT OF PLACE BUT THATS HOW BUDDYPRESS WORKS ./wp-content/plugins/bp-custom.php). Last but not least, you need to copy the woocommerce single-product/tabs/description.php into your theme folder (./themes/yourtheme/woocommerce/single-product/tabs/description.php) and edit that to add one line of code.
If you can clean up this code so it works better, that's great. Please do so and let me know! :)
Remember, I'm just a guy who doesnt know what he's doing but tries his hardest.
--------- begin THIS GOES IN YOUR THEMES functions.php ---------
/* BuddyPress PM mod */
if ( bp_is_active( 'messages' ) ){
add_action( 'bp_directory_members_actions', 'hibuddy_send_private_message_button', 30 );//experiment with the last value to change position
}
/* BuddyPress change members directory to only show vendors */
add_action('bp_ajax_querystring','bpdev_exclude_users',20,2);
function bpdev_exclude_users($qs=false,$object=false){
//list of users to exclude
$excluded_user=join(',',bpdev_get_subscriber_user_ids());//comma separated ids of users whom you want to exclude
if($object!='members')//hide for members only
return $qs;
$args=wp_parse_args($qs);
//check if we are searching for friends list etc?, do not exclude in this case
if(!empty($args['user_id']))
return $qs;
if(!empty($args['exclude']))
$args['exclude']=$args['exclude'].','.$excluded_user;
else
$args['exclude']=$excluded_user;
$qs=build_query($args);
return $qs;
}
function bpdev_get_subscriber_user_ids(){
$users=array();
$subscribers= get_users( array( 'role' => 'customer' ) );
if(!empty($subscribers)){
foreach((array)$subscribers as $subscriber)
$users[]=$subscriber->ID;
}
return $users;
}
--------- end THIS GOES IN YOUR THEMES functions.php ---------
---- begin Make a file called /wp-content/plugins/bp-custom.php <-- YES, IN THE PLUGINS DIRECTORY THIS ISNT A TYPO ----
<?php
/* Buddypress Private Messaging Mod */
function hibuddy_get_context_user_id($user_id=false){
if ( bp_is_my_profile() || !is_user_logged_in() )
return false;
if( !$user_id )
$user_id = bp_get_member_user_id();//for members loop
if( !$user_id && bp_is_user() ) //for user profile
$user_id = bp_displayed_user_id();
return apply_filters( 'hibuddy_get_context_user_id', $user_id );
}
function hibuddy_get_send_private_message_url() {
$user_id = hibuddy_get_context_user_id();
if( !$user_id || $user_id == bp_loggedin_user_id() )
return;
if ( bp_is_my_profile() || !is_user_logged_in() )
return false;
return apply_filters( 'hibuddy_get_send_private_message_url', wp_nonce_url( bp_loggedin_user_domain() . bp_get_messages_slug() . '/compose/?r=' . bp_core_get_username( $user_id ) ) );
}
function hibuddy_get_send_private_message_button() {
//get the user id to whom we are sending the message
$user_id = hibuddy_get_context_user_id();
$defaults = array(
'id' => 'private_message-'.$user_id,
'component' => 'messages',
'must_be_logged_in' => true,
'block_self' => true,
'wrapper_id' => 'send-private-message-'.$user_id,
'wrapper_class' =>'send-private-message',
'link_href' => hibuddy_get_send_private_message_url(),
'link_title' => __( 'Ask the model a question about this item!', 'buddypress' ),
'link_text' => __( 'Question? Send me a Private Message', 'buddypress' ),
'link_class' => 'send-message',
);
$btn = bp_get_button( $defaults );
return apply_filters( 'hibuddy_get_send_private_message_button', $btn );
}
function hibuddy_send_private_message_button() {
echo hibuddy_get_send_private_message_button();
}
add_filter('hibuddy_get_context_user_id', 'bp_custom_get_author_id');
function bp_custom_get_author_id( $user_id ){
if(in_the_loop())
return get_the_author_ID();
return $user_id;
}
?>
---- end Make a file called /wp-content/plugins/bp-custom.php <-- YES, IN THE PLUGINS DIRECTORY THIS ISNT A TYPO ----
---- begin In ./themes/yourtheme/woocommerce/single-product/tabs/description.php - Open it, and under the line that says: <?php the_content(); ?>.... add this line UNDER it. This line calls the code you entered. I put it under the product description. You can put it anywhere you want, of course. But I know this way works. ;-) ----
<?php hibuddy_send_private_message_button(); ?>
---- end In ./themes/yourtheme/woocommerce/single-product/tabs/description.php - Open it, and under the line that says: <?php the_content(); ?>.... add this line UNDER it. This line calls the code you entered. I put it under the product description. You can put it anywhere you want, of course. But I know this way works. ;-) ----
@bentasm1
Copy link
Author

This is a very outdated Gist. Only kept here for archival purposes. No longer needed.

@alexgasol
Copy link

Hi, No longer needed means you made another solution or there is something new on wp for the same

@arirangz
Copy link

arirangz commented Jul 5, 2016

Hi,
I have the same question :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment