从3.3开始,我们可以在短代码函数中有条件地将脚本排队,但当我尝试使用一些PHP类(使用session_start()
在__construct()
函数)您可以猜到,它给了我headers already sent
错误
问题是(这是使用Facebook PHP SDK in conjunction with the JS SDK), 我只想在帖子上存在一个短代码,并且当前帖子已经填写了2个元字段(类需要这2个值[应用ID和应用机密])的情况下实例化这个类。
有没有简单的解决方案?如果是这样,我可以在下面的代码中更改什么来允许这样做?
public function fb_connect_shortcode( $atts, $content = null ) {
extract( shortcode_atts( array( ), $atts ) ); /**/ global $post;
$app_id = get_post_meta( $post->ID, $this->meta_prefix . \'AppID\', true );
$app_secret = get_post_meta( $post->ID, $this->meta_prefix . \'AppSecret\', true );
if( $app_id !== \'\' && $app_secret !== \'\' ) {
/**
* This is the class I am trying to instantiate conditionally
* but since it uses session_start(), it can\'t send out headers
*/
$facebook = new Facebook( array(
\'appId\' => $app_id,
\'secret\' => $app_secret,
) );
// See if there is a user from a cookie
$user = $facebook->getUser();
if( $user ) {
try {
// Proceed knowing you have a logged in user who\'s authenticated.
$user_profile = $facebook->api( \'/me\' );
$app_id = $facebook->getAppId();
} catch( FacebookApiException $e ) {
echo \'<pre>\' . htmlspecialchars( print_r( $e, true ) ) . \'</pre>\';
$user = null;
}
}
wp_enqueue_script( \'jw-fbsdk\', plugins_url( \'jw-fbsdk.js\', __FILE__ ), array(), \'3.3.1\', true );
wp_localize_script( \'jw-fbsdk\', \'jwuc\', array(
\'appId\' => $app_id,
\'channelUrl\' => plugins_url( \'channel.php\', __FILE__ )
) );
if( is_null( $content ) )
$content = \'Connect with Facebook\';
if( isset( $user_profile ) && $user_profile ) {
return $user_profile[\'name\'];
} else {
return \'<div class="fb-login-button" data-scope="email,publish_stream,read_stream,status_update">\' . $content . \'</div>\';
}
} else {
return "You forgot to add your App ID and/or App Secret! Facebook needs these. :)";
}
}