3

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?

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();
}
| improve this answer | |
1

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 at least to start with.

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.

| improve this answer | |
  • 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. – Karich Nikita Sep 13 at 16:38
  • I also had updated the question with problem demonstration – Karich Nikita Sep 13 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 Sep 13 at 18:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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