weixin_33698043 2016-03-31 15:51 采纳率: 0%
浏览 4

Ajaxurl返回0

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.

  • 写回答

1条回答 默认 最新

  • weixin_33713707 2016-03-31 19:09
    关注

    I'm speculating here but is the function exampleajaxrequest actually a method within the plugin class?

    If so you need to queue it up like this:

    add_action( 'wp_ajax_exampleajaxrequest', array($this,'exampleajaxrequest') );
    add_action( 'wp_ajax_nopriv_exampleajaxrequest', array($this,'exampleajaxrequest') );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)