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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵