Mailster has a powerful tag engine built in where you can dynamically populate your newsletter campaigns. Dynamic Custom Post Types work similarly to regular post types but are not stored in your database but generate content on demand.
Let’s imagine you like to display the third latest post title of your site in a tag. You would do this with a dynamic tag like so:
But what if you like to display some special content that is not certainly a post? Some use cases could be
- The most popular posts on your site.
- The items in a cart on an e-commerce site.
- The posts contain certain words.
As you can see the possibilities are endless so it’s up to you what you need.
This is only available in WordPress 4.6 and above!
Simple Example
To put this opportunity in a simple example we are going to create a dynamic custom post type where we create a dynamic tag for users.
The function you need to use to register the tag is similar to the official register_post_type
method:
mailster_register_dynamic_post_type( $dcpt, $arguments, $callback );
The first parameter $dcpt
accepts a string (the name of the custom post type). The second accepts the same argument as the register_post_type
method and can be found here. You can also just pass the display name of your DCPT as not all options are needed.
mailster_register_dynamic_post_type( 'my_users', 'My Users', 'my_users_callback');
function my_users_callback( $offset, $campaign_id, $subscriber_id, $term_ids, $args, $random ) {
$user_query = new WP_User_Query( array( 'number' => 1, 'offset' => absint( $offset ) ) );
$users = $user_query->get_results();
if ( empty( $users ) ) {
// return null if no user has been found
return null;
}
// get first user of the array
$user = $users[0];
// return array or object
return array(
'post_title' => $user->display_name,
'post_link' => get_author_posts_url( $user->ID ),
);
}
The third parameter of the mailster_register_dynamic_post_type
function accepts a $callback
which should return the actual post. Internally it will get converted to an WP_Post
object if you return data as an object or an array.
If you return null
Mailster will recognize the used tag as not existing and will not display any output.
Output
Once you add the code you find a new item in the drop-down if you choose dynamic insertion mode:
Since we only defined the post_title and the post_link the excerpt will be empty but you can of course define that as well. Find all tags you can use here.
You can also return custom properties as a string:
// return array or object
return array(
'zodiac_sign' => get_users_zodiac_sign( $user->ID ), //custom method
);
With the above example, you can use them {my_users_zodiac_sign:-1}
to trigger the custom get_users_zodiac_sign method.
This is just a basic example of what you can do with Dynamic Custom Post types in Mailster. Possibilities are of course almost endless.