我需要一个PHP页面使用wordpress登录验证进行保护。因此,我使用了以下代码:
$args = array(
\'echo\' => true,
\'redirect\' => site_url( $_SERVER[\'REQUEST_URI\'] ),
\'form_id\' => \'loginform\',
\'label_username\' => __( \'Username\' ),
\'label_password\' => __( \'Password\' ),
\'label_remember\' => __( \'Remember Me\' ),
\'label_log_in\' => __( \'Log In\' ),
\'id_username\' => \'user_login\',
\'id_password\' => \'user_pass\',
\'id_remember\' => \'rememberme\',
\'id_submit\' => \'wp-submit\',
\'remember\' => true,
\'value_username\' => NULL,
\'value_remember\' => false );
wp_login_form( $args );
但重定向不起作用。当我以管理员用户身份登录时,我会被重定向到wp admin页面。当我以用户身份登录时,我会被重定向到首页:-s
所以,我尝试了这个:
function login_redirect( $redirect_to, $request, $user ){
return home_url(site_url( $_SERVER[\'REQUEST_URI\'] ));
}
add_filter( \'login_redirect\', \'login_redirect\', 10, 3 );
不起作用…
所以我试着用这个(http://codex.wordpress.org/Plugin_API/Filter_Reference/login_redirect)
完整代码:
<?php
header( \'Cache-Control: no-store, no-cache, must-revalidate\' );
header( \'Cache-Control: post-check=0, pre-check=0\', false );
header( \'Expires: Mon, 26 Jul 1997 05:00:00 GMT\' );
header( \'Last-Modified: \' . gmdate( \'D, d M Y H:i:s\' ) . \' GMT\' );
header( \'Pragma: no-cache\' );
require(\'../wp-load.php\' );
get_header();
function my_login_redirect( $redirect_to, $request, $user ){
//is there a user to check?
global $user;
if( isset( $user->roles ) && is_array( $user->roles ) ) {
//check for admins
if( in_array( "administrator", $user->roles ) ) {
// redirect them to the default place
return $redirect_to;
} else {
return home_url();
}
}
else {
return $redirect_to;
}
}
add_filter("login_redirect", "my_login_redirect", 10, 3);
if ( is_user_logged_in() )
{
echo "Hello. You are logged in<br>";
wp_loginout();
}
else
{
echo "Hello. please login<br>";
wp_login_form();
echo"<a href=\\"".wp_lostpassword_url( get_permalink() )."\\" title=\\"Lost Password\\">I lost my password</a>";
}
get_footer();
?>
仍然。。。没有成功。
我正在使用WP 3.8版
===========================================
添加了功能。主题中的php文件
<?php
require_once(\'inc/Sidebar.php\');
register_nav_menu( \'top-menu\', \'Top Menu\' );
/*
* Extending excerpt
*/
function custom_excerpt_length( $length ) {
return 350;
}
add_filter( \'excerpt_length\', \'custom_excerpt_length\', 999 );
//removing default "Continue Reading from Excerpt
function sbt_custom_excerpt_more( $output ) {
return preg_replace(\'/<a[^>]+>Continue reading.*?<\\/a>/i\',\'\',$output);
}
add_filter( \'get_the_excerpt\', \'sbt_custom_excerpt_more\', 20 );
//redirecting all users to dashboard (only subscribers on home)
function redirect_users($user_login, $user) {
if(!current_user_can("subscriber")) {
wp_redirect(get_admin_url());
}
}
add_action(\'wp_login\', \'redirect_users\', 1, 2);
function wpse126853_redirect_to_request( $redirect_to, $request, $user ) {
// instead of using $redirect_to we\'re redirecting back to $request
return $request;
}
add_filter( \'login_redirect\', \'wpse126853_redirect_to_request\', 10, 3 );
?>