Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class helloChamp{
- function __construct(){
- /*
- * Ajax Requests
- */
- $this->ajax_requests();
- }
- protected function ajax_requests(){
- /*
- * Send somethings
- */
- add_action('wp_ajax_save-something', 'save_something');
- //add_action('wp_ajax_nopriv_save-something', 'save_something');
- function save_something(){
- /*
- * Prevent data you don't want
- */
- $data = $_POST;
- $keys = [
- 'action',
- 'key',
- 'value',
- ];
- if(count($data) !== count($keys)) return;
- foreach($keys as $key){
- if(!isset($data[$key]) || !$data[$key] || empty($data[$key])){
- echo wp_json_encode([
- 'status' => 'error',
- 'message' => 'Your data doesn\'t respect the requirements.'
- ]);
- exit;
- }
- }
- /*
- * Now you sure you have the data you need, make sure the data is like your JS verifications here
- */
- $k = $data['key'];
- $v = $data['value'];
- /*
- * Do your verifications here...
- */
- $error = false;
- /*
- * Ok, verifications are good, save
- */
- if(!$error){
- update_field('field_key', wp_json_encode(
- [
- 'key' => $k,
- 'value' => $v
- ]
- ), $post_id);
- echo wp_json_encode([
- 'status' => 'success',
- 'message' => 'Your datas has been saved.'
- ]);
- } else {
- echo wp_json_encode([
- 'status' => 'error',
- 'message' => 'Your can\t be save.'
- ]);
- }
- exit;
- }
- }
- }
- new helloChamp();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement