Robbin Janssen
Robbin Janssen

Reputation: 1

Connection codeigniter and wordpress

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!

Upvotes: 0

Views: 1866

Answers (1)

Smort
Smort

Reputation: 428

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/

Upvotes: 1

Related Questions