• TrisectDevelopment

    (@trisectdevelopment)


    I’m working with custom pages in WP and I have beeb making a few pages.
    In the database I have created a new table for profiles.

    I have made a custom PHP page where you can create a new profile. At the same time it creates a new WP user.

    I have also made a custom PHP page where you can login to the system (new table).

    Here in this file I want to also let the user login to WP, but here I have failed so far.

    Heres the code I have tried:

    $creds = array(
            'user_login'    => 'username',
            'user_password' => 'password',
            'rememember'    => true
        );
        $user = wp_signon( $creds, true )

    When I then go to /wp-admin page is okay, but I have an forum page that only WP users can se and this page I’m not allowed to visit.

    If I login to WP with the same credential the normal way (wp-login) I can see the forum fine.

    So my question is simply:

    How do I login to a WP site in PHP code?
    After login I want to go the the main page of the site.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter TrisectDevelopment

    (@trisectdevelopment)

    If I run this code after the login

    $current_user = wp_get_current_user();
    
            echo 'Username: ' . $current_user->user_login . '<br />';
    	echo 'User email: ' . $current_user->user_email . '<br />';
    	echo 'User first name: ' . $current_user->user_firstname . '<br />';
    	echo 'User last name: ' . $current_user->user_lastname . '<br />';
    	echo 'User display name: ' . $current_user->display_name . '<br />';
    	echo 'User ID: ' . $current_user->ID . '<br />';

    It show that I’m logged int, but when I try to open the forum page it says that I’m not logged in.

    Thread Starter TrisectDevelopment

    (@trisectdevelopment)

    Really!
    Nobody?

    Moderator bcworkz

    (@bcworkz)

    I take it you are calling wp_signon() from your page template? If so, I’m surprised it works in any context. Calling from a page template should be too late, as the page headers that set the auth cookie have already been sent. There’s a small window where it’s appropriate to call wp_signon(). Too soon and the user functions are not set up and too late, the headers have already been sent. One action that fires at the proper time is ‘after_setup_theme’.
    https://codex.www.ads-software.com/Function_Reference/wp_signon#Examples

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Login to WP from PHP’ is closed to new replies.