我正在尝试为我的wordpress博客创建一个登录系统,该系统使用用户的Google帐户信息登录,以便他们可以发帖评论和投票等。我有关于如何使用它的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()
部分,如果我需要处理页面不同部分的信息,请在标题中说一次,在页脚中说一次,我不知道如何包含代码。
答案 0 :(得分:1)
WordPress身份验证比处理所有这些内容的.php文件复杂得多。没有一个文件。 此外,非常不鼓励更改任何核心文件,因为它使更新WordPress变得不可能或非常困难。
如果您决定实施自己添加Google OAuth登录的插件,那么一个很好的起点是the codex。
我会建议不要这样做。您正在寻找的功能是already implemented其他人使用免费的开源插件。你也可以看看那些起点。