Hook of the Month: taking control of email subscriptions

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' );

Explore the benefits of Jetpack

Learn how Jetpack can help you protect, speed up, and grow your WordPress site. Get up to 50% off your first year.

Explore plans

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!

This entry was posted in Code snippets, Tips & Tricks and tagged , , , , , . Bookmark the permalink.

Jeremy Herve profile
Jeremy Herve

Jetpack Mechanic 🚀 at Automattic. WordPress, board games, TV Series, music.

Explore the benefits of Jetpack

Learn how Jetpack can help you protect, speed up, and grow your WordPress site. Get up to 50% off your first year.

Explore plans

Have a question?

Comments are closed for this article, but we're still here to help! Visit the support forum and we'll be happy to answer any questions.

View support forum

Comments

  1. David T. LeBlanc says:

    A timely post for me, but I do have question because I am not a coder. In what file do I put the above code or set a filter to “true”. My first thought would be in the functions.php file in a child theme.

    Like

  2. tomdryan says:

    Please just add this as an option that mere mortals can check on and off.

    Like

    • Jeremy says:

      Once you add the first filter to your theme’s functions.php or to a functionality plugin, you’ll get that checkbox on all new posts!

      Like

      • tomdryan says:

        Please put this option in the USER interface, where it belongs. Thx.

        Like

      • Jeremy says:

        If you don’t feel comfortable adding that line to your theme, or to a functionality plugin, you can go to Plugins > Add New > Upload in your dashboard, and install this little plugin:
        http://i.wpne.ws/f6mf

        The plugin includes the filter I mentioned earlier, and will add the option in the User Interface for you!

        Like

      • tomdryan says:

        Thanks Jeremy. I will check this out. It really should be included in the standard Jetpack interface though — because I’m going to need to make this modification on every WP site I run — and you are excluding anyone who hasn’t read this post from utilizing this feature.

        These kind of things are what make WP frustrating to use. Core functionality is an add on (or worse, a “code on”), rather than in the main product.

        Like

  3. Scott Winterroth says:

    I love the JP Subscriptions module, I recommend it to all of my students. I wish there was some type of delay option to send it it out later. Sometimes you find an error right after you publish or you hit publish on accident.

    Like

  4. Pam Huggins says:

    Works great with the code snippet plugin! Thank you!!
    I hope someday that Jetpack Subscriptions can send an email for custom posts too.
    I’m using a ComicPress theme and would love to be able to send out emails when I post a new comic.

    Thanks for all your hard work. Jetpack is AWESOME!

    Like

  5. Franck PARIENTI says:

    Dear Jeremy, it works.
    Thanks for the great advice. It works with the plugin https://wordpress.org/plugins/code-snippets/ and the code
    add_filter( ‘jetpack_allow_per_post_subscriptions’, ‘__return_true’ );

    It’d be even better if we can manage subscription to different subscribers list.
    Example :
    send subscription from category Price to our customers
    send subscription from category Job to our suppliers

    Like

  • Enter your email address to follow this blog and receive news and updates from Jetpack!

    Join 112.8K other subscribers
  • Browse by Topic