Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8765051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:11:28+00:00 2026-06-13T16:11:28+00:00

I am writing a plugin that simply sends subscribers emails when new post is

  • 0

I am writing a plugin that simply sends subscribers emails when new post is published(so it shouldn’t send any emails when it’s on pending, updating, or draft, etc)

In the plugin definition I have:

add_action('plugins_loaded', 'setup_plugin_actions');

function setup_plugin_actions(){
  if(has_action('new_to_publish')){
    $simple_email_subscriber = new email_subscriber();

    add_action('new_to_publish', $simple_email_subscriber->email_subscribers());
  }
}

And in my email_subscribers method I have:

class email_subscriber(){
  function email_subscribers(){
    //get post information
    $post = get_post($post_id);

    $post = get_post( $post_id );
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return false;

    if(wp_is_post_revision($post->ID) || wp_is_post_autosave($post->ID)) return;

    //email code
    ......
  }
}

A very simple code that just trying to add an action listener to the publish_post method.

However, since publish_post does fire so often, I was getting 8 emails whenever I made an update, new publish, or even if I just stay on the wordpress page and it does auto updates.

I was trying to find some callback that only happens when a new post is truly made: new_to_post

Doesn’t work at all.

Can someone help me?

Cheers
Phil

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:11:30+00:00Added an answer on June 13, 2026 at 4:11 pm

    The reason you are getting so many emails is because you are not adding the action properly, explained below. As it stands now, you are actually calling your function to send the emails lots of times and hooking the result of the function to the action (which doesn’t really do anything), instead of hooking the function to the action.

    Your add_action has a couple of issues – you are adding your method to the transition new_to_publish action (which fires only when a post status is “new” and changes to “publish”) and not publish_post which will catch posts that are published from any status, and the method you are passing is invalid – the argument is a string for a function or array if it is a method on an object with the first element being the object and the second being the function. It should look like:

    add_action('publish_post', array($simple_email_subscriber, 'email_subscribers') );
    

    Your function is also not taking the $post_id as an argument, so it will never know what post to load. Your definition should be:

    function email_subscribers($post_id){ /* your code */ }
    

    You are also calling $post = get_post( $post_id ); twice in your function which isn’t necessary, but if you want to make sure that this function only runs when a post’s status is publish no matter how it is called, add:

    $post = get_post( $post_id );
    if ($post->post_status != "publish") return;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm (somewhat) new to JavaScript and I'm writing a timing plugin that requires <body
I am writing a jquery UI widget that simply wraps the bootstrap popover plugin,
I'm writing a simple jQuery plugin that will interface with a JSON object/array. I
I am writing a simple GreaseMonkey script that has a jQuery plugin called hoverIntent
I'm writing a plugin that will allow parameters to 'set it up.' But I
I'm writing a plugin that's modifying an existing UI. One thing I want to
I am writing a WordPress plugin that uses WP_HTTP for making an API call.
I am writing a wordpress plugin that redirects the user. for some reason, wp_redirect
I am writing a jQuery plugin that manipulates the value of an input field
I'm currently writing a JQuery plugin that loads colors from a JSON web service

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.