【问题标题】:Connection codeigniter and wordpress连接codeigniter和wordpress
【发布时间】:2017-05-18 17:48:16
【问题描述】:

情况为:wordpress在root安装,ci安装在subdomain1.domain.com的/subdomain1。

我想执行以下操作;我的 wordpress 网站的用户可以在 codeigniter 应用程序中使用相同的凭据登录。我尝试了这里和其他教程中解释的方法,但一件事一直在发生。当我添加 require_once('../wp-load.php');在 ci 的 index.php 文件中,它调整了 load.php 文件和 MY_url_helper.php 文件,它一直重定向到:subdomain1.domain.com/index.php/login/wp-admin/install.php 我试图关闭重写,但似乎无法解决此问题。

有人有解决办法吗?我真的很感激!

【问题讨论】:

标签: wordpress codeigniter


【解决方案1】:

有两种方法:

1.在您的 Codeigniter 中加载 Wordpress 数据库

为此添加到您的“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
);

不要忘记将'#' 替换为您的数据库登录信息。

之后,您可以在任何需要的地方加载数据库

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

来源:https://www.codeigniter.com/user_guide/database/connecting.html

2。使用 Wordpress wp-load.php

如果需要查看用户是否已登录,请使用以下代码(PS:最后还有一个检查,包括如何检查用户是否在您的 Wordpress 安装中通过 EasyDigitalDownloads 购买了产品 - 如果需要) :

<?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";
        }
    }

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

【讨论】:

  • 采用了第二种方法,但像之前一样,它不断重定向到 wp-admin/install.php 并得到一个 404 页面未找到。因此,codeigniter 应用程序 url 是 subdomain.domain.com/index.php/login,当我进行更改时,它指的是 subdomain.domain.com/index.php/login/wp-admin/install.php
猜你喜欢
  • 2015-06-20
  • 1970-01-01
  • 1970-01-01
  • 2020-12-08
  • 1970-01-01
  • 1970-01-01
  • 2020-02-28
  • 1970-01-01
相关资源
最近更新 更多