0

The situation is as: wordpress installation in root and ci installation in /subdomain1 of subdomain1.domain.com.

I want to perform the following; users from my wordpress site can login with the same credentials in the codeigniter app. I tried methods explained here and in other tutorials but one thing keeps happening. When I add require_once('../wp-load.php'); in the index.php file from ci it and adjusted the load.php file and MY_url_helper.php file it keeps redirecting to: subdomain1.domain.com/index.php/login/wp-admin/install.php I tried to shut off rewriting but it doesn't seem to fix this.

Anyone have a solution? I would really appreciate it!

3
  • If you want to use the same login infos from wordpress database, just load the database in codeigniter and you're good to go ( more info here: codeigniter.com/user_guide/database/connecting.html ) Commented Jan 4, 2017 at 13:00
  • Or have a look here: dovy.io/wordpress/… Commented Jan 4, 2017 at 13:08
  • Will look into that tomorrow looks very helpful!! Thanks .. Commented Jan 5, 2017 at 21:14

1 Answer 1

1

There are two methods:

1. Load the Wordpress Database in your Codeigniter

To do so add to your "application/config/database.php":

$db['wordpress'] = array(
    'dsn'   => '',
    'hostname' => 'localhost',
    'username' => '#',
    'password' => '#',
    'database' => '#',
    'dbdriver' => 'mysqli',
    'dbprefix' => '',
    'pconnect' => FALSE,
    'db_debug' => (ENVIRONMENT !== 'production'),
    'cache_on' => FALSE,
    'cachedir' => '',
    'char_set' => 'utf8',
    'dbcollat' => 'utf8_general_ci',
    'swap_pre' => '',
    'encrypt' => FALSE,
    'compress' => FALSE,
    'stricton' => FALSE,
    'failover' => array(),
    'save_queries' => TRUE
);

Don't forget to replace '#' with your database login information.

After that you can load the database where ever needed with

$this->load->database('wordpress');

Source: https://www.codeigniter.com/user_guide/database/connecting.html

2. Use the Wordpress wp-load.php

Where ever needed to see if the user is logged in use the following code (PS: at the end there is also a check included how you could check if a user purchased a product via EasyDigitalDownloads in your Wordpress installation - if needed):

<?php
    define( 'WP_USE_THEMES', false ); // Do not use the theme files
    define( 'COOKIE_DOMAIN', false ); // Do not append verify the domain to the cookie
    define( 'DISABLE_WP_CRON', true ); // We don't want extra things running...

    //$_SERVER['HTTP_HOST'] = ""; // For multi-site ONLY. Provide the 
                                  // URL/blog you want to auth to.

    // Path (absolute or relative) to where your WP core is running
    require("/var/www/yourdomain.com/htdocs/wp-load.php");

    if ( is_user_logged_in() ) {
        $user = wp_get_current_user();
    } else {
        $creds                  = array();
        // If you're not logged in, you should display a form or something
        // Use the submited information to populate the user_login & user_password
        $creds['user_login']    = "";
        $creds['user_password'] = "";
        $creds['remember']      = true;
        $user                   = wp_signon( $creds, false );
        if ( is_wp_error( $user ) ) {
            echo $user->get_error_message();
        } else {
            wp_set_auth_cookie( $user->ID, true );
        }
    }

    if ( !is_wp_error( $user ) ) {
        // Success! We're logged in! Now let's test against EDD's purchase of my "service."

        if ( edd_has_user_purchased( $user->ID, '294', NULL ) ) {
            echo "Purchased the Services and is active.";
        } else {
            echo "Not Purchased";
        }
    }

Source: http://dovy.io/wordpress/authenticating-outside-of-wordpress-on-diff-domain/

Sign up to request clarification or add additional context in comments.

1 Comment

Went for the second method but like earlier it keeps redirecting to wp-admin/install.php and get a 404 page not found. So the codeigniter app url is subdomain.domain.com/index.php/login and when I made the changes it refers to subdomain.domain.com/index.php/login/wp-admin/install.php

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.