0

So, I'm building my first plugin for WordPress. It's a simple plugin that queries a SQL database, gets blog readers emails, and sends them an email whenever a post is updated or published.

The idea is to have an email which tells the reader if the post was updated from its older version or a new post is published. It is also supposed to provide a link to the post for quick reading.

I've gotten this far:

  add_action( 'publish_post', 'email_function' );

function email_function( $arg ) {
    $link = mysqli_connect("*********", "********", "********", "***********");

            if (mysqli_connect_error()) {

                die ("There was an error connecting to the database");

            } 

    $query = "SELECT `******` FROM `*******`";

    $msg = "There has been a new blog post at The White Road!";

    if ($result = mysqli_query($link, $query)){
         while ($row = mysqli_fetch_array($result)){

             mail($row['email'],"New Post!",$msg);

         }}else {
             //for testing
            echo "break";
         }
}

Now, the issue is that in this situation I want this function to be carried out automatically, but to be able to do that I'll have to be able to find a few unknowns programmatically. In particular, the post that is being updated, if the post is being updated or if it's new, the author of said post, and the link to its page.

I've had a look at the WordPress Codex, and all the low-hanging functions (which would be easy for a beginner to understand) require either the post ID or the ID of the author to make that happen. That, of course, does not fulfill the design.

Any help on how to find and use these things would be greatly appreciated. :)

1
  • I don't know whether this is a bad question or just badly worded, but finding something "unknown" in anything sounds pretty much impossible! I presume you mean you are trying to find the post ids based on some other criteria? Your questions isn't very clear Commented Mar 8, 2019 at 19:20

1 Answer 1

0

There is a post_updated hook in the Wordpress API that you could attach this function to with add_action.

https://codex.wordpress.org/Plugin_API/Action_Reference/post_updated

You could compare before and after, and if the values of a particular field differ, it would trigger an update. I suppose the easiest would be checking the datetime, alternatively, you could have it check the title if minor spelling/grammar changes don't merit an email.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.