2

When we want post data from frontend to WORDPRESS everyone says i must use admin-ajax.php.
But i can add query vars and receive posted data in my plugin file using a global variable, and then i can do anything that i want with that.
This way everything becomes easier for me , but is it safe to receive and store posted data in plugin file ? and why all are using admin-ajax.php in their projects ?

0

First, everyone doesn't say that. Everyone says to use admin-ajax.php for ajax, which is it's meant for, not form submissions precisely. It's not as popular and well covered, but there is actually different endpoint for form submission — admin-post.php.

The reasons are, however, same. What you cannot do in your plugin is reliably load WordPress core. When WordPress core loads plugins it composes coherent reliable environment, but it doesn't work the other way around.

The point of using core–provided endpoints is precisely to rely on that environment.

3
  • Thank you for your answer. But first of all when i receive posted data in my plugin and store that in a global variable then i can for example hook into init and by using stored value in my global variable i can receive or even send data to ajax (because i can print sth and then use die()) . and second reason could be that maybe admin sets password on the wp-admin in cpanel. so i think receiving data directly in my plugin is really better and only thing that i don't know is that is it makes security issues or not ?
    – f.n174
    Dec 25 '14 at 19:49
  • @f.n174 you cannot hook into anything in a plugin because in general case plugin has no idea where WordPress core is. The building of custom endpoints is challenging and practically limited to private code. If you want to do it then do, but you aren't going to get answer other than it's likely just doing it wrong.
    – Rarst
    Dec 25 '14 at 20:14
  • Yes, what you say about that this is specific to every project is true, because it forced me to use some conditions in my code. but i think saying this is wrong is not true because it works fine and is really simpler than using what wordpress provided . anyway ! what you think about the security in this approach ?! or any other problem that maybe happens for users who uses my plugin ?
    – f.n174
    Dec 25 '14 at 20:54

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.