Programatically create 'Send this user a a private message link' for user pages I know that this question had been asked several times, but it seems like all existing answers only apply to ealier versions of Privatemsg module. I am running 7.x-2.x-dev and I would like to programatically render the "Send this user a private message" link. The PHP code is supposed to be the $block['content'] of a programatically created block that will be displayed on user pages.

Somebody suggested to useif ($account = menu_get_object('user') ) {
      if ($url = privatemsg_get_link(array($account))) {
        $block['content'] = l(t('Send this user a private message'), $url, array('query' => drupal_get_destination()));
      }
}but this is doing absolutely nothing.

I also want that the link is only displayed if the viewed user has enabled the "Enable private messages" option in his/her account settings and if the viewing user has permission to send messages. Any ideas how to achieve this?
Hope this will help you,

In Drupal 7, you can use privatemsg_get_link.


privatemsg_get_link - Returns a link to send message form for a specific users.

  if ($enabled_private_messages) {//check user private message settings
    if ($account = menu_get_object('user')) {// Get user object. You can also use user_load to get user object.
      if (is_object($account) &amp;&amp; privatemsg_user_access('write privatemsg')) {
        if ($url = privatemsg_get_link(array($account))) {
          return l(t('contact user'), $url, array('query' => drupal_get_destination()));
        }
      }
    }
  }