简体   繁体   English

$ wpdb在WordPress插件文件中不起作用

[英]$wpdb not working in file of WordPress plugin

I am working in WordPress plugin. 我正在使用WordPress插件。 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. 当用户单击“提交”按钮时,其重定向到自定义过程文件,我在其中编写用于插入和更新数据的查询。

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. 我是我的process.php文件,首先我调用全局$ wpdb ,但是我的插入和更新查询不起作用,因此我从net找到了一个解决方案,要求在我的过程文件中使用config.php文件。

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

Then global $wpdb work perfectly. 然后, 全局$ wpdb可以完美工作。 But the wordpress repository says that its illegal. 但是wordpress存储库说这是非法的。 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. 我已经在插件主文件index.php中包含了form.php文件,因此我也包含了process.php并注释了require_once代码,但它不起作用。 I don't know how can i fixed this issue. 我不知道该如何解决这个问题。

Here is my form.php code: 这是我的form.php代码:

<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") ?>" />

And here is my process.php code: 这是我的process.php代码:

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. 因此,还有其他解决方案,请帮助我。

thanks alot 非常感谢

Using WP actions can solve your issue. 使用WP动作可以解决您的问题。 Your problem is that you don't give WP chance to go through it's lifecycle and load all neccessary sources. 您的问题是您没有给WP机会经历它的生命周期并加载所有必要的资源。 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. 如果插件封装在类中,则可以在插件加载期间注册动作,也可以在__construct()函数中注册动作。 There are several actions you can use for this purpose, I'm mostly using these: 您可以为此使用几种操作,我主要使用这些操作:

  • wp_ajax_{action_name} - if using AJAX wp_ajax_ {action_name}-如果使用AJAX
  • admin_post_{action_name} - if request is from logged user in wp-admin admin_post_ {action_name}-如果请求来自wp-admin中的登录用户
  • admin_post_nopriv_{action_name} - if anybody can do this request admin_post_nopriv_ {action_name}-如果有人可以执行此请求

The code is then simple: 代码很简单:

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

if this line of code is present in your plugin sources it will try to call function mySettingsProcess() { ... } function. 如果您的插件源代码中包含此行代码,它将尝试调用function mySettingsProcess() { ... }函数。 Inside this function you will have no problem using $wpdb global var. 在此函数内部,使用$wpdb全局变量将没有问题。 You also have to provide a proper response for the request, WP will not do this for you. 您还必须对请求提供适当的响应,WP不会为您这样做。

Form actions will then look like this: 表单操作将如下所示:

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

All these information are described in detail in WP Codex . 所有这些信息在WP Codex中都有详细描述。 Generally I would use WP built in functionality to launch any plugin custom code. 通常,我会使用内置的WP功能来启动任何插件自定义代码。 Adding PHP sources living outside WP is not a good practice. 将PHP源代码添加到WP之外并不是一个好习惯。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM