# WordPress new comment notifications on your phone

> Get a push notification when someone comments on your WordPress site, with a button straight to the moderation queue when it needs approval.

- Company: WordPress (https://www.justpush.io/recipes/wordpress)
- Event: `comment.created` (New comment)
- Tags: Websites, Content
- Install: https://studio.justpush.io/recipes/wordpress/new-comment
- Web page: https://www.justpush.io/recipes/wordpress/new-comment

## Setup

1. Click Install in Studio and sign in. The recipe is added to your account and you get a personal webhook URL.
2. Copy the PHP snippet from the setup notes below. It already contains your endpoint URL.
3. Save it as a new .php file in wp-content/mu-plugins/ on your site (create the folder if it doesn't exist).
4. WordPress loads it automatically. Trigger a New comment to test.
5. WordPress doesn't send webhooks by itself, so this recipe comes with a small **must-use plugin**. Save the code below as `wp-content/mu-plugins/justpush-new-comment.php` (create the `mu-plugins` folder if it doesn't exist). WordPress loads files there automatically; there's nothing to activate.
6. ```php
7. <?php
8. /**
9. Plugin Name: JustPush — New comment
10. Description: Sends a push through JustPush Studio when a new comment is posted.
11. */
12. add_action('comment_post', function ($comment_id, $approved) {
13. // Spam and trashed comments aren't worth a push.
14. if ($approved === 'spam' || $approved === 'trash') {
15. return;
16. }
17. $comment = get_comment($comment_id);
18. // Real comments only, not pingbacks or trackbacks.
19. if (!$comment || !in_array($comment->comment_type, array('comment', ''), true)) {
20. return;
21. }
22. $pending = (string) $approved !== '1';
23. wp_remote_post('your webhook URL (shown in Studio after install)', array(
24. 'blocking' => false, // don't slow the page down waiting for JustPush
25. 'timeout'  => 3,
26. 'headers'  => array('Content-Type' => 'application/json'),
27. 'body'     => wp_json_encode(array(
28. 'event'      => 'comment.created',
29. 'site'       => get_bloginfo('name'),
30. 'status'     => $pending ? 'pending' : 'approved',
31. 'author'     => $comment->comment_author,
32. 'post_title' => get_the_title($comment->comment_post_ID),
33. 'excerpt'    => wp_trim_words($comment->comment_content, 20),
34. 'url'        => $pending
35. ? admin_url('edit-comments.php?comment_status=moderated')
36. : get_comment_link($comment),
37. )),
38. ));
39. }, 10, 2);
40. ```
41. To stop the notifications, delete the file.

## Code

Studio calls `handleRequest(request)` with the incoming webhook (`{ method, headers, body, query, raw }`) and sends the message object it returns. Returning `null` sends nothing.

```js
// WordPress → new comment
// Fires when the "JustPush — New comment" mu-plugin (see INSTALL.md) reports a
// "comment.created" event. WordPress has no webhooks of its own, so the snippet sends them.

// The snippet sends plain strings; treat blanks as missing.
function text(value) {
    return typeof value === "string" && value.trim() ? value.trim() : null
}

// Only link to real http(s) URLs.
function link(url, cta) {
    return typeof url === "string" && /^https?:\/\//.test(url) ? [{ cta, url }] : []
}

function handleRequest(request) {
    const event = request.body

    // Guard: only handle this recipe's event, so other snippets on this integration stay quiet.
    if (event?.event !== "comment.created") {
        console.log(`Ignoring WordPress event "${event?.event ?? "unknown"}"`)
        return null
    }

    const pending = event.status === "pending"
    const author = text(event.author) ?? "Someone"
    const on = text(event.post_title) ? ` on "${text(event.post_title)}"` : ""
    const excerpt = text(event.excerpt) ? `: ${text(event.excerpt)}` : ""

    return {
        title: pending ? "💬 Comment awaiting approval" : "💬 New comment",
        message: `${author}${on}${excerpt}`,
        topic: "WordPress",
        priority: pending ? 0 : -1, // Normal when it needs you, Low when it's already live
        buttons: link(event.url, pending ? "Moderate" : "View comment"),
    }
}
```

## WordPress comment.created webhook payload (sample)

```json
{
  "url": "https://example.com/wp-admin/edit-comments.php?comment_status=moderated",
  "site": "My Site",
  "event": "comment.created",
  "author": "Mark",
  "status": "pending",
  "excerpt": "Tip four changed my mornings. Do you have one for shared inboxes?",
  "post_title": "Ten tips for a calmer inbox"
}
```

## FAQ

### Does this work on iPhone and Android?

Yes. Install the JustPush app from the App Store or Google Play and sign in. Every phone signed in to your account gets the notification.

### Do I need to write code?

No. Install the recipe in Studio, paste your webhook URL into WordPress and you are done. The code is there if you want to change the text, the sound or the buttons.

### Can I change what the notification says?

Yes. After install the recipe's code is yours. Edit it in Studio and test it against the sample payload before you save.

### What does it cost?

JustPush is free for 30 days. After that it's $19.99 a year, or $39.99 once. There is no extra charge for recipes.
