$ wpdb does not work in WordPress plugin file

I work in a WordPress plugin. I create a custom form where the user adds values ​​and then clicks the submit button. When the user clicks the Submit button, he is redirected to the user process file, where I write requests for inserting and updating data.

I got my process.php file, first I call the global $ wpdb , but my insert and update requests do not work, so I found a solution from the network that requires config.php in the process file.

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

Then global $ wpdb works fine. But the wordpress repository says it's illegal. I already include the form.php file in the main index.php plugin file, so I also include process.php and comment on the require_once code, but it doesn’t work. I do not know how I can fix this problem.

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

+2
source share
1 answer

Using WP actions may solve your problem. Your problem is that you are not giving WP the opportunity to go through this life cycle and download all the necessary sources. Fine tuning using a configuration file is not a good way. You can register an action during plugin loading or __construct() if the plugin is encapsulated in a class. There are several actions you can use for this purpose, I mainly use the following:

  • wp_ajax_ {action_name} - when using AJAX
  • admin_post_ {action_name} - if the request is from a registered user in wp-admin
  • admin_post_nopriv_ {action_name} - if anyone can fulfill this request

Then the code is 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 the function mySettingsProcess() { ... } . Inside this function you will not have problems using $wpdb global var. You must also provide the correct answer for the request, WP will not do this for you.

Form actions will look like this:

  • for admin_post_ and admin_post_nopriv_ : http: // {wp_url} /wp-admin/admin-post.php?action=mysettingsprocess
  • when using JS / JQuery and AJAX, add to data: { action: 'mysettingsprocess' },

All this information is described in detail in WP Codex . Typically, I would use WP's built-in functionality to run any custom plugin code. Adding PHP sources living outside of WP is not good practice.

+3
source

All Articles