Send HTML message to newly registered user automatically

Hello,

I am trying to add a welcome message automatically as user registers, based on this snippet. Thanks Yehven, by the way !

However, it seems HTML (even basic like link) is sanitized. And I would like to provide some links.

Reading the documentation, I saw it was possible to retrieve the message ID, once it has been created/saved :

So I want to retrieve the message ID, and update the comment content value with some HTML. However, I can’t. I get an error message saying that get_id() method is not recognized (even though the original message is saved correctly in DB).

What’s wrong with my code ?

add_action(
	'hivepress/v1/models/user/register',
	function($user_id, $values){
		$admin_email = get_option('admin_email');
		
		if(!$admin_email){
			return;
		}
		
		$user = \HivePress\Models\User::query()->filter(['email' => $admin_email])->get_first();
		
		if(!$user){
			return;
		}
		//add message. 
		$admin_welcome_message = ( new \HivePress\Models\Message() )->fill(
			[
				'sender'               => $user->get_id(),
				'sender__display_name' => $user->get_display_name(),
				'sender__email'        => $user->get_email(),
				'recipient'            => $user_id,
				'read'                 => 0,
				'text' => 'Some basic welcome message, as it get sanitized',
			]
		)->save();

        //error happens here. 
		$admin_msg_id = $admin_welcome_message->get_id();

		$new_content = "This is the <b>updated</b><br> <a href='https://google.com/'>comment</a> content."; 

		$comment_data = array(
			'comment_ID'      => $admin_msg_id,
			'comment_content' => $new_content,
		);
		wp_update_comment($comment_data); 
				
	},
	1000,
	2
);

Hi,

Please try to call save() separately, after filling it with values. This issue occurs because save() returns true or false (can be used for checking if the model is successfully saved in the DB) instead of the model object.

$admin_welcome_message->save();

Hope this helps

Thank you Boss ! It works like a charm !! :ok_hand:

Here’s the updated code, for those interested :

//add a default HTML message as user registers
add_action(
	'hivepress/v1/models/user/register',
	function($user_id, $values){
		$admin_email = get_option('admin_email');
		
		if(!$admin_email){
			return;
		}
		
		$user = \HivePress\Models\User::query()->filter(['email' => $admin_email])->get_first();
		
		if(!$user){
			return;
		}
		//add message. 
		$admin_welcome_message = ( new \HivePress\Models\Message() )->fill(
			[
				'sender'               => $user->get_id(),
				'sender__display_name' => $user->get_display_name(),
				'sender__email'        => $user->get_email(),
				'recipient'            => $user_id,
				'read'                 => 0,
				'text' => 'Plain text message', //will be replaced soon !
			]
		);
			
		$admin_welcome_message->save();	

		$new_content = "This is the <b>updated</b><br> <a href='https://google.com/'>HTML</a> content."; //much better 
		
		$comment_data = array(
			'comment_ID'      => $admin_welcome_message->get_id(),
			'comment_content' => $new_content,
		);
		wp_update_comment($comment_data); 				
	},
	1000,
	2
);

You will notice <br> seems to be sanitized (stripped off), so if you want the same behaviour, you just need to add carriage returns, same for paragraphs (double carriage returns).

$new_content = "I will jump here
to next line.

And here is a paragraph.

And another one.
";

2 Likes

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