1

i have a function that is not triggering when a post is updated. the way it works, i grab an array of names from JSON and i append these names (along with an image) to a post. when this post's content is updated, i would like to have it show up in real time to other users connected to the site (through websocket/zmq plugin). now, i actually have this working. the kicker is, it only works when i log in as admin and click the update button myself. i see the updated content displayed on the front end page and the edit post section (as admin) also shows the updated content. on the zmq side of things, the data is received, but only when the update button is clicked, so i don't think it has anything to do with zmq/websocket, but maybe i'm missing a step somewhere, regarding the status of the post.

how can i get it to work without clicking the update post button? here is my code, there is a little bit of ZMQ syntax in there but its not much:

// pass names from JS to php and update the teams post
add_action('wp_ajax_nopriv_test_function', 'test_function');
add_action('wp_ajax_test_function', 'test_function');

function test_function() {
  if ( isset($_POST) ) {

        $nameData = $_POST['nameData'];

        //Strip any double escapes then use json_decode to create an array.
        $nameDecode =  json_decode(str_replace('\\', '', $_POST['nameData']));


        //loop through names array and create a container for each
        $html_string = "";

       foreach ($nameDecode as $keyIndex => $name) {
            $html_string .= '<div class="team-container team-container--inline col col--md-2 col--lg-2 col--xl-2"><img src="'. get_template_directory_uri() .'/images/baseball/team' . $keyIndex . '.jpg"> <p>'.$name.' <p /></div>';
       }

        echo ( $html_string);

        // update teams post
        if($html_string != "") {
          $my_post = array(
            'ID'           => 6,
            'post_content' => $html_string,
        );

        // Update the post into the database
          wp_update_post( $my_post );
        } else {
          echo 'html string is empty!';
        }
    }
  die();
}

ZMQ plugin:

<?php
   /*
   Plugin Name: ZMQ
   Plugin URI:
   description: send ZMQ message
   Version: 1
   Author: Mr. Awesome
   Author URI:
   License:
   */

   add_action('post_updated', 'send_zmq_message', 10,4);

   $context = new ZMQContext();

   $socket = $context->getSocket(ZMQ::SOCKET_PUSH);
   $socket->connect('tcp://127.0.0.1:5556');

   function send_zmq_message($post_ID, $post_after, $post_before){
     global $socket;
     global $context;

      if (get_the_ID() === 6) {
        $post_content = $post_after->post_content;

        $raw_data=array(
           'updated_post_content'=>$post_content,
        );

        $json_data= json_encode($raw_data);
        $socket->send($json_data);
      }

   }

?>
2
  • 2
    You should use $post_ID and not get_the_ID() in your if statement. Jul 21, 2018 at 18:20
  • wow, i made that change and it worked. thank you. so simple.... Jul 21, 2018 at 20:22

0

Your Answer

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

Browse other questions tagged or ask your own question.