$wpdb 在 WordPress 插件文件中不起作用 [英] $wpdb not working in file of WordPress plugin

查看:34
本文介绍了$wpdb 在 WordPress 插件文件中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 WordPress 插件.我创建了一个自定义表单,用户在其中添加值,然后单击提交按钮.当用户单击提交按钮时,它会重定向到自定义流程文件,我在其中编写用于插入和更新数据的查询.

I am working in WordPress plugin. I create a custom form where user add values and then click on submit button. When user click on submit button its redirect to custom process file, where i write queries for inserting and updating data.

我的 process.php 文件,首先我调用 global $wpdb,但是我的插入和更新查询不起作用,所以我从网络找到了一个解决方案来要求 config.php 文件在我的流程文件中.

I my process.php file, first i call global $wpdb, but my insert and update queries not working so i found a solution from net to require config.php file in my process file.

require_once( str_replace('//', '/', dirname(__FILE__) . '/') . '../../../wp-config.php');

然后全局 $wpdb 完美运行.但是 wordpress 存储库说这是非法的.我已经在插件主文件 index.php 中包含了我的 form.php 文件,所以我也包含了 process.php 并注释了 require_once 代码,但它不起作用.我不知道如何解决这个问题.

Then global $wpdb work perfectly. But the wordpress repository says that its illegal. I already include my form.php file in plugin main file index.php, so i also include process.php and comment the require_once code but its not working. I don't know how can i fixed this issue.

这是我的 form.php 代码:

Here is my form.php code:

<form method="post" action="<?php echo plugin_dir_url(__FILE__); ?>settings_process.php">

<input type="text" name="post_name_slug" id="post_name_slug" value="<?php echo POST_NAME_SLUG ?>" />

<input type="submit" name="save_settings" value="<?php _e("Save Changes","abc") ?>" />

这是我的 process.php 代码:

And here is my process.php code:

require_once( str_replace('//', '/', dirname(__FILE__) . '/') . '../../../wp-config.php');
    global $wpdb;

My insert and update queries

有没有其他解决方案,请帮帮我.

So is there any other solution for this, please help me.

非常感谢

推荐答案

使用 WP 操作可以解决您的问题.你的问题是你没有给 WP 机会去经历它的生命周期并加载所有必要的源.通过包含配置文件来调整它不是一个好方法.如果插件被封装在一个类中,你可以在插件加载或 __construct() 函数期间注册一个动作.为此,您可以使用多种操作,我主要使用这些操作:

Using WP actions can solve your issue. Your problem is that you don't give WP chance to go through it's lifecycle and load all neccessary sources. Tweaking it by including config file is not a good way to go. You can register an action during plugin loading or __construct() function if the plugin is encapsulated in a class. There are several actions you can use for this purpose, I'm mostly using these:

  • wp_ajax_{action_name} - 如果使用 AJAX
  • admin_post_{action_name} - 如果请求来自 wp-admin 中的登录用户
  • admin_post_nopriv_{action_name} - 如果有人可以执行此请求

代码很简单:

add_action('wp_ajax_mysettingsprocess', 'mySettingsProcess');
//If plugin "is object"
add_action('wp_ajax_mysettingsprocess', array($this, 'mySettingsProcess');

如果您的插件源中存在这行代码,它将尝试调用 function mySettingsProcess() { ... } 函数.在这个函数中,使用 $wpdb 全局变量没有问题.您还必须为请求提供适当的回复,WP 不会为您做这件事.

if this line of code is present in your plugin sources it will try to call function mySettingsProcess() { ... } function. Inside this function you will have no problem using $wpdb global var. You also have to provide a proper response for the request, WP will not do this for you.

表单操作将如下所示:

  • 对于admin_post_admin_post_nopriv_:http://{wp_url}/wp-admin/admin-post.php?action=mysettingsprocess
  • 如果使用 JS/JQuery 和 AJAX 添加到 data: { action: 'mysettingsprocess' },
  • for admin_post_ and admin_post_nopriv_: http://{wp_url}/wp-admin/admin-post.php?action=mysettingsprocess
  • if using JS/JQuery and AJAX add to data: { action: 'mysettingsprocess' },

WP Codex 中详细描述了所有这些信息.通常我会使用 WP 内置功能来启动任何插件自定义代码.添加位于 WP 之外的 PHP 源不是一个好习惯.

All these information are described in detail in WP Codex. Generally I would use WP built in functionality to launch any plugin custom code. Adding PHP sources living outside WP is not a good practice.

这篇关于$wpdb 在 WordPress 插件文件中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆