Wordpress包括新的php sdk facebook
我已经安装了wordpress 4.1和以下插件:Wordpress包括新的php sdk facebook,php,wordpress,facebook,sdk,include,Php,Wordpress,Facebook,Sdk,Include,我已经安装了wordpress 4.1和以下插件: 伊塞姆斯安全 WordPress搜索引擎优化 我插入了facebook sdk的一个函数 代码如下: //Footer add_action('wp_footer', 'my_footer'); function my_footer() { $post_to_check = get_post(get_the_ID()); if ( is_singular() && has_shortcode( $post_t
- 伊塞姆斯安全
- WordPress搜索引擎优化
//Footer
add_action('wp_footer', 'my_footer');
function my_footer() {
$post_to_check = get_post(get_the_ID());
if ( is_singular() && has_shortcode( $post_to_check->post_content, 'myshortcode' ) ) {
?>
<script>
var DEBUG = true;
jQuery(document).ready(function($){
$.ajax({
type : "POST",
url : "index.php",
data : { action : "value" },
success : function(response){
// the server has finished executing PHP and has returned something, so display it!
if(DEBUG){ console.log('AJAX done...'); }
}
});
});
</script>
<?php
}
}
//ajax
add_action('init', 'my_request');
function my_request() {
if ( isset($_POST['action']) && $_POST['action'] == 'value' ) {
session_start();
require_once __DIR__ . "/sdk/autoload.php";
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookSDKException;
$app_id = getAppId();
$app_secret = getAppSecret();
$rdr_fb_url = curPageURL();
$required_scope = 'public_profile, email';
if(empty($app_id) || empty($app_secret))
exit('Error.');
FacebookSession::setDefaultApplication($app_id , $app_secret);
$helper = new FacebookRedirectLoginHelper($rdr_fb_url);
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
// When Facebook returns an error
die(" Error : " . $ex->getMessage());
} catch(\Exception $ex) {
// When validation fails or other local issues
die(" Error : " . $ex->getMessage());
}
var_dump($session);
if ($session){
$user_profile = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
echo 'Hi'.$user_profile->getName();
}else{
//display login url
$login_url = $helper->getLoginUrl( array( 'scope' => $required_scope ) );
}
?>
<div class="login">
<a href="<?php echo $login_url; ?>">Login</a>
</div>
<?php
exit();
}
}
//页脚
添加_操作(“wp_页脚”、“我的_页脚”);
函数my_footer(){
$post_to_check=get_post(get_the_ID());
if(is_singular()&&has_shortcode($post_to_check->post_content,'myshortcode')){
?>
var DEBUG=true;
jQuery(文档).ready(函数($){
$.ajax({
类型:“POST”,
url:“index.php”,
数据:{操作:“值”},
成功:功能(响应){
//服务器已经完成了PHP的执行,并返回了一些内容,所以请显示它!
if(DEBUG){console.log('ajaxdone…');}
}
});
});
已解决:我在ajax调用之外的函数my_footer(){