Last updated August 26, 2009. Created on October 30, 2005.
Edited by ronald_istos, add1sun, sepeck, Dublin Drupaller. Log in to edit this page.

PLEASE NOTE! These snippets are user submitted. It is impossible to check them all, so please use at your own risk! For users who have setup drupal using an alternate database to the default (MYSQL), please note that the snippets may contain some database queries specific to MYSQL.

Description

This php snippet inserts the SEND A PRIVATE MESSAGE link

Dependencies: privatemsg.module must be installed and enabled.

Usage

  • For use in your user profile page override
  • Copy and paste the code into your user_profile.tpl.php file
  • Change the div class name or the link text to suit.
<?php if (user_access('access private messages') && (isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1)) { ?>
<?php $frommetoprofileuser = arg(1); ?>
<div class="fields">
<?php print l(t('Send a private message to ').$user->name, 'privatemsg/msgto/'. $frommetoprofileuser); ?>
</div>
<?php } ?>

Hide link when on your own page

<?php
if (arg(0) == 'user' && is_numeric(arg(1))) {
   
$node = node_load(arg(1));
    if (
$node->uid == $user->uid) {
      echo ;
    }

    else {
  print
l('Send a message to '.$user->name, 'privatemsg/msgto/'. $user->uid);
 
   }
  }
?>

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

Glowingtree’s picture

just put this in the user_profile.tpl.php file or custom block....

<?php
   
print l('Send me a message',"privatemsg/msgto/".$user->uid);  
?>

$user->uid is already referring to the owner of the user_profile.tpl.php page regardless
of the logged in user - good enough solution for starters, though
users would still have it on their own page - However on top of this you could do a simple " if - else"
statement...

<?php
        
 
if  ($user->uid != ($GLOBALS['user']->uid) )
        
         { print
l('Send This User a Private Message',"privatemsg/msgto/".$user->uid); }

         elseif (
$user->uid == ($GLOBALS['user']->uid) ) 
        
         { print (
'') ; }
?>

and I actually tried it! It actually worked! (Drupal 5.7)

And of course, you would want to put in some DIVs in proper place so you can give it a CSS class.
You could just put a div around the whole thing and used contextual CSS selection to style the A tag. i.e.

" div.msg-me-link a "

however if you want to style it as a box the box would be there wether the link is there or not...
(not sure about syntax for conditional printing of html tags, the " " to give the div the class would
kill the link syntax used in the PHP above, any suggestions?)

Kit_Hally’s picture

please delete my post

lelizondo’s picture

With Drupal 6.x it would be something like this:

<?php
if (user_access('access private messages') && (isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1)) {
   
$frommetoprofileuser = arg(1);
    if (
arg(0) == 'user' && is_numeric(arg(1))) {
            if (
$account->uid == $user->uid) {
                echo
'';
            }
            else {
            print
l(t('Send a message to ').$account->name, 'messages/new/'. $frommetoprofileuser);
         }
        }
    }
?>

Luis

Epifrin’s picture

user_access('write privatemsg') worked for me. Instead of user_access('access private messages').

<?php
if (user_access('write privatemsg') && (isset($user->privatemsg_allow) ? $user->privatemsg_allow : 1)) {
   
$frommetoprofileuser = arg(1);
    if (
arg(0) == 'user' && is_numeric(arg(1))) {
            if (
$account->uid == $user->uid) {
                echo
'';
            }
            else {
            print
l(t('Send a message to ').$account->name, 'messages/new/'. $frommetoprofileuser) ;
         }
        }
    }
?>
mailfox’s picture

Epifrin! Good work!

I'm working with Drupal
my blog http://mailfox.ru

merchadmin’s picture

Unfortunately, this snippet does not work for me with drupal 6. I am at a loss to find a snippet or static link to send a private message to another user.

Strangely enough, the original link on the profile page to send a private message just disappeared. I can't say if it was something with my css or what is the reason, but I would really like to be able to customize my own anyway.

Can anyone tell me what in the world would be the proper way to put a static link to send a private message to the corresponding profile page?

It would be much, much appreciated, I assure you!
Thanks in advance, of course.

lelizondo’s picture

With Drupal 6.x

<?php print $profile[privatemsg_send_new_message]; ?>

Will do the job and it's the right way to do it since you're using the block created by the module. This also prevents the link from showing up when the user it's viewing it's own profile page.

Luis

merchadmin’s picture

Again, this link will not show in a block on any page, let alone profile page.

I have figured out, tho, that it was the My Account Alter module that made the original link fro privatemsg module disappear. When I disable My_Account Alter, then the privatemsg link returns. Unfortunately, without the my_account alter module, the profile page is a mess.

I have been trying to get this link to appear in a block to put in a content header on the profile page but I just can't seem to get it going.

By any chance, do you have any more ideas?

lelizondo’s picture

not really, the only thing I could suggest is to configure every aspect of the profile page... it's not that hard and you could have amazing results.

Luis

mouse77e’s picture

<?php
        if ($profileuser->uid == $loggedinuser->uid) {
          print "&nbsp";
         } else {
           if (user_access('access private messages') && (isset($profileuser->privatemsg_allow) ? $profileuser->privatemsg_allow : 1)) {
              print l(' ' .theme_image(path_to_theme() .
              "/images/profile/user_comment.png", $alt = 'Private Message', $title = 'Private Message', $attributes = NULL, $getsize = TRUE),
               'privatemsg/msgto/'. $profileuser->uid,NULL,NULL,NULL,NULL,TRUE);
            }
        }

Can anyone help please on this topic the above code worked well with Privatemsg 1.8 but broke in 3.0 a bit stuck
Many thanks

I WILL learn this Drupal stuff! [My Dyslexia be damned!]

lelizondo’s picture

did you try using

<?php print $profile[privatemsg_send_new_message]; ?>

instead of the code you're providing?

Luis

derbenito’s picture

Hi,

@lelizondob you code helped me ;).
Thnx
Benitoi

web506’s picture

Hi Luis,

I added this piece of code in a block, then I added in my user profile panel, but it is not showing up. Any ideas why would this happened? or which would be the right way of using it? I have Advance Profile and Panels. Your help will be appreciated, thanks!

Gonzalo Garcia
Freelance Webmaster

mouse77e’s picture

'privatemsg/msgto/'. $profileuser->uid,NULL,NULL,NULL,NULL,TRUE);

becomes

'privatemsg/new/'. $profileuser->uid,NULL,NULL,NULL,NULL,TRUE);

hope that clears things up!

I WILL learn this Drupal stuff! [My Dyslexia be damned!]

jelladean’s picture

When I use this code:

<?php
 
print $profile[privatemsg_send_new_message];
?>

The message is not being printed to the screen. Everything else works fine, as I've printed other text to the screen just fine.

Any ideas?

renyi’s picture

Admin -> Site Configuration -> Private Messages -> "Send private message" link settings

Make sure "Display link on profile pages." is checked.