1

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.

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;
  • i have updated the code i tried to login – Balas Jan 10 '14 at 6:58
  • Don't tell me that you edited a core file... – kaiser Jan 10 '14 at 7:00
  • i just included the file that i created in wp-content/install.php in upgrade.php – Balas Jan 10 '14 at 7:10
  • That is editing a core file. – kaiser Jan 10 '14 at 7:15
  • Btw, +1 - I like the question itself. – kaiser Jan 10 '14 at 7:25
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.

  • thanks for your time. but the code doesnt work and got exit after wp installed. – Balas Jan 10 '14 at 7:38
  • First, this is example code. As stated I'm not sure if the $_POST data is correct (until you can link to WP core and confirm that). Second, you always need to exit() after a wp_redirect(). Third, please specify what "got exit" exactly means in your case. – kaiser Jan 10 '14 at 8:00
0

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.

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.