How to Send a Notification with WordPress

How to send a notification with wordpress

Sending push notifications using the OneSignal WordPress Plugin happens when publishing a blog post. You can also send anytime with the OneSignal Dashboard or API. See Sending Push Messages for more details.

Send Push Notifications from the OneSignal WordPress Plugin

Notifications can be configured to be sent when:

  • A post is created using WordPress's default post editor
  • A post is created using a third-party plugin
  • An RSS feed is updated

For Posts Created From WordPress's Default Post Editor

Create your post as you normally would, and then check Send notification on post publish.

If your post is a different post type, you might see "Send notification on forum publish". If you are updating instead of creating, you might see "Send notification on post update". Each of these options should successfully send a notification on the described action.

Set checkbox to checked or unchecked by default

You can set your notifications to send automatically in the OneSignal Plugin Settings under Automatic Notification Settings > Automatically send a push notification when I create a post from the WordPress editor.

This setting will make the checkbox unchecked or checked by default on all posts.

If your 3rd party plugin uses a custom post type, you can leverage our onesignal_meta_box_send_notification_checkbox_state filter to keep this permanently activated.

Issue: I can't find the checkbox

Newer versions of WordPress (~5.0+) put the OneSignal Push Notifications checkbox under the Document tab on the right-hand side toolbar (the other tab is Block). Make sure to verify the setting you want before publishing or updating your post!

Issue: There were "no recipients"

Perhaps you are attempting to send multiple notifications for the same post in a short period of time. The initial push notification will go through, but subsequent pushes for the same post will be rate-limited to a one-minute interval.

Issue: My push notification is not scheduling

Scheduling from the WordPress scheduler may or may not work depending on if your WordPress theme is blocking our plugin from sending the notification when the post gets published.

If the scheduler does not work, your options would be:

  1. Schedule the notification for the same time as the post from the OneSignal Dashboard or API. See Sending Push Messages and Product Demo Video

  2. Add custom code to the plugin to schedule it. In this example, you will find how the send notification code looks in the [onesignal_send_notification filter](#onesignal_send_notification-filter. The code sample below demonstrates how to change the delivery time on the notification:

    See our API Delivery Parameters for more information.

  3. You can use the Zapier OneSignal integration to send an automated notification based on a specific trigger.

For Posts Created From 3rd Party Plugins

"Post" Post Types

Make sure that Automatically send a push notification when I publish a post from 3rd party plugins is checked, as shown here:

Custom Post Types

If your 3rd party plugin is using custom post types, a notification will not be sent out by default. You must add the custom post type to this text box (comma-separated) to allow our plugin to automatically send notifications. This is to prevent accidental spam if a plugin sends many notifications of a custom type.

A list of WooCommerce Custom Post types can be found here: https://docs.woocommerce.com/document/installed-taxonomies-post-types/

Finding The Custom Post Type

When creating the Custom Post Type in the WordPress post editor, you will usually see the custom post_type name within the URL in your browser's address bar.

For example, https://yoursite.com/wp-admin/post-new.php?post_type=this_is_the_custom_post_type_name.

In the above example, the custom post type name within the URL is this_is_the_custom_post_type_name.

You can add that into the "Additional Custom Post Types for Automatic Notifications..." field in the OneSignal WordPress Plugin by separating it from other types by a comma, like this: article,this_is_the_custom_post_type_name

My Custom Post Type is Not Working

Certain post types may lack the necessary meta data our plugin uses to send the push notification.

Using our onesignal_include_post Filter you can add your post_type. OneSignal's WordPress plugin is open source on Github.

Send Push When a Page is Published

See our example code under onesignal_include_post Filter. More details on WordPress Post Types.

For RSS Feed Updates

Zapier is a 3rd party service that allows you to perform actions when a trigger occurs, such as publishing a new post. OneSignal has a Zapier integration for RSS feed updates. You can see the OneSignal Zapier integration here for more details.


How do I send multiple notifications for the same blog post?

If you wish to send multiple notifications for the same blog post, you can do so by simply opening the post in your Wordpress editor, checking the "Send notification on post publish" checkbox, and clicking "Update" or "Publish." You do not necessarily have to edit the post content to do this.

NOTE: You must wait at least one minute from the last notification event to be able to resend notifications for that particular post.


Welcome Notification

Welcome notifications an optional way to thank users after subscribing to your site. Users will see a welcome notification as soon as they subscribe. You can edit or disable them in the Configure section of your plugin.


Sending Web And Mobile Notifications Together With WordPress

If you have both a website and mobile app platforms configured in your OneSignal dashboard, and you have subscribers on both platforms, you can configure the OneSignal WordPress plugin to send both web and mobile notifications to your subscribers.

The instructions below demonstrate how to configure a link to open a web browser when the web push notification is clicked, and how to open your mobile app when the mobile notification is tapped.
Custom code is required to accomplish these steps.

Method 1. Show the URL with an in-app browser

Turn on this switch in your OneSignal WordPress plugin:

That will send the notifications to your mobile apps upon publishing a post. Done!

Method 2. Deep Linking

Turn OFF the Send notifications additional to iOS & Android platforms switch. (This option does not allow customizing the mobile notifications, so we have to disable this option first.)

Add the onesignal_send_notification filter code (examples below) into a PHP file that will always be called on all of your pages. We recommend using WordPress' must-use plugins feature, which allows you to add your code to a specific file and directory that is protected from being overwritten by updates to our plugin or WordPress.

WordPress Code Placement
  1. Look in your wp-content directory for the mu-plugins directory. If it doesn't exist, create it
  2. Create a file in the mu-plugins directory called "onesignal-mobile-support.php"
  3. Add the code snippet that matches your use case to the file and save it
Sending Notifications To Mobile Devices Only (No Browsers)
<?php
function onesignal_send_notification_filter($fields, $new_status, $old_status, $post)
{
    $fields['isAndroid'] = true;
    $fields['isIos'] = true;
    $fields['isAnyWeb'] = false;
    $fields['isChrome'] = false;
    $fields['data'] = array(
        "myappurl" => $fields['url']
    );
    /* Unset the URL to prevent opening the browser when the notification is clicked */
    unset($fields['url']);
    return $fields;
}
Sending Notifications To Both Web Browsers And Mobile Devices
<?php
    add_filter('onesignal_send_notification', 'onesignal_send_notification_filter', 10, 4);
    function onesignal_send_notification_filter($fields, $new_status, $old_status, $post) {
        /* Goal: We don't want to modify the original $fields array, because we want the original web push notification to go out unmodified. However, we want to send an additional notification to Android and iOS devices with an additionalData property.
         *     */
        $fields_dup = $fields;
        $fields_dup['isAndroid'] = true;
        $fields_dup['isIos'] = true;
        $fields_dup['isAnyWeb'] = true;
        // $fields_dup['android_channel_id'] = "<CHANNEL ID UUID HERE>";
        $fields_dup['data'] = array("customkey" => $fields['url']);
        /* Important to set web_url to support opening through both mobile and browser*/
        $fields_dup['web_url'] = $fields_dup['url'];
        /* Important to unset the URL to prevent opening the browser when the notification is clicked for mobile app users */
        unset($fields_dup['url']);
        $onesignal_post_url = "https://onesignal.com/api/v1/notifications";
        /* Hopefully OneSignal::get_onesignal_settings(); can be called outside of the plugin */
        $onesignal_wp_settings = OneSignal::get_onesignal_settings();
        $onesignal_auth_key = $onesignal_wp_settings['app_rest_api_key'];
        $request = array("headers" => array("content-type" => "application/json;charset=utf-8", "Authorization" => "Basic " . $onesignal_auth_key), "body" => json_encode($fields_dup), "timeout" => 60);
        $response = wp_remote_post($onesignal_post_url, $request);
        if (is_wp_error($response) || !is_array($response) || !isset($response['body'])) {
            $status = $response->get_error_code();
            $error_message = $response->get_error_message();
            error_log("There was a " . $status . " error returned from OneSignal when sending to mobile users: " . $error_message);
            return;
        }
        return $fields;
    }

Within your Mobile App's OneSignal Notification Click Handler add code to detect the result.notification.payload.additionalData which should be set to customkey parameter like shown in our Deep Linking Guide.


Did this page help you?