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 oled显示有问题,初始化后应该啥也不显示,但却亮了一大片
    • ¥15 【通信原理】为什么传信率不变?频带利用率为啥没有二倍
    • ¥15 CANOPEN SDO
    • ¥15 r语言数据集循环获取问题
    • ¥30 求佬们帮助,总是出bug,求佬们解决一下bug
    • ¥15 后端Java转换字符串传给前端,前端如何解析呢?
    • ¥15 010editor导入文件后一直是只读
    • ¥15 psychopy(python为基础的)中引入cmd
    • ¥15 不知道怎么去做关于前端电子请柬
    • ¥15 Ubuntu22.04打开是tty界面。提示OOM