0

I need to do a curl request every time a new post is created.

In order to achieve that, i used the following code, very similar to the one provided in the Codex.

add_action('transition_post_status', 'wpse120996_do_curl', 10, 3);
function wpse120996_do_curl($new_status, $old_status, $post)
{
    $post_ID = $post->ID;
    error_log(" [ ". date("Y-m-d H:i:s") . " ] ". $post_ID . " : " .
    $old_status . " -> " . $new_status . "\n", 3, $_SERVER['DOCUMENT_ROOT'] . "/log.txt");

    if ($post->post_type != 'post' || $new_status != 'publish' || $old_status != 'draft') {
       return;
    }

    $url = 'https://api.telegram.org/bottoken/sendMessage';
    // Get cURL resource
    $curl = curl_init();
    // Set some options - we are passing in a useragent too here
    curl_setopt_array($curl, array(
          CURLOPT_RETURNTRANSFER => 1,
          CURLOPT_URL => $url,
          CURLOPT_USERAGENT => 'Codular Sample cURL Request',
          CURLOPT_POST => 1,
          CURLOPT_POSTFIELDS => array(
              chat_id => '@mychatid',
              text => print_r($post, true)
          )
      ));
    // Send the request & save response to $resp
    $resp = curl_exec($curl);
    error_log(" [ ". date("Y-m-d H:i:s") . " ] sending post with ID = ". $post_ID ."\n", 3, $_SERVER['DOCUMENT_ROOT'] . "/log.txt");
    if (!curl_exec($curl)) {
        die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
    }
    // Close request to clear up some resources
    curl_close($curl);
}

The problem is that wpse120996_do_curl() is called twice every time a post is saved, as you can see in the following screenshot. enter image description here

How can i get only one function call when a post is saved?

I'm running this code in Wordpress 4.8.2 with no plugin installed.

Edit: after adding error_log() to the code, looking into log.txt, it seems that wpse120996_do_curl() is called only once, but unfortunately two curl request are done.

 log.txt content

 [ 2017-10-13 16:31:31 ] 109 : new -> auto-draft
 [ 2017-10-13 16:31:37 ] 109 : auto-draft -> draft
 [ 2017-10-13 16:32:05 ] 109 : draft -> publish
 [ 2017-10-13 16:32:05 ] sending post with ID = 109
 [ 2017-10-13 16:32:06 ] 110 : new -> inherit
0

Replace this line:

if ($old_status != 'draft' || $new_status != 'publish') {

with:

if ( $post->post_type != 'post' || $new_status != 'publish' || $old_status != 'auto-draft' ) {

You want to run your code on post transition from auto-draft to publish, which will occur once only, when your post is created.

UPDATE

The problem is in this part of your code:

$resp = curl_exec($curl);
error_log(" [ ". date("Y-m-d H:i:s") . " ] sending post with ID = ". $post_ID ."\n", 3, $_SERVER['DOCUMENT_ROOT'] . "/log.txt");
if (!curl_exec($curl)) {
    die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}

You are calling curl_exec($curl) in two lines! First:

$resp = curl_exec($curl);

and second:

if (!curl_exec($curl)) {

Change your if statement to:

if (!$resp) {
  • i updated my code according to your suggestions. Looking into log.txt content, it seems that wpse120996_do_curl() is called only once, but i got two telegram messages yet. I can't figure out why two curl requests are made. – Dario Capozzi Oct 13 '17 at 16:59
  • @DarioCapozzi please read my updated answer. – Frank P. Walentynowicz Oct 13 '17 at 20:58

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.