Advertisement
Dexlogi22

Ajax

Oct 1st, 2022
779
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.66 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * The plugin bootstrap file
  5.  *
  6.  * This file is read by WordPress to generate the plugin information in the plugin
  7.  * Dashboard. This file defines a function that starts the plugin.
  8.  *
  9.  * @wordpress-plugin
  10.  * Plugin Name:         Oop Ajax
  11.  * Plugin URI:          http://
  12.  * Description:         A simple example of using OOP principles to submit a form from the front end.
  13.  * Version:             1.0.0
  14.  * Author:              Digvijay Naruka
  15.  * Author URI:          http://
  16.  * License:             GPL-2.0+
  17.  * License URI:         http://www.gnu.org/licenses/gpl-2.0.txt
  18.  * Text Domain:         oop-ajax
  19.  * Domain Path:         /languages
  20.  */
  21.  
  22. // If this file is called directly, abort.
  23. if ( ! defined( 'WPINC' ) ) {
  24.     die;
  25. }
  26.  
  27. class Oop_Ajax {
  28.  
  29.   // Put all your add_action, add_shortcode, add_filter functions in __construct()
  30.   // For the callback name, use this: array($this,'<function name>')
  31.   // <function name> is the name of the function within this class, so need not be globally unique
  32.   // Some sample commonly used functions are included below
  33.     public function __construct() {
  34.    
  35.         // Add Javascript and CSS for front-end display
  36.         add_action('wp_enqueue_scripts', array($this,'enqueue'));
  37.  
  38.         // Add the shortcode for front-end form display
  39.         add_action( 'init', array( $this, 'add_form_shortcode' ) );
  40.         // Add ajax function that will receive the call back for logged in users
  41.         add_action( 'wp_ajax_my_action', array( $this, 'my_action_callback') );
  42.         // Add ajax function that will receive the call back for guest or not logged in users
  43.           //Here you can see the Action is fired by appending wp_ajax_ to the requested action query parameter value my_action. Programmatically, it is 'wp_ajax_' . $_REQUEST['action']. Attaching a function with this hook, responses are sent to browser or to the ajax call.
  44.         add_action( 'wp_ajax_nopriv_my_action', array( $this, 'my_action_callback') );
  45. // 'my_action_callback' is a function you use to do some processing and/or sending some output for the request.
  46.    
  47.     }
  48.  
  49.     // This is an example of enqueuing a JavaScript file and a CSS file for use on the front end display
  50.     public function enqueue() {
  51.         // Actual enqueues, note the files are in the js and css folders
  52.         // For scripts, make sure you are including the relevant dependencies (jquery in this case)
  53.         wp_enqueue_script('my-ajax-script', plugins_url('js/oop-ajax.js', __FILE__), array('jquery'), '1.0', true);
  54.  
  55.         // Sometimes you want to have access to data on the front end in your Javascript file
  56.         // Getting that requires this call. Always go ahead and include ajaxurl. Any other variables,
  57.         // add to the array.
  58.         // Then in the Javascript file, you can refer to it like this: my_php_variables.ajaxurl
  59.         wp_localize_script( 'my-ajax-script', 'my_php_variables', array(
  60.             'ajaxurl' => admin_url('admin-ajax.php'),
  61.             'nonce' => wp_create_nonce('_wpnonce')
  62.         ));
  63.  
  64.     }
  65.  
  66.     /**
  67.      * Registers the shortcode for the form.
  68.      */
  69.     public function add_form_shortcode() {
  70.  
  71.         add_shortcode( "oop-ajax-add-form", array( $this, "add_form_front_end" ) );
  72.  
  73.     }
  74.  
  75.     /**
  76.      * Processes shortcode oop-ajax-add-form
  77.      *
  78.      * @param   array    $atts        The attributes from the shortcode
  79.      *
  80.      * @return    mixed    $output        Output of the buffer
  81.      */
  82.     function add_form_front_end($atts, $content) {
  83.  
  84.         echo "<form id='my_form'>";
  85.  
  86.             echo "<label for='name'>Name: </label>";
  87.             echo "<input id='name' type='text' name='name' ";
  88.  
  89.             echo "<br>" ;
  90.  
  91.             echo "<label id='email' for='email'>Email: </label>" ;
  92.             echo "<input type='text' name='email'>";
  93.  
  94.             echo "<br>" ;
  95.  
  96.             echo "<input type='hidden' name='action' value='my_action' >" ;
  97.             echo "<input id='submit_btn' type='submit' name='submit' value='submit'> ";
  98.  
  99.         echo "</form><br><br>";
  100.         echo "<div id='response'>ajax responce will be here</div> ";
  101.     }
  102.    
  103.      /**
  104.      * Callback function for the my_action used in the form.
  105.      *
  106.      * Processses the data recieved from the form, and you can do whatever you want with it.
  107.      *
  108.      * @return    echo   response string about the completion of the ajax call.
  109.      */
  110.     function my_action_callback() {
  111.         // echo wp_die('<pre>' . print_r($_REQUEST) . "<pre>");
  112.  
  113.         check_ajax_referer( '_wpnonce', 'security');
  114.  
  115.         if( ! empty( $_POST )){
  116.  
  117.             if ( isset( $_POST['name'] ) ) {
  118.  
  119.                 $name = sanitize_text_field( $_POST['name'] ) ;
  120.             }
  121.  
  122.             if( isset( $_POST['email'] ) ) {
  123.  
  124.                 $email = sanitize_text_field( $_POST['email'] ) ;
  125.             }
  126.  
  127.             ///////////////////////////////////////////
  128.             // do stuff with values
  129.             // example : validate and save in database
  130.             //          process and output
  131.             ///////////////////////////////////////////
  132.  
  133.             $response = "Wow <strong style= 'color:red'>". $name . "!</style></strong> you rock, you just made ajax work with oop.";
  134.             //this will send data back to the js function:
  135.             echo $response;
  136.  
  137.         } else {
  138.  
  139.             echo "Uh oh! It seems I didn't eat today";
  140.         }
  141.  
  142.         wp_die(); // required to terminate the call so, otherwise wordpress initiates the termination and outputs weird '0' at the end.
  143.  
  144.     }
  145.  
  146. }
  147. //initialize our plugin
  148. global $plugin;
  149.  
  150. // Create an instance of our class to kick off the whole thing
  151. $plugin = new Oop_Ajax();
  152.  
  153.  
  154.  
  155.  
Tags: ajax
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement