John Davidson

php - Wordpress Plugin Classes and Ajax

0 comments
Message:


I'm in desperation, spent 2 days Googling and trying all kinds of ways to do this, but did not succeed.


What I am trying to do.


I want to move some functionalities from my theme to a plugin. I now have a custom page with some rewrite rules to allow for query vars to be passed to page itself (meaning showing different data dependent on passed var). I also have 4-5 Ajax calls running either on click, or on document ready. With moving to plugin one thing I would like to optimize is usage of those query vars in Ajax calls, also share some functions for each call. I would prefer to pass all the functionality from Ajax hooks to methods, not have any data retrieval and calculations inside Ajax functions.


So my general idea is to do this:



  • have a Class for plugin itself to create rewrite rules, enqueue scripts and styles.

  • have a Class for handling HTML templates

  • have a Class for handling Ajax calls / and probably data retrieval from db

  • or also have a separate Class for data retrieval and additional functionalities


Now I have to admit that I'm new to Classes in general and Plugins too.


What I have tried to do:



  1. Load plugin on 'init'

  2. Add rewrite rules, register query vars

  3. If page name matches - enqueue scripts

  4. Get query vars and set them as Class vars

  5. Load html template files as required


What I have problems with is managing Ajax calls. If I add Ajax hook in data retrieval Class, then that Class is launched on every Ajax call and I cant set Class vars. If I add Ajax hooks in separate Ajax Class, then I can't share vars (or I dont know how).


So I'm seeking help. How should I manage my Ajax calls to also be able to use Class vars inside their methods? Maybe I'm going from a wrong side to all of this with Classes?


I hope I made myself clear.



My Answer:
One way to approach this is to create a separate class specifically for handling Ajax calls within your plugin. This class can have methods for each individual Ajax call that you need to make, and you can pass any necessary data to these methods as parameters.

Here's an example of how you could structure your plugin classes:

1. Main Plugin Class:
php
class MyPlugin {
public function __construct() {
add_action('init', array($this, 'init'));
}

public function init() {
// Add rewrite rules, enqueue scripts, etc.
}
}


2. HTML Template Class:
php
class Template {
public function __construct() {
// Load template files as required
}
}


3. Data Retrieval Class:
php
class DataRetrieval {
public function __construct() {
// Initialize data retrieval methods
}

public function getData($query_vars) {
// Retrieve data from the database based on query vars
}
}


4. Ajax Class:
php
class Ajax {
public function __construct() {
add_action('wp_ajax_my_ajax_action', array($this, 'my_ajax_method'));
}

public function my_ajax_method() {
$data_retrieval = new DataRetrieval();
$query_vars = $_POST['query_vars'];

$data = $data_retrieval->getData($query_vars);

// Process data and return response
wp_send_json($data);
}
}


In this setup, the Ajax class is responsible for handling all Ajax calls within your plugin. When an Ajax call is made, it triggers the `my_ajax_method` method, which then uses the Data Retrieval class to retrieve the necessary data based on the query vars passed in the Ajax request.

By structuring your plugin in this way, you can easily share data between classes and keep your code organized and modular.

Rate this post

4 of 5 based on 8714 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us