• Resolved islp

    (@islp)


    Hello, I developed a JavaScript single page application and I embedded it in a WP page.

    When the user has finished using this application he sends data to the server using simple JS.

    At the moment it works but, after reading some post here and there, I found maybe I conceived this thing the wrong way.

    That is: currently, I send data using a PHP file that’s NOT part of WordPress (either theme, plugin or core). This thing is probably not a good idea.

    I red something about wp_nonce and its validation but I could not understand how the entire sending process would work.

    I know how to get wp_nonce but I don’t know how and which file should validate wp_nonce (phpfunctions.php?) and how to connect my request to that file/function.

    Can you help me?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello islp!

    I also write single page applications using Angular 4 as the front-end framework and WordPress’s REST API as a backend. I am familiar with working with wp_nonce and getting the nonce. I usually pass the wp_nonce as a X-WP-Nonce header. If you’re using the WordPress REST API, WordPress should handle processing the nonce for you, especially if you’re using WordPress functions like current_user_can() and is_user_logged_in(). If your JavaScript single page application is making HTTP requests when submitting the user’s data, I recommend considering using a custom REST API endpoint instead of sending the request to a PHP file. That would make your application more portable and WordPress-compatible. You probably could reuse all of your PHP code by calling the functions or methods it defines from a WordPress REST API Controller class.

    The WordPress Codex has documentation on the REST API here:
    https://developer.www.ads-software.com/rest-api/

    If it helps, I have a boilerplate/skeleton project I wrote that implements these concepts with Angular 4 as the single page application and with WordPress’s REST API as a backend:
    https://github.com/jadonn/ng-wp-plugin-skeleton

    That project is for a single page application plugin that is embedded in the WordPress admin dashboard as a new admin dashboard page. It’s pretty basic, but I think it demonstrates some of the things you’re trying to do, like sending requests with a nonce from a single page application to WordPress for data and for processing.

    I hope this helps. I’m happy to try to help with more information if you have more questions.

    Thread Starter islp

    (@islp)

    Thanks, very interesting, I was reading this old post and found some more infos: https://solislab.com/blog/5-tips-for-using-ajax-in-wordpress/

    Anyway, a REST API would be more interesting. I didn’t use Angular, I’m exploring Vue, Angular was a little “too much” for what I was going to do, and Vue scales quite well. ??

    Cool! Thanks for the link. That’s an interesting find. I haven’t used admin-ajax.php before for handling AJAX requests. I went with the REST API because I have previously written REST APIs using the Express JavaScript MVC framework and the PHP Zend Framework MVC framework. I also think the REST API stuff is going to be the way WordPress goes in the future after seeing Calypso and Gutenberg, which both heavily use Facebook’s React library and the WordPress REST API.

    I totally understand Angular being wayyyy too much sometimes. I’ve looked at Vue a little bit, and I agree it can be a much better fit than Angular. I’ve used React, too, and found it is much more lightweight than Angular and more convenient when I just want to do a single task with a JavaScript app.

    Thread Starter islp

    (@islp)

    The right tool totally depends on your needs (what you must do now and how could one day grow your project), and your skills too.

    I come from a RIA background, now I’m a mobile developer (mainly Obj-C/Swift), I’m quite proficient with JS and PHP (I’m looking into Laravel this days) but absolutely not very good at WP, and sometimes I found myself using a lot personal insight, experience and senses I didn’t suspect I had ?? In SPAs, I played a little with EmberJS (very good, but really “big”, and you must find the right project) ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Send data from a WP page using AJAX’ is closed to new replies.