Title: Create a PHP Listener
Last modified: March 13, 2018

---

# Create a PHP Listener

 *  Resolved [pc2chris](https://wordpress.org/support/users/pc2chris/)
 * (@pc2chris)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/create-a-php-listener/)
 * Hi,
 * I’m a completely new to WordPress & PHP but have been asked to look at adding
   a webhook into a customer website such that we can perform an HTTP post to it
   with some json data.
 * I’ve been looking on the web to try find some sort of tutorial but as a starter
   all I can find is something along the following lines.
 *     ```
       function process_webhooks() {
   
       	if( ! isset( $_GET['listener'] ) || strtolower( $_GET['listener'] ) != 'pcl' ) {
       			return;
       	}    			
       }
       add_action('init', 'process_webhooks');
       ```
   
 * What I don’t know is where this code should be added to i.e. which file, and 
   how I can perform a simple test to see that it has been actioned.
 * Any help on this would be appreciated.
 * Chris.

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Jacob Peattie](https://wordpress.org/support/users/jakept/)
 * (@jakept)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/create-a-php-listener/#post-10069865)
 * If you’re adding code that’s being hooked (with `add_action()`, like in your 
   example, or `add_filter()` then it should go in your theme’s functions.php file.
 * But if it’s not your own personally developed theme then you run the risk of 
   losing it if the theme updates. The better approach is to create a [Child Theme](https://codex.wordpress.org/Child_Themes),
   or better yet [a simple plugin](http://justintadlock.com/archives/2011/02/02/creating-a-custom-functions-plugin-for-end-users).
   Then you put your code in the child theme’s functions.php file or your own plugin
   file.
 * Regarding creating a webhook, if you need to have a URL that something can post
   JSON to, then a custom REST API endpoint would be the best way to go about it.
 * You can read the documentation on adding REST endpoints [here](https://developer.wordpress.org/rest-api/extending-the-rest-api/adding-custom-endpoints/),
   but here’s a brief overview.
 * The first thing is to register the route:
 *     ```
       add_action( 'rest_api_init', function () {
         register_rest_route( 'myplugin/v1', '/endpoint', array(
           'methods'  => 'POST',
           'callback' => 'my_awesome_func',
         ) );
       } );
       ```
   
 * This creates a URL at `http://example.com/wp-json/myplugin/v1/endpoint`. This
   will be the webhook URL. The `methods` argument means that it will accept POST
   requests, and `callback` is the PHP function that will handle the request.
 * It just needs to be a callable function with a single argument that represents
   the request. This will also go in the functions file or plugin:
 *     ```
       function my_awesome_func( $request ) {
       	// Do something here.
       }
       ```
   
 * If the request is JSON in the body, then you can retrieve that using the `get_json_params()`
   method of the request:
 *     ```
       function my_awesome_func( $request ) {
       	$data = $request->get_json_params();
       }
       ```
   
 * That will set `$data` to a PHP array representing the JSON data. Then you can
   do with it what you’d like.
 * For testing, a simple method might be to log the request to the error log:
 *     ```
       function my_awesome_func( $request ) {
       	$data = $request->get_json_params();
   
       	error_log( print_r( $data, true ) );
       }
       ```
   
 * Where that ends up depends on your server setup, by try looking for an `error_log`
   in the root directory, or `debug.log` in `wp-content/`.
 *  Thread Starter [pc2chris](https://wordpress.org/support/users/pc2chris/)
 * (@pc2chris)
 * [8 years, 2 months ago](https://wordpress.org/support/topic/create-a-php-listener/#post-10074716)
 * Thanks for the answer [@jakept](https://wordpress.org/support/users/jakept/).
   I went down the route of adding a simple plugin which is now working as expected.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Create a PHP Listener’ is closed to new replies.

## Tags

 * [listener](https://wordpress.org/support/topic-tag/listener/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [webhook](https://wordpress.org/support/topic-tag/webhook/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 1 reply
 * 2 participants
 * Last reply from: [pc2chris](https://wordpress.org/support/users/pc2chris/)
 * Last activity: [8 years, 2 months ago](https://wordpress.org/support/topic/create-a-php-listener/#post-10074716)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
