I would like to send push messages from my website to iOs/android users everytime I publish a new post. Which would be the same as execute php code I think. I have tried to paste a php script which send a push message to my iOs device everytime I launch it, but without success. I've pasted the php code right after this code

<div id="content">


    if( have_posts() ):

        get_template_part( 'templates/content/content', str_replace('bw_pt_', '', get_post_type() ) );

In the single.php file in word press->Appearance->editor. As well as I have uploaded the ck.pem file in the same folder. This is my php code and when I launch it from the terminal, it works well. What I need is to add some custom code, which would be a retrieve database to see all device and to send a push notification for each device. I'll do the code later, for the moment I need to know were I have to put this code, in order to be executed everytime the admin press the publish new entry button.

This is my php code:


// Put your device token here (without spaces):
$deviceToken = '993c92d21b053c5115d1xxXXXXxxXXXXXxxxXXXXXxxebe9c21f0f59a0723de8bd38';

// Put your private key's passphrase here:
$passphrase = '123456';

// Put your alert message here:
$message = 'Hello world!';


$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Open a connection to the APNS server
$fp = stream_socket_client(
    'ssl://gateway.sandbox.push.apple.com:2195', $err,

if (!$fp)
    exit("Failed to connect: $err $errstr" . PHP_EOL);

echo 'Connected to APNS' . PHP_EOL;

// Create the payload body
$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default'

// Encode the payload as JSON
$payload = json_encode($body);

// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
    echo 'Message not delivered' . PHP_EOL;
    echo 'Message successfully delivered' . PHP_EOL;

// Close the connection to the server

1 Answer 1


Wrap it in a function and hook it to transition_post_status:

function wpse_18140_transition_post_status( $new, $old, $post ) {
     if ( $new === 'publish' && $new !== $old ) {
           // Your code here

add_action( 'transition_post_status', 'wpse_18140_transition_post_status', 10, 3 );

This will run everytime a post is published, but not when you simply update the post content.

  • Sorry I'm really noob in word press. Can you tell me in what file do I need to do that?
    – Alfro
    Mar 17, 2015 at 12:10
  • Really it belongs in a plugin, but you can get away with placing it in your theme's functions.php Mar 17, 2015 at 12:23
  • And I do the function call from the single.php file?
    – Alfro
    Mar 17, 2015 at 12:29
  • No. This is a hook that runs in the backend. Nothing to do with your template files. You don't call it, WordPress will when you publish a post. Mar 17, 2015 at 12:30
  • Thanks a lot, now is doing a try to connect when I post a new entry, but I get always this connection timeout message: "Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Connection timed out) in /home/mongini/public_html/guiasdelsur/wp-content/themes/peliegro/functions.php on line 33 Failed to connect: 110 Connection timed out" Do you have any idea of what the problem can be?
    – Alfro
    Mar 17, 2015 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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