Setting up Server-Side processing on a WordPress site
Setting up Server-Side processing on a WordPress site
Hello,
I am trying to use DataTables Editor in a WordPress plugin I am writing, and need a little help to get it to work.
My work was divided into 2 stages.
1. I was testing if the client side was working well. I succeeded in loading the libraries, using jQuery to load the table, and even populate it using my own written AJAX function that returned rows. Double-Clicking on a cell enabled me to do inline-editing. (without committing the data).
- After verifying this worked I moved to implement the Server-Side. I updated the database details in the config.php file, and initialized the Editor instance.
This time, I don't manage to even populate the table.
Since AJAX actions are defined in the WordPress method by a hook, I suppose this requires a different approach than what is shown in your examples.
My code is:
Client Side:
editor = new jQuery.fn.dataTable.Editor( {
ajax: "/wp-admin/admin-ajax.php?action=ajax_table_php",
table: "#ajaxtest",
idSrc: 'id',
fields: [ {
label: "First name:",
name: "id"
}, {
label: "Last name:",
name: "name"
}, {
label: "Position:",
name: "deleted"
},
]
} );
jQuery('#ajaxtest').DataTable( {
dom: "Bfrtip",
ajax: "/wp-admin/admin-ajax.php?action=ajax_table_php",
columns: [
{ data: "id" },
{ data: "name" },
],
select: true,
buttons: [
{ extend: "create", editor: editor },
{ extend: "edit", editor: editor },
{ extend: "remove", editor: editor }
]
} );
Server-Side:
//The following function runs when an AJAX call is made either from the Editor or from the DataTable object.
function ajax_table_php() {
Editor::inst( $db, 'mea_departments' )
->fields(
Field::inst( 'id' ),
Field::inst( 'name' ),
Field::inst( 'deleted' )
)
->process( $_POST )
->json();
}
The page address is: http://app.meafood.org.il/table/
the debug code is : 'owawic'
Could you please help ?
Thank you,
Yaron
This question has an accepted answers - jump to answer
Answers
Okay, got it to work.
Best to set up the REST API on the top directory without using any WordPress wp-admin AJAX.
Hi,
Thanks for your update - great to hear that you've been able to get it working!
It should be quite possible to use
admin-ajax.php
with a custom function. Its been a while since I've used WordPress, but I think they get registered usingadd_action
and then put intocustom.php
(or anywhere else that is included).The one major advantage of doing that would be that you get the benefit of any security that WordPress has installed and you could potentially jack into their authentication / permissions structure if you needed to.
On the other hand, doing it as you have done means that the install is a lot less tricky!
One quick comment about Editor and WordPress - Editor core can't be redistributed as part of other software as it itself is not open source. Any developer installing Editor needs their own license.
Regards,
Allan