-1

Ok, I am building this membership plugin.

Plugin sends user to a digital payment site and this one is sending user after the operation back to the wp site with a code (POST), to a page that was chosen by user (on plugin's options panel). Plugin listens to this POST, grabs the code and makes a wp_remote_get request to digital payment API, who informs plugin if the payment was successful or not.

My goal is to display to the user on this same page he/she was sent back by the gateway the status of the payment (ACTIVE, PENDING or CANCELLED).

My code is something like this (variables are omitted):

function wxy_listener(){

if( !isset( $_GET['code'] ) ){ 
    return false;
}  
  if( !isset( $_GET['returnfrom'] ) || 'ps' != $_GET['returnfrom'] ){ 
    return false;
}

$code = $_GET['code'];

global $wxy_options;
$psToken = ...;
$psEmail = ...;
$psSandbox = ...;
//URL for the first HTTP POST request
$psUrl = ...;
//If Sandbox is on, we should set  different URL...

if ($psSandbox === "1") {
  $psToken = ...;
  $psEmail = ...;
  $psUrl = ...;
}
$user_info = wp_get_current_user();
$user_email = $user_info->user_email;

$response = wp_remote_retrieve_body( wp_remote_get( $psUrl ) );
if( is_wp_error( $response ) ) {
                // There was an error
                $error_message = $response->get_error_message();
                wxyLog($error_message);
      return false;
    }

$resp_xml = simplexml_load_string($response);
if ($resp_xml === false) {
 wxyLog('Failed loading RESP_XML');  
 return false;    
 } 

  $subsc_status = $resp_xml->status;

        // HOW TO ECHO THE STATUS AT THIS MOMENT ON THE PAGE, INSIDE THE CONTENT (AFTER OR BEFORE)?    

}
add_action('init','wxy_listener');

I could use the_content filter, but inside a function? As far as I've read, I am not supposed to do this.

I do think it is a silly question, but...

0

If you have to add our message before/after content then you're stuck with 'the_content' filter. It would be better if you put your message in a shortcode or hook so the theme customizations were more integrated. But essentially after you run your logic to get the status you just need to wait till the message makes sense to display.

add_action('init','wxy_listener');

function wxy_before_the_content ( $content ) {
    $status = get_subsc_status_message();
    if(!empty($status)){
        $custom_content = '<div class="my-plugin-message">'.$status.'</div>';
    } else return $content; // no status info
    $custom_content .= $content;
    return $custom_content;
}

function wxy_after_the_content ( $content ) {
    $custom_content = 'AFTER CONTENT GOES HERE';
    $content .= $custom_content;
    return $content;
}

function get_subsc_status_message()
{
    global $subsc_status;
    if ( empty( $subsc_status) ) return '';

    $messages = array (
        'ACTIVE'    => __('Your stuff is active.', 'text_domain'),
        'PENDING'   => __('Your stuff is pending.', 'text_domain'),
        'CANCELLED' => __('Your stuff is cancelled.', 'text_domain'),
    );

    if ( isset( $messages[ $subsc_status ] ) {
        return $messages[ $subsc_status ];
    }

    return '';
}

function wxy_listener(){

    if( !isset( $_GET['code'] ) ){ 
return; // nobody is listening
    }

    if( !isset( $_GET['returnfrom'] ) || 'ps' != $_GET['returnfrom'] ){ 
return; // nobody is listening
    }

    $code = $_GET['code'];

    global $wxy_options;
    $psToken = ...;
    $psEmail = ...;
    $psSandbox = ...;

    // URL for the first HTTP POST request
    $psUrl = ...;

    // If Sandbox is on, we should set  different URL...
    if ($psSandbox === "1") {
        $psToken = ...;
        $psEmail = ...;
        $psUrl = ...;
    }

    $user_info = wp_get_current_user();
    $user_email = $user_info->user_email;

    $response = wp_remote_retrieve_body( wp_remote_get( $psUrl ) );
    if( is_wp_error( $response ) ) {
        // There was an error
        $error_message = $response->get_error_message();
        wxyLog($error_message);
return; // nobody is listening
    }

    $resp_xml = simplexml_load_string($response);
    if ($resp_xml === false) {
        wxyLog('Failed loading RESP_XML');  
return; // nobody is listening 
    } 

    // store the response for later
    global $subsc_status;
    $subsc_status = $resp_xml->status; 

    // add hooks to the content
    add_filter( 'the_content', 'wxy_before_the_content', 0 );
    add_filter( 'the_content', 'wxy_after_the_content', 99 );
}
  • So this is essentially the same answer as s-ha-dum's, right? Can I assume you too do not see any harm in hooking into a filter inside a function (which is hooked to an action - init)? Second question: why do you say that this solution would affect theme customizations integrations? – Luis Rock Dec 10 '15 at 10:43
  • The content filter hook is the same other than he uses a closure function which is great but not supported on all PHP versions. This also shows the difference between before and after hooks. I also moved the actual call to get the value into its own function which is a design choice. I don't know your whole setup but appending values to content just seems messy. I think a theme should know where it expects your message and to design it appropriately. Like, what if the page your displaying this message on doesn't even show its content? Maybe its job is only to list a custom loop? – jgraup Dec 10 '15 at 12:56
  • I see...using closures will send away PHP 5.2 and below users. And I also get the point about a shortcode being a preferable solution. Thanks for the tip. I will think about it. – Luis Rock Dec 10 '15 at 13:00
  • I actually find odd issues in 5.3 as well. It needs to be upgraded but if you're a plugin then larger support is key. Don't feel bad using hooks. You might be able to check and make sure a short code exists before outputting inside content, same with other hooks. You could check once you're in the content filter for alternate subscribers and only then append/prepend the value. – jgraup Dec 10 '15 at 13:06
  • Who downvoted and why? – Luis Rock Dec 10 '15 at 16:03
1

Your code runs on init which is well before the the_content filter is called. There is no reason you can't hook into it. It should be as easy as:

add_filter(
  'the_content',
  function ($content) use ($subsc_status) {
    return $content.' || '. $subsc_status;
  }
);
  • Yes, this works. So you really think that there is no harm in hooking into a filter inside a function (which is hooked to an action - init)? – Luis Rock Dec 10 '15 at 10:36
  • Oh, and thanks for introducing me to closures. I didn't know their existence... – Luis Rock Dec 10 '15 at 10:56
  • No. There is no problem with chaining hooks, other than efficiency if you get too carried away. There is a problem with closures in the they are very hard to remove from hooks. That may or may not be a problem for you. – s_ha_dum Dec 10 '15 at 12:52
  • I will mark jgraup's answer as the one because he elaborated more and someone downvoted it (and I don't know why). But your answer was extremely helpful for me. It is a shame that I cannot vote yet. – Luis Rock Dec 10 '15 at 16:05

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.