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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀