Display content based on an event

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 );
}