# WordPress post published notifications on your phone

> Get a push notification when a post or page is published on your WordPress site, including scheduled posts going live.

- Company: WordPress (https://www.justpush.io/recipes/wordpress)
- Event: `post.published` (Post published)
- Tags: Websites, Content
- Install: https://studio.justpush.io/recipes/wordpress/post-published
- Web page: https://www.justpush.io/recipes/wordpress/post-published

## 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 Post published 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-post-published.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 — Post published
10. Description: Sends a push through JustPush Studio when a post or page is published.
11. */
12. add_action('transition_post_status', function ($new_status, $old_status, $post) {
13. // Only the first time something goes live: not updates, drafts, revisions or autosaves.
14. if ($new_status !== 'publish' || $old_status === 'publish') {
15. return;
16. }
17. if (!in_array($post->post_type, array('post', 'page'), true)) {
18. return;
19. }
20. if (wp_is_post_revision($post) || wp_is_post_autosave($post)) {
21. return;
22. }
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'     => 'post.published',
29. 'site'      => get_bloginfo('name'),
30. 'post_type' => $post->post_type,
31. 'title'     => get_the_title($post),
32. 'author'    => get_the_author_meta('display_name', $post->post_author),
33. 'scheduled' => $old_status === 'future',
34. 'url'       => get_permalink($post),
35. )),
36. ));
37. }, 10, 3);
38. ```
39. 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 → post published
// Fires when the "JustPush — Post published" mu-plugin (see INSTALL.md) reports a
// "post.published" 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 !== "post.published") {
        console.log(`Ignoring WordPress event "${event?.event ?? "unknown"}"`)
        return null
    }

    const kind = event.post_type === "page" ? "page" : "post"
    const by = text(event.author) ? ` by ${text(event.author)}` : ""
    const when = event.scheduled === true ? " (scheduled)" : ""

    return {
        title: `📝 New ${kind} published`,
        message: `${text(event.title) ?? "Untitled"}${by}${when}`,
        topic: "WordPress",
        priority: -1, // Low — good to know, nothing to do
        buttons: link(event.url, `View ${kind}`),
    }
}
```

## WordPress post.published webhook payload (sample)

```json
{
  "url": "https://example.com/ten-tips-for-a-calmer-inbox/",
  "site": "My Site",
  "event": "post.published",
  "title": "Ten tips for a calmer inbox",
  "author": "Jane Doe",
  "post_type": "post",
  "scheduled": false
}
```

## 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.
