$wpdb not working in file of WordPress plugin

余生颓废 提交于 2019-12-11 03:35:49

问题


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.

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');

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.

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

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.

thanks alot


回答1:


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} - if using AJAX
  • admin_post_{action_name} - if request is from logged user in wp-admin
  • admin_post_nopriv_{action_name} - if anybody can do this request

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. 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.

Form actions will then look like this:

  • 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' },

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.



来源:https://stackoverflow.com/questions/32114812/wpdb-not-working-in-file-of-wordpress-plugin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!