Skip to content

Instantly share code, notes, and snippets.

@benheu
Last active June 6, 2017 01:40
Show Gist options
  • Save benheu/cf9eb925b0e17e6dbd6c to your computer and use it in GitHub Desktop.
Save benheu/cf9eb925b0e17e6dbd6c to your computer and use it in GitHub Desktop.
MailPoet 2.X - Custom shortcodes - How to retrieve normal WP user data?
<?php
/**
*
* @param string $tag_value
* @param int $user_id mailpoet's subscriber id
* @return type
*/
function mailpoet_shortcodes_custom_filter( $tag_value , $user_id ) {
// run this code only for my custom tag [custom:my_custom_tag]
if ($tag_value === 'my_custom_tag') {
// get the wpuser_id of that subscriber
$model_user = WYSIJA::get('user','model');
$subscriber_data = (array)$model_user->getOne(array( 'wpuser_id' ) , array( 'user_id' => $user_id));
// get the wp-user data based on the wp-user id
$wpuser_data = get_userdata( $subscriber_data['wpuser_id'] );
// return the key you want
$replacement = $wpuser_data->last_name;
}
if ($tag_value === 'blog_name') {
$replacement = get_bloginfo('name');
}
return $replacement;
}
// filter registration allowing the two parameters $tag_value and $user_id to be passed
add_filter('wysija_shortcodes', 'mailpoet_shortcodes_custom_filter', 10 , 2);
@jjci
Copy link

jjci commented Jun 6, 2017

I understand the approach, so that's good. However, I am having a problem within the shortcode establishing the '$user_id' value that is being used in the SQL WHERE clause. Does it come into the shortcode as a parameter value, or is it established in some other manner?
Also, is it correct to make a distinction between '$user_id' and 'subscriber id'?
I have tried many things to establish either the $user_id value or the subscriber value within the shortcode, but have not been able to make it work.

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