i wanna redirect users every 12hours to another page
and want use header(location:url) and set_transient
i use this codes
$transient = get_transient( 'name_my_transient' );
if ( empty( $transient ) ){
function redirect() {
$data = 'redirected';
return $data;
header('location: http://www.google.com');
}
add_action('wp_head', 'redirect');
set_transient('name_my_transient', $data, 60*60*12 );
}
if i remove return $data it always redirect users
Your reflexion is good but i think you had made some mistakes on your code organization, try with this :
<?php
// Call your function with a top priority
add_action( 'wp_head', 'redirect', 1 );
function redirect() {
// Get the transien
$transient = get_transient( 'name_my_transient' );
// If the data do not exist, set it and redirect user
if ( $transient === false ) {
// Set the transient for 12 hours
set_transient('name_my_transient', 'redirected', 12 * HOUR_IN_SECONDS );
// Redirect the user with wp_redirect
wp_redirect( 'http://www.google.com' ); exit;
}
}
?>
Related
I have created a page template that will be used to show a custom wordpress login form.
<?php
/*
* Template Name: Login
*/
get_header();
if( !is_user_logged_in() ){
?>
html code
<?php
} else {
$user = wp_get_current_user();
if( current_user_can('edit_users') && in_array('customrole', $user->roles) ){
wp_redirect( site_url('/role-page') );
exit;
}
if( current_user_can('edit_users') && in_array('customrole1', $user->roles) ){
wp_redirect( site_url('/role-page1') );
exit;
}
}
get_footer();
?>
I've noticed that if an user is logged in, the page will be loaded and is blank, the users will be not redirected to the desired location. How I can fix this problem and let the logged users be redirected based on their role?
Use template_redirect
add_action( 'template_redirect', function() {
if ( is_user_logged_in()){
$restricted = array( 250(login page id) ); // all your restricted pages
if ( in_array( get_queried_object_id(), $restricted ) ) {
wp_redirect( site_url( '/custom_page slug' ) );
exit();
}
}
});
I think we can use wp_redirect before content is sent to the browser. You should use hooks for that.
But you can try this for custom template:
global $current_user;
$role = $current_user->roles;
$current_role = implode( $role );
if ( $current_role == 'customrole' ) {
$url = site_url( '/role-page' );
echo "<script>window.location.href = '$url';</script>";
exit;
}
With Wordpress, I'm trying to create a function which autologins users, but upon visiting certain pages also performs an autologout if the initial login was obtained via autologin.
I've written the following code but I guess my boolean check for the autologin boolean does not work.
/* AUTO LOGIN / LOGOUT */
function autologinout() {
global $wp;
// Autologin
if( isset($_GET['username']) ) {
$user = get_user_by('login', $_GET['username']);
// Redirect URL //
if ( !is_wp_error( $user ) ) {
if ( in_array( 'customer', (array) $user->roles ) ) {
wp_clear_auth_cookie();
wp_set_current_user ( $user->ID );
wp_set_auth_cookie ( $user->ID );
wp_redirect( '/lovelists/toon-lovelist/' );
$autologin = true;
exit();
}
}
}
// Autologout
$path = $_SERVER['REQUEST_URI'];
if ( is_user_logged_in() && $autologin = true && ( $path == '/lovelists/maak-lovelist/' || $path == '/lovelists/login/' ) ) {
wp_clear_auth_cookie(); // so you don't get the cache error
wp_logout(); // this will logout user
$autologin = false;
}
}
add_action( 'init', 'autologinout' );
Any idea what I'm doing wrong?
I am using wp_signon from inside a plugin and inside a shortcode function to login a user. To clarify, the short code is being called from a page content. The wp_signon function successfully returns the user info. But is_user_logged_in() is returning false. Why it's happening or how can I set is_user_logged_in() true so that user stays login?
ob_start();
$user_data = array();
$user_data['user_login'] = $username;
$user_data['user_password'] = $password;
$user_data['remember'] = $remember;
$user = wp_signon( $user_data, false );
ob_get_clean();
if ( is_wp_error($user) ) {
$err = $user->get_error_message();
} else {
$_SESSION['fc_user']=$user->ID;
var_dump($user); // gives the user info
var_dump(is_user_logged_in()); // gives false
}
The function wp_signon
sends headers to the page. It must be run before any content is returned.
So running it inside a shortcode (like you specified in your comment) is too late: headers have already been sent.
In order to achieve what you want you should divide your code in two parts, to be run at different time, something like this:
add_action( 'after_setup_theme', function() {
// run this only on the page you want
if( !is_page('the_page_you_want') )
return;
$user_data = array();
$user_data['user_login'] = $username;
$user_data['user_password'] = $password;
$user_data['remember'] = $remember;
$user = wp_signon( $user_data, false );
if ( is_wp_error($user) ) {
$GLOBALS['user_error_message'] = $user->get_error_message();
} else {
$_SESSION['fc_user'] = $user->ID;
}
} );
add_shortcode( 'your_shortcode', function( $atts ) {
if( is_user_logged_in() ) {
// get user info
return print_r( get_userdata(get_current_user_id()), 1 );
} elseif( isset($GLOBALS['user_error_message']) ) {
// show error
return print_r( $GLOBALS['user_error_message'], 1 );
}
} );
I just ran into a similar issue with an MVC Wordpress plugin.
For me the solution was to include
require_once($_SERVER['DOCUMENT_ROOT'].'/wp-includes/pluggable.php');
to have access to the core function via a plugin.
I have created a maintenance mode function, but I can only get it to work for one username.
get_currentuserinfo();
global $current_user;
// MAINTAINANCE MODE
function website_site_maintenance() {
global $current_user;
if ( 'josh' != $current_user->user_login ) {
// vars
$logout_url = wp_login_url().'?mode=maintenance';
wp_logout();
wp_redirect( $logout_url, 302 );
}
}
add_action('get_header', 'website_site_maintenance');
add_action('admin_init', 'website_site_maintenance');
// CUSTOM LOGIN MESSAGES
function website_my_login_message() {
if( $_GET['mode'] == 'maintenance' ){
$message = '<p class="message"><b>Site undergoing maintenance.</b></p>';
return $message;
}
}
add_filter('login_message', 'website_my_login_message');
this above function works, but I want to add more maintenance users.
So i've tried this but it does not work...
// MAINTAINANCE MODE
function website_site_maintenance() {
global $current_user;
$maintenance_users = array('josh','george','bob');
if ( $maintenance_users != $current_user->user_login ) {
// vars
$logout_url = wp_login_url().'?mode=maintenance';
wp_logout();
wp_redirect( $logout_url, 302 );
}
}
I simply tried to add the users into an array.
$maintenance_users = array('josh','george','bob');
But this doesn't seem to work as it should.
Instead of using
if ( $maintenance_users != $current_user->user_login ) {
// vars
$logout_url = wp_login_url().'?mode=maintenance';
wp_logout();
wp_redirect( $logout_url, 302 );
}
use
if ( !in_array($current_user->user_login, $maintenance_users) ) {
// vars
$logout_url = wp_login_url().'?mode=maintenance';
wp_logout();
wp_redirect( $logout_url, 302 );
}
The problem you're having is that you're comparing an array to a string, and this is not going to work. In the "correct" version you'll be checking whether the username of the currently logged in user is in the array $maintenance_users you specified. Hope that helps.
I have some custom post meta being generated and am ready to add to a post's meta. I know how to do this. However, save_post causes a redirection after POST data has been sent. This means I am redirected to the dashboard and lose access to my POST data - therefore I cannot debug easily.
Currently I am using something like:
add_action('save_post', 'something_process');
function something_process() {
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return;
print_r($_POST);
}
Is there a way to easily debug this?
The best approach for me has been to use a function to log the values to wp-content/debug.log, lifted from http://fuelyourcoding.com/simple-debugging-with-wordpress:
if(!function_exists('log_it')){
function log_it( $message ) {
if( WP_DEBUG === true ){
if( is_array( $message ) || is_object( $message ) ){
error_log( print_r( $message, true ) );
} else {
error_log( $message );
}
}
}
}
Then use the function like this in your save_post hook:
log_it($_POST);
log_it('The value for ' . $custom_field . ' is ' . $_POST[$custom_field]);
Make sure that wp-content/debug.log is writable, and that you have debugging enabled in wp-config.php:
#ini_set('display_errors',0);
define( 'WP_DEBUG', true ); // Turn debugging ON
define( 'WP_DEBUG_DISPLAY', false ); // Turn forced display OFF
define( 'WP_DEBUG_LOG', true ); // Turn logging to wp-content/debug.log ON
define( 'WP_POST_REVISIONS', false); // Disables revision functionality
Method 1:
if (isset($_POST)) die(print_r($_POST)); //answer by Tumas
Method 2:
create log file (my_logs.txt) in a folder, where you use this code:
add_action('save_post', 'something_process',11,11);
function something_process()
{
print_r($_POST);
$tmp = fopen(dirname(__file__).'/my_logs.txt', "a+"); fwrite($tmp,"\r\n\r\n".ob_get_contents());fclose($tmp);
}
The best solution I've found so far is storing the $_POST in a session var for access later.
First Approach:
die(print_r($post_id));
Second Approach:
var_dump($post_id);
Third Approach:
<?php
echo <pre>{whatever you want to echo goes here}</pre>
?>
Or take any browser add-ons for console logging
may one of three help..Good Luck
You could also save your debug messages in a WordPress option and show it as an admin message after the redirect.
// display all notices after saving post
add_action( 'admin_notices', 'show_admin_notices', 0 );
// your custom save_post aciton
add_action( 'save_post', 'custom_save_post' );
function custom_save_post() {
store_error_in_notices_option( 'my debug message' );
}
function store_error_in_notices_option( $m ) {
if ( ! empty( $m ) ) {
// store error notice in option array
$notices = get_option( 'my_error_notices' );
$notices[] = $m;
update_option( 'my_error_notices', $notices );
}
}
function show_admin_notices() {
$notices = get_option( 'my_error_notices' );
if ( empty( $notices ) ) {
return;
}
// print all messages
foreach ( $notices as $key => $m ) {
echo '<div class="error"><p>' . $m . '</p></div>';
}
delete_option( 'my_error_notices' );
}
I use this for a quick formatted output :
die( '<pre>' . print_r( $_POST, true ) . '</pre>');