4

I have a little non-standard WP dev environment, I use one WP core for all my projects and switch each project in core's wp-config.php just changing the $project variable (e.g. proj1, proj2, example...). Projects and core are separated and each project has its own DB, wp-content folder, wp-config-dev.php (DB credentials and table prefix), and wp-config.php (usual wp-config that I deploy on the server).

//core's wp-config.php
<?php
$project = 'example';
define( 'WP_CONTENT_DIR', 'C:/dev/projects/'.$project.'/wp/wp-content' );
define( 'WP_CONTENT_URL', 'https://projects.folder/'.$project.'/wp/wp-content' );
include('C:/dev/projects/'.$project.'/wp/wp-config-dev.php');
if ( ! defined( 'ABSPATH' ) ) {
    define( 'ABSPATH', dirname( __FILE__ ) . '/' );
}
require_once( ABSPATH . 'wp-settings.php' );
// custom functions
include('custom.php');

All my projects on the development environment have the same user and password (e.g. foo and bar) and I change them when I export DB when my project is ready to production, so my goal to switch each project just with changing $project and not login in every time cuz it is really annoying.

When i switch beetwen projects i have always to log in again into switched project. In included custom.php file i have wp_signon() function that was worked on WP 4+ but since WP 5.0 it is stopped working. Earlier that approach automatically logged in each project and i even can't log out cuz it keep me logged on page reload :)

$current_user = wp_get_current_user();
if (!user_can( $current_user, 'administrator' )) {
    //without if(){} i have same behaviour
    $creds = array();
    $creds['user_login'] = 'foo';
    $creds['user_password'] = 'bar';
    $creds['remember'] = false;
    wp_signon( $creds, false );
}

Now after the switch, I need to update the page again to the admin bar appear and when I go to the console it redirects me to wp-login.php where is user input had filled and the password input are empty.

enter image description here

So how to make it always automatically logged in when I change the project and make that each session has no expiration time?

Note. I need to make it work only in the custom.php file that I include to core's wp-config, I don't need to make it in each project instance that I deploy on the server.


Update. Now autologin works fine, the problem was in second parameter of the wp_signon() function, it should be true if you use HTTPS on your site.

//custom.php

echo "is_user_logged_in() — ";
var_dump(is_user_logged_in());

//this condition not working after switch project, have to reload page
if (!is_user_logged_in()) {
    $creds = array();
    $creds['user_login'] = 'foo';
    $creds['user_password'] = 'bar';
    $creds['remember'] = false;
    $user = wp_signon( $creds, true ); //set second parameter to true to enable secure cookies

    add_filter( 'auth_cookie_expiration', 'keep_me_logged_in_for_1_year' );
    function keep_me_logged_in_for_1_year( $expirein ) {
        return 31556926; // 1 year in seconds
    }
}

It always keep me logged in for current project and I haven't problem with redirection to wp-login page but when i switch to another project I still need to reload page to login and for the admin bar to appear.

In drop down menu i switch project with changing $project var in core's config enter image description here

How to login immediately and without reloads after changing $project? Is there a problem with that engine got another DB and wp-content folder?

2 Answers 2

2

Here's one milder suggestion to try out, assuming different cookie domains, if you're using WordPress 5.5+

For your development installs, set the getenv environment variable WP_ENVIRONMENT_TYPE or define:

define( 'WP_ENVIRONMENT_TYPE', 'development' );

within the corresponding wp-config-development.php or the name you use for it.

Then extend the authentication cookie expiration period for the development installs with e.g.

add_filter( 'auth_cookie_expiration', function( $ttl, $user_id, $remember ) {

    // Adjust to your working environment needs.
    $my_working_types = [ 'development' ];
    $my_working_ttl   = YEAR_IN_SECONDS;

    return in_array( wp_get_environment_type(), $my_working_types, true ) 
        ? $my_working_ttl 
        : $ttl;

}, PHP_INT_MAX - 1, 3 );

So you would have to login once to start with, checking the "Remember me" on the login screen, else we get the session expiration.

There are four supported environment types of wp_get_environment_type() supported:

  • local
  • development
  • staging
  • production (default)

See the dev notes for the back story.

ps: The setup of using the same domain, with multiple sites in sub-directories, sharing the same WordPress install, reminds me of WordPress multisite.

5
  • Thanks for answer, but I am unsure about implementation environments with 'WP_ENVIRONMENT_TYPE', the main problem (now) that after switch I have to reload page to login in and for admin bar appear. I suppose it is a great point about different cookie domains (cuz I switch between different DBs even there is same log-pass), but honestly I am not so know how cookies works under the hood. Commented Sep 13, 2020 at 16:38
  • I also had updated the question with problem demonstration Commented Sep 13, 2020 at 16:43
  • Looks like you're using the same domain for all. Sounds like you're building your own sub-directory multisite. Maybe you should look into WP multisite? Maybe you can add your own redirection to homepage to avoid manual reloading, but be careful when deploying with auto-login code and for storing passwords in php files.
    – birgire
    Commented Sep 13, 2020 at 18:17
  • This looks great! But, weirdly, if I fully close my browser and reopen it, I still have to login again... It only remembers me between browser sessions if I check 'Remember Me' on the login screen. I looked into wp_set_auth_cookie in pluggable.php and this seems to cause the different behavior if $remember is false: $expire = 0;
    – rassoh
    Commented Apr 15, 2021 at 14:02
  • 1
    Thanks @rassoh, I should have mentioned that this should be used with "Remember me" checked, else we get the session expire.
    – birgire
    Commented Apr 15, 2021 at 15:52
1

I found out my solution, I just need make an additional redirect to make wp_signon() work after I switch project.

if (!is_user_logged_in()) {

    $creds = array();
    $creds['user_login'] = 'foo';
    $creds['user_password'] = 'bar';
    $creds['remember'] = false;
    $user = wp_signon( $creds, true ); 

    add_filter( 'auth_cookie_expiration', 'keep_me_logged_in_for_1_year' );
    function keep_me_logged_in_for_1_year( $expirein ) {
        return 31556926; // 1 year in seconds
    }

    //add redirection
    header("Location: "."https://".$_SERVER['HTTP_HOST']);
    exit();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.