0

I'm developing my own theme and I currently work on a script that validates form data sends an email and puts the details into a second databse.

In the Codex I saw how to connect to a second database, but I was unable to find an answer to a way more important question. How can I access the wpdb class?

The script file is just a script, that outputs a json-encoded array which is needed for my ajax request, so I did not include any headers, footers or other parts that may import wordpress functions.

Locically when running my code, the script says, that the class wpdb is unknown. But I wonder how to include that class and all the other wordpress functions, classes, objects etc.

Thanks for your help. :)

PS: What could be interesting to say, is that I placed the file in a folder which I named inc. So it isn't in the main directory of the theme but one level up!

theme-directory > inc > file.php

0

never access theme or plugin files directly in their directory, this is a non secure way to develop code which will be blocked on any server with hardened security.

If you need to get information from the site you can either add an ajax access, or json end point in your code for that.

  • What exactly is the problem in adding a database connection to a script? I only whant to save the submitted data in my database. I could also do this by PDO, but as WordPress already has written this all down (SQL injection etc), I thought it would be more simple (to not reinvent the wheel) to use the wpdb class. – Sam Sep 16 '16 at 9:39
  • 1. it is a sign of bad code. 2.you don't know where wordpress is installed and therefor you can not botstrap it – Mark Kaplun Sep 16 '16 at 12:04
  • I really do not see your point. If i write an article that article is also saved to the database. Where's the difference in saving the data submitted by the user in a contact form? How would you save the data if you don't want to use the wpdb class? Should I write a new class with my own PDO connection and save the data with that class? I thought, that one should not repeat code, so why write own classes if they are already included in wordpress? – Sam Sep 16 '16 at 13:12
  • Thanks, but I know Ajax very well and my contact form also uses ajax for validation. But that's no answer to my question... What is wrong in saving data with the built in functions?? – Sam Sep 16 '16 at 14:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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