Auto login after wordpress installation

I am creating a product in wordpress and here i am installing the custom theme and plugins during wp installation.

No problem until here. But the admin wants to login automatically during the installation ends without prompting for log in and want to redirect to the page what i wish to give.

Read More

Any help?

I tried to put this code after the wp_install() in wp-admin/install.php

        $creds = array();

        $creds['user_login'] = $_POST['user_name'];

        $creds['user_password'] = $_POST['admin_password'];

        $creds['remember'] = true;          

        $user = wp_signon( $creds, false );

        if ( is_wp_error($user) )
            echo $user->get_error_message();                
        wp_redirect(home_url('products'));exit;

Related posts

2 comments

  1. Your problem seems to be that you hook in too early (aside from the problem that you’re editing a core file), so let’s take a look at wp-admin/install.php:

    No hooks at all, but at the end of the page when everything was properly rendered, there’s a call for wp_print_scripts( 'user-profile' ). And gladly there we’ll find a number of hooks. One that directly sits inside that function on top of it and is named after the function itself: wp_print_scripts. And other inside WP_Scripts.

    So let’s give this a try (untested) with a small custom plugin:

    <?php
    defined( 'ABSPATH' ) or exit;
    /* Plugin Name: (#129229) Auto-Login User after install */
    
    add_action( 'wp_print_scripts', 'wpse129229LoginAfterInstall' );
    function wpse129229LoginAfterInstall()
    {         
        $user = wp_signon( array(
            'remember'      => true,
            'user_login'    => $_POST['user_name'],
            'user_password' => $_POST['admin_password'],
        ), false );
    
        if ( is_wp_error( $user ) )
            echo $user->get_error_message();                
    
        exit( wp_redirect( home_url( "products" ) ) );
    }
    

    I’m not sure if admin_password is the proper form field id/name. Normally/IIRC WP uses user_password. Second, the user won’t get logged in with home_url( "products" ). You’ll need to use admin_url() for that.

    Last Note: NEVER EVER edit a core file. You’ll loose all the changes during any upgrade. If it would be just about installing (and not upgrading) your way of doing it would somehow even be valid (if you want to maintain a WP fork), but not if you want to upgrade.

  2. First, instead of editing the core file, notice the following:

    1. The wp-admin/install.php includes the wp-admin/includes/upgrade.php file, right at the top.
    2. That upgrade.php file checks for the custom wp-content/install.php file and includes it if there. So, your custom install.php file gets included in the install process. This is important because,
    3. The wp_install() function is wrapped in an if ( !function_exists() ) type of check. Meaning that the wp_install() function itself is “pluggable”.

    In other words, if you define your own wp_install() function in the wp-content/install.php, then yours gets used instead, letting you customize the whole function all you like, without editing core.

    So, step 1: Copy the existing wp_install() function, as is, into your wp-content/install.php file. You will modify this function instead of editing the core code.

    As for step two, you want to auto-log the resulting user in and redirect. Fortunately, all you really have to do is to set a cookie and then redirect and exit. Easily done. Instead of the return array bit at the end of the function, do something more like this:

    wp_set_auth_cookie($user_id, true);
    wp_safe_redirect(home_url('products'));
    exit;
    

    Note: Untested. Haven’t tried it yet. But it should work. You may need to add error handling and such as well.

Comments are closed.