0

Is it possible to communicate with a plugin from an outside source?

I have an apache web server set up with wordpress running, and I would like to have automated input from a script trigger an action in a plugin.

I thought at first I might just be able to make a plugin that listens on a socket for input and uses hooks to call the right functions from other plugins. This just caused the website to never load completely.

Is it possible? Am I just missing something?

Edit: If anyone needs clarification let me know and I will do my best to explain differently. I realized that it might not be clear where the outside input would be coming from. It would be completely seperate from what is happening on the website, more like a bash script run locally on the server.

Edit2: My question might be a duplicate of Setting up an API "listening" page, but I'm not restricted to just an HTTP post.

Edit3: I also realized the plugin I was using might help:(plugin.php)

$address = 'localhost';
$port = 8888;

if(( $sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false ) {
  error_log("socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "\n");
}


if(socket_bind($sock, $address, $port) === false ){
  error_log("socket_bind() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n");
}

if(socket_listen($sock, 5) === false){
  error_log("socket_listen() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n");
}  

socket_set_nonblock($sock);

do{
  if(($msgsock = socket_accept($sock)) === false ){
    error_log("socket_accept() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n");
  }
  else{
    do{
      if( false === ($buf = socket_read($msgsock, 2048, PHP_NORMAL_READ))){
        error_log("socket_read() failed: reason: " . socket_strerror(socket_last_error($sock)) . "\n");
      }
      //Do things with buf
      socket_close($msg_socket);
    } while(true);
  }
  break;
} while(true);

socket_close($sock);

Right now it doesn't do anything except for wait for a connection and read 2048 bytes, but when it goes to socket_accept it gets an error "Success".

1
  • I suspect you need to look at your problem from a different perspective. Assuming I'm understanding your goal, I think you should be looking at AJAX type interactions with your environment. Halting the output of a page for external input is not the way http is designed to work.
    – KenB
    Mar 6 '16 at 6:18
0

The php code at apache, and therefor wordpress, is executed only as a response to an http request, therefor you can not "wait for event" the way you try to do. You will either have to trigger the required action via http, a specifically crafted URL for example, or load wordpress from the CLI, and after it has bootstraped, execute your code. You can look at the code of wordpress CLI utility https://github.com/wp-cli/wp-cli to learn how ir can be done.

2
  • If I triggered the action via say an http post how would I access whatever information was sent via the post?
    – Phenom588
    Mar 10 '16 at 17:47
  • @Phenom588, the obvious way is from the reply to the post. Mar 10 '16 at 18:20
0

In case you need the action to happen independently from any visitor action, then consider WP_Cron.

In the above fashion, you could:

  • Create a function that calls something outside the server. Ex: using "curl" or REST or anything.
  • Add that function as an action like:

    add_action( 'my_task_hook', 'my_task_function' );
    
  • Set WordPress cron like this:

    if ( ! wp_next_scheduled( 'my_task_hook' ) ) {  
        wp_schedule_event( time(), 'hourly', 'my_task_hook' );
    }
    

Just to make sure, the above solution does not take into account any visitor activity. It's a server (yours) to server (3rd party) communication based on an automated manner via WordPress Cron Jobs.

1
  • Looking at the wp_schedule_event reference it says it is dependent on visitor activity, "the action will trigger when someone visits your WordPress site". I'm trying to find something that is completely independent of any user interaction.
    – Phenom588
    Mar 10 '16 at 17:50

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.