如果您必须在内容之前/之后添加我们的消息,那么您必须使用“the\\u content”过滤器。如果您将消息放在一个短代码或挂钩中,这样主题定制就会更加集成,这会更好。但基本上,在运行逻辑以获取状态之后,只需等待消息显示出来。
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 );
}