Skip to content

Instantly share code, notes, and snippets.

@benheu
Last active June 6, 2017 01:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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);
@walrus-hunter
Copy link

Where should we put this code? under functions.php of theme?

@walrus-hunter
Copy link

I dont retrieve the last name, is it compatible with the latest wysija mail poet plugin? thank you

@fmarzocca
Copy link

I am adding several posts to my MailPoet newsletter and from a shortcode I need to gather the specific post content. Is there any WYSIJA:get for this?
Thanks

@tubiz
Copy link

tubiz commented Feb 14, 2015

@fmarzocca what do you mean by gather the specific post content.

@BendaCoding
Copy link

thanks for the example!
however, im struggling to get the users custom fields data of mailpoet.
something like $wpuser_data->field_2 wont work.
any idea?

@oheinrich
Copy link

Did anybody managed to read out the custom fields? I searched and tried a lot but couldn't find any solution.
Thanks in advance for any hint!

@oheinrich
Copy link

I have been trying a lot, but still no success. Any help would be great? Am I the only one customizing Mailpoet? ;-)

@oheinrich
Copy link

oheinrich commented Jun 13, 2016

I solved the problem with a workaround:
I read out the data directly from the MySQL-database without using the Mailpoet-API:

// Make variable global to get access to DB
global $wpdb; 

// Get ZIP of subscribers:
// cf_2 is the name of the custom field -> replace by the name of your field
foreach( $wpdb->get_results("SELECT cf_2 FROM wp_wysija_user WHERE user_id ='".$user_id."'") as $key => $row) {
    $zip = $row->cf_2;
}

I hope this helps other users of Mailpoet.

@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