Im making a wordpress plugin which allows the admin delete a particular row in the database by clicking a button
<td>
<img src="/DeleteRed.png" onclick="deleteRow(<?php echo $rowa->id?>)"><br>
</td>
Initially I was calling ajax like this
function deleteRow(val) {
var url = "id="+val;
alert (url);
$.ajax(
{
type:'GET',
url:"../wp-content/plugins/salah-world/delete.php",
data:url,
success:function(data) {
console.log(data);
}
});
}
and using this PHP code
<?php
$id=$_GET["id"];
define('WP_USE_THEMES', false);
require_once( dirname(dirname(dirname(dirname(__FILE__)))) . '/wp-load.php');
echo $id."ID";
global $wpdb;
$table_name = $wpdb->prefix . 'iqamahTimes';
$wpdb->delete( $wpdb->prefix . 'iqamahTimes' , array( 'id' => $id ) );
?>
Which was working fine, returning the id as well as deleting it from the database. But when I submitted my plugin Wordpress said calling wp-load.php is big no no. Which I understand why.
I then try using ajaxurl and came up with this for the ajax
function deleteRow(val) {
var url = val;
alert (url);
$.ajax(
{
url:ajaxurl,
data: {
'action':'exampleajaxrequest',
'id' : url
},
success:function(data)
{
console.log(data);
//location.reload(true);
}
});
}
And the php is as followed
function exampleajaxrequest() {
$id = $_REQUEST['id'];
echo($id."id");
global $wpdb;
$table_name = $wpdb->prefix . 'iqamahTimes';
$wpdb->delete( $wpdb->prefix . 'iqamahTimes' , array( 'id' => '5' ) );
die();
}
I have put the add_action under the construct method as followed
public function __construct(){
add_action( 'wp_ajax_exampleajaxrequest', 'exampleajaxrequest' );
add_action( 'wp_ajax_nopriv_exampleajaxrequest', 'exampleajaxrequest' );
add_action("admin_menu", array($this,"add_plugin_menu_fnbar"));
add_action("admin_init", array($this,"register_dasettings"));
add_action('admin_enqueue_scripts', array($this, 'register_admin_scripts'));
}
However when I run it I get this error
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'exampleajaxrequest' not found or invalid function name in /home/he/public_html/sd.org/wp/wp-includes/plugin.php on line 525
0
When I place the add_Action method somewhere else I just get 0 and nothing happens to the row.
NOTE: This is a backend script.