• I am wanting to run code contained within a plugin I have created when a webhook is triggered by a POST request on a webofrm. All of my code works except “null” is always returned, or rather, some blank space and newline characters followed by null. My abridged code is as follows:

    `
    add_filter(‘wpwhpro/run/actions/custom_action/return_args’, ‘CreateInvoice’, 10, 3);

    function CreateInvoice($return_args, $identifier, $response_body){

    //Some code to perform the task I want (which does work)

    if( $identifier === ‘student-invoice’ ){

    $return_args = array(
    ‘success’ => true,
    ‘msg’ => ‘The call was successful’,
    ‘data’ => array()
    );

    return $return_args;

    }
    }
    }

    When implemented as a code snippet the response is fine but not when implemented as a plugin. For my code to function, it must be implemented as a plugin and not a snippet. I was wondering if you know any reason why the response would be null when I use your custom_action webhooks within a plugin?

    Many thanks,

    John

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Ironikus

    (@ironikus)

    Hey @johnrichardarmstrong – thank you for your message, as well as for using our plugin.

    Apart from the fact that your code above is wrongly formatted and contains invalid code, the issue you are facing is that you return the arguments only within the if clause.
    Please use the properly formatted code down below.

    add_filter( 'wpwhpro/run/actions/custom_action/return_args', 'CreateInvoice', 10, 3 );
    function CreateInvoice($return_args, $identifier, $response_body){
    
    	//Some code to perform the task I want (which does work)
    
    	if( $identifier === 'student-invoice' ){
    
    		$return_args = array(
    		'success' => true,
    		'msg' => 'The call was successful',
    		'data' => array()
    		);
    
    	}
    
    	return $return_args;
    }

    Feel free to let me know in case you have any further questions. ??

    Thread Starter johnrichardarmstrong

    (@johnrichardarmstrong)

    Thanks for your very prompt response. I’m not sure that it solves the issue I am having though. I have tried to simplify my problem to help illustrate it better:

    I have taken on board your points about formatting and where the return statement is placed but I am still getting the response (some whitespace followed by null).

    “null”

    My code looks like this:

    add_filter('wpwhpro/run/actions/custom_action/return_args', 'SimpleFunction', 10, 3);
    
    function SimpleFunction($return_args, $identifier, $response_body){
    
        if( $identifier === 'test-webhook-as-plugin' ){
            
            $return_args = array(
                'success' => true,
                'msg' => 'The call was successful',
                'data' => array()
            );
            
            //wp_mail(myEmailAddress, 'function fired successfully', 'see subject');
        }
        
        return $return_args;
    }

    As you can see, to test that the code was actually being run I had the wp_mail (now commented out) function send me an email which did arrive, showing that the code did run. I was wondering if there is something obvious I am missing. The webhook can be triggered using the following GET request:

    https://findmytutoronline.com/?wpwhpro_action=receive-invoice-data&wpwhpro_api_key=xxxxxxxxxxxx&action=custom_action&wpwh_identifier=test-webhook-as-plugin

    Many thanks again; I do appreciate you efforts.

    John

    Plugin Contributor Ironikus

    (@ironikus)

    Hi @johnrichardarmstrong – Thank you for your answer.

    The most important thing up front: Please NEVER post your webhook URL within a public place and keep it ONLY for yourself. This is critical for security in case you do not work with access tokens or whitelists or special permissions.

    The code looks correct now, but the issue I can see is most likely not caused by our plugin.
    Since the output looks very odd to me (from the link you posted), the issue might appear from some other malfunctioned PHP code.
    From experience, I know that poorly coded plugins contain some empty space or characters in front of the <?php tag of PHP files, which is outputted as content. This should be fixed in any way since it can break the functionality of a lot of different plugins.

    Please also make sure that you do not use a similar code on another position within your plugin that is malfunctioned like the previous one you posted.

    For further debugging, I suggest you deactivate your plugins and activate it one by one to see if the error persists.

    If you have any further questions, feel free to let me know.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Webhooks returning null’ is closed to new replies.