Quantcast
Channel: Code snippets – Jetpack
Viewing all articles
Browse latest Browse all 20

Hook of the Month: taking control of email subscriptions

$
0
0

Are you familiar with Jetpack Subscriptions? This month, we’ll use filters to customize this module, and control which posts should be sent to your subscribers.

Jetpack’s Subscriptions module sends an email to your subscribers every time you publish a new post. That’s practical, but sometimes you may not want to bother your subscribers for something that might not be interesting to them.

Let’s discover three filters that will allow you to customize the default behavior. You can pick which posts should be sent to your subscribers, and which ones should be ignored.

Disable Subscriptions for a specific post

Jetpack Subscriptions Options

If that filter is set to true, a checkbox will appear in the post editor, allowing you to choose whether or not the post should be sent to subscribers:

add_filter( 'jetpack_allow_per_post_subscriptions', '__return_true' );

Disable Subscriptions if it belongs to a specific category

Sometimes, you want to exclude a series of posts from Jetpack Subscriptions. You can do so thanks to the jetpack_subscriptions_exclude_these_categories filter. You can use that filter to exclude one or more categories.

In the example below, we exclude all posts belonging to the “VaultPress” and “Akismet” categories:

function jetpackme_exclude_posts_subscriptions( $categories ) {
    $categories = array( 'vaultpress', 'akismet' );
    return $categories;
}
add_filter( 'jetpack_subscriptions_exclude_these_categories', 'jetpackme_exclude_posts_subscriptions' );

Only send Subscription emails for posts belonging to a specific category

If, on the other hand, you’d rather not send any subscription emails by default, you can use the jetpack_subscriptions_exclude_all_categories_except filter.

In the example below, we only send subscription emails if our post belongs to the “VaultPress” or “Akismet” category:

function jetpackme_exclude_all_posts_subscriptions_except( $categories ) {
    $categories = array( 'vaultpress', 'akismet');
    return $categories;
}
add_filter( 'jetpack_subscriptions_exclude_all_categories_except', 'jetpackme_exclude_all_posts_subscriptions_except' );

The Subscriptions module includes thirteen other hooks. Do you want to learn more? Check the code.

Do you want to dive deeper into Jetpack, and discover other useful hooks to help you customize Jetpack? Make sure to follow this blog and discover a new hook each month!


Viewing all articles
Browse latest Browse all 20

Trending Articles