【问题标题】:Is there any way to access WordPress database in core php code有什么方法可以在核心 php 代码中访问 WordPress 数据库
【发布时间】:2019-11-05 03:00:21
【问题描述】:

我想要实现的目标:将数据存储到 PHP 数据库和 WordPress 数据库中

例如,有两个数据库,A 用于 corephp 站点,B 用于 wp 站点。现在,当用户通过 corephp 站点注册时,该条目是在数据库 A 中创建的,以及我应该如何在数据库 B 中创建相同的用户条目,这也是用于 WP 的,并且现在无论如何都没有连接到 corephp 站点。

连接这两个网站的原因是,我的 WP 网站显示了 woocommerce 订阅购买,我需要在 WP 数据库中拥有该用户数据,以查看哪个用户注册了哪个会员订阅。

更新代码:

$dir = getcwd().'/explore';
include $dir.'/wp-config.php';
include $dir.'/wp-load.php';
if ($f == 'register') {
    $register = Wo_RegisterUser($re_data, $in_code);
    if ($register === true) {
        wp_create_user($_POST['username'], $_POST['password'], $_POST['email']);
    }
}

【问题讨论】:

    标签: php mysql wordpress mariadb


    【解决方案1】:

    是的,你可以实现。您需要在核心 php 项目中包含“wp-config.php”和“wp-load.php”。之后,您需要调用 WordPress 函数“wp_create_user”在 WordPress 数据库中插入新的用户数据。

    了解更多详情。 https://developer.wordpress.org/reference/functions/wp_create_user/

    //include 'wp-config.php' and 'wp-load.php' in your core php project. Make sure you are passing the correct url of these two files.
    include '/wp-config.php';
    include '/wp-load.php';
    
    $user_id = username_exists( $user_name );
    
    if ( ! $user_id && false == email_exists( $user_email ) ) {
        $random_password = wp_generate_password( $length = 12, $include_standard_special_chars = false );
        $user_id = wp_create_user( $user_name, $random_password, $user_email );
    } else {
        $random_password = __( 'User already exists.  Password inherited.', 'textdomain' );
    }
    

    【讨论】:

    • 谢谢,我正在处理您给定的代码。我确实成功获得了 wordpress 路径。但是,在 Inspect element - network 我确实收到此错误 Status Code: 500 Internal Server Error when I add your given line $user_id = username_exists( $user_name );
    • 您是否使用 ajax 创建用户?你遇到了什么错误?
    • 它会起作用的。这是实际的过程。我尝试了同样的方法为我工作
    • 是的,我在用户注册页面上使用 ajax。现在我在我的 php 站点中使用 woowonder 脚本。用户在 php 数据库中注册良好,因为该代码是首先编写的。然后我添加下面的代码,它显示网络中的 500 Internal server error。 $register = Wo_RegisterUser($re_data, $in_code); if ($register === true) { $user_id = username_exists($_POST['username']); }
    • 您在哪里添加了该文件?在您发送ajax请求的文件中添加。您收到错误是因为未找到 username_exists 函数。只需检查您的 ajax 文件中包含的 wp-config.php 和 wp-load 文件是否正确。
    猜你喜欢
    • 2011-01-14
    • 1970-01-01
    • 2011-07-03
    • 1970-01-01
    • 2010-11-19
    • 2021-05-18
    • 2012-02-03
    • 2011-05-08
    • 1970-01-01
    相关资源
    最近更新 更多