Passing email token as parameter to shortcode

Hello,

I would like to customize my email templates a little more than just displaying basic token.

In the message template for instance, %recipient.id% returns the user id.
So I created a little shortcode, but I am struggling with this :


//usage : [user_type_text id="123"]
//        [user_type_text id="%recipient.id% "]
function user_type_text_func( $atts ) {
	$user_info = get_userdata( intval($atts['id']) );
    return '{'.$user_info->user_email.'}{' . $atts['id'].'}' ;
}
add_shortcode( 'user_type_text', 'user_type_text_func' );

and is the message template :
[user_type_text id="%recipient.id%" ]

Problem is : $atts[‘id’] is retrieved and is displayed, but never being passed to the get_userdata() function.
The return line displays : {}{114} and if 114 is hard-coded, it DOES return a user email.
The email is just an example here. I know I don’t need to retrieve it to send the email .

I spent some much time of this, trying to cast the variable (as here), using no quote, single quote, double quote for the param, using an intermediate variable to store the param. Also tried to pass the param as content of the shortcode : [myshortcode]someID[/myshortcode]
I also tried to do a simple sum as a return value
return 2+intval($atts['id']); //always returns 2

So I am wondering if there is a limitation with this special tokens.

Please advise.

1 Like

Hi,

You can try to error_log(print_r($user_info, true)) to check why it’s empty, and if it’s an object at all. I recommend accessing properties via the token though, you can access the same fields as with the ID:

%recipient.email%

Or any custom attribute:

%recipient.attribute_name_here%

Hope this helps

1 Like

Thanks @ihor, you’re right, it seems to be passed as an array.
But still, can’t make heads or tails out of it.
Giving up.

1 Like

Thank you @ihor !

This alternative solution worked for me.

No need for a shortcode, let’s create my own token instead.
Basically, I have a custom attribute called ‘type’ (vendor / customer), and I wanted to display a different message according to this attribute.

add_filter(
	'hivepress/v1/emails/message_send',
	function( $email ) {
		$email['tokens']['custom_msg_by_type'] = "";		
		//recipient is known, it's a hivepress User, let's get user meta from its ID. 
		$user_type = get_user_meta($email['tokens']['recipient']->get_id(), 'hp_type', true);		
		if($user_type==115){//freelance
			$email['tokens']['custom_msg_by_type'] = "Hello Freelance!"; //or whatever
		}else if($user_type==116){//customer 
			$email['tokens']['custom_msg_by_type'] = "Hello Customer!";			
		}
		return $email;
	}
);

in message email template : use %custom_msg_by_type% to display the message.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.