I have this function which emails all users who subscribe to an author whenever the author creates a new post which works fine. I am trying to ajaxify the function and check every few few seconds if a post has been updated or if a new post has been published by a user. If so, update all the subscribers of that author via a "live" notification popup (It's not really live but it seems live as it's done through the setInterval function). Anyway, any help would be greatly appreciated!

Here is the current function:

add_action('publish_listings', 'vba_update', 10, 1);
add_action('draft_listings', 'vba_update', 10, 1);
function vba_update($post_id){ 
    $args = array(
        'include'   => tb_get_followers($user_id)
    $user_query = new WP_User_Query($args);

    if ( !empty($user_query->results) ) : 

        $post_title = get_the_title( $post_id ); 
        $post_url = get_permalink( $post_id ); 
        $subject = 'A post has been updated'; 
        $message = "A post has been updated on your website:\n\n";
        $message .= "<a href='". $post_url. "'>" .$post_title. "</a>\n\n"; 

        /** Send an individual message to each user */
        foreach ( $user_query->results as $user ) :
            wp_mail($user->data->user_email, $subject, $message);
  • I just came across a plugin called "Mark As Read" which is the closest to what I am trying to achieve but it's not done by ajax or in near realtime, if that helps anyone posting an answer. wordpress.org/plugins/mark-as-read – Matt H Jun 28 '18 at 0:47

So basicly you want an ajax method that checks for post changes.

Here's a great article on WP ajax for front & back-end.

In short you want to do the following:

  • Load the Ajax JS script on the pages you want to show the notification popup.
  • Create wp ajax server-side handling, with hook wp_ajax_{your_custom_action}. OR use the WP REST API.
  • Check if a post has changed etc, echo result back to ajax js script.
  • If ajax response is 'new_post' etc show popup.

I wouldn't use setinterval. Do the first ajax call on page load, if the ajax response is good, do the next ajax call after X seconds.

  • I don't think you need wp_ajax in here. You could use REST API, I guess... (If only that CPT is available via REST API) – Krzysiek Dróżdż Jun 28 '18 at 6:20
  • Sure, but the OP wants to ajaxify his method. – Bjorn Jun 28 '18 at 10:15
  • OP wants to do this on front-end. So you have to do this with JS and if you want to use JS to get anything from server you will use AJAX. What you don't need to do is to write your own AJAX processing on back-end - so no, you don't have to use wp_ajax_ hooks for this task ;) – Krzysiek Dróżdż Jun 28 '18 at 10:25
  • 1
    You're wright, i've changed my answer. – Bjorn Jun 28 '18 at 10:36
  • @Bjorn thanks for the reply, I understand what it is that I need to do but the thing is I am not experienced enough with coding at the moment to achieve it. If you have an example code which I can go off of based on my above example, that would be fantastic! :) – Matt H Jun 28 '18 at 23:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.