我正在尝试为我的 wordpress 博客创建一个登录系统,该系统使用用户谷歌帐户信息登录,以便他们可以发表评论并对帖子进行投票等。我有关于如何执行此操作的 php 代码,当我将它用作静态 html 页面时它可以工作,但不知道在 wordpress 中的何处添加它才能正常工作而不会出现错误,我试图将它添加到标题中.php 但这似乎不正确,有时会产生一些错误,
<?php
//Googles API
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';
//start session
session_start();
$client = new Google_Client();
$client->setApplicationName("Google UserInfo PHP Starter Application");
// Visit https://code.google.com/apis/console?api=plus to generate your
// oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.
// $client->setClientId('insert_your_oauth2_client_id');
// $client->setClientSecret('insert_your_oauth2_client_secret');
// $client->setRedirectUri('insert_your_redirect_uri');
// $client->setDeveloperKey('insert_your_developer_key');
$client->setClientId('473410134519.apps.googleusercontent.com');
$client->setClientSecret('******');
$client->setRedirectUri('http://www.pharzan.com/bitlog');
$client->setDeveloperKey('AIzaSyBLKLt******');
$oauth2 = new Google_Oauth2Service($client);
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
return;
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if (isset($_REQUEST['logout'])) {
unset($_SESSION['token']);
$client->revokeToken();
}
if ($client->getAccessToken()) {
$user = $oauth2->userinfo->get();
// These fields are currently filtered through the PHP sanitize filters.
// See http://www.php.net/manual/en/filter.filters.sanitize.php
$email = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
$img = filter_var($user['picture'], FILTER_VALIDATE_URL);
$personMarkup = "$email<div><img src='$img?sz=50'></div>";
$name=filter_var($user['given_name'], FILTER_SANITIZE_STRING);
//Get user details if user is logged in
$user_id = $user['id'];
$user_name = filter_var($user['name'], FILTER_SANITIZE_SPECIAL_CHARS);
$email = filter_var($user['email'], FILTER_SANITIZE_EMAIL);
$profile_url = filter_var($user['link'], FILTER_VALIDATE_URL);
$profile_image_url = filter_var($user['picture'], FILTER_VALIDATE_URL);
$personMarkup = "$email<div><img src='$profile_image_url?sz=50'></div>";
// The access token may have been updated lazily.
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
}
?>
那么我需要使用这样的代码来显示用户标记图像并查看他们是否已登录。
<?php if(isset($personMarkup)): ?>
<img class='markup' src='<?php echo $profile_image_url;?>'> </img>
<?php endif ?>
<?php //USER NOT LOGGED IN
if(isset($authUrl)) {
print "<a class='login' href='$authUrl' title="Login with google" </a> ";
print "<script>console.log('.$authUrl.');</script>";
}
else {
print '<a class="log_out" href="?logout"> Logout?</a>';
print "<script>console.log('.$authUrl.');</script>";
}
?>
我的问题是我不知道在wordpress中包含这个代码的位置,如果我把它放在标题中,第一部分每次都会被加载并在该
start_session()
部分产生错误,如果我需要处理不同部分的信息页面,在页眉中说一次,在页脚中说一次我不知道如何包含代码。