0

Usually, In my wordpress website, when I modify template page file, it is done by only add global $wpdb for databse operation without including wp-config file.

Now, I create my own php file in public_html folder for database operation. For this to work, I should include wp-config.php file.

My question is should I add global $wpdb if I have already include wp-config.php file?

Thx

3
  • 1
    Whatever you are trying to do, you are most likely doing it wrong Commented Mar 4, 2016 at 14:56
  • Hello @MarkKaplun could you please assist me about how to do it in the right way?
    – assal0le
    Commented Mar 4, 2016 at 21:37
  • you should have moe details in your question, as it is it the only thing that is possible to understand from it is that you are trying to do something in non reccomended way Commented Mar 5, 2016 at 4:26

1 Answer 1

0

If you need a processing endpoint with access to the WordPress API, use the admin_post_ action and eliminate the need to manually include wp-config.php or any other WordPress files.

// add for logged-in visitors
add_action( 'admin_post_your_action', 'prefix_your_action_function' );
// for non logged-in visitors
add_action( 'admin_post_nopriv_your_action', 'prefix_your_action_function' );
function prefix_your_action_function() {
    global $wpdb;
    // your processing code here
}

Use admin_url to target the file in WordPress context, like:

echo admin_url( 'admin-post.php?action=your_action' );

You can GET or POST whatever additional data you need to pass.

2
  • Hello @Milo first of all, thanks for your fast answer. I'm sorry if you are confused about my question. I think this is the answer that I need: stackoverflow.com/questions/35798986/…
    – assal0le
    Commented Mar 4, 2016 at 21:45
  • 2
    I'm not confused about your question, you're doing something that is not recommended, I'm showing you an alternative.
    – Milo
    Commented Mar 4, 2016 at 22:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.