dongshuo5101 2019-05-10 21:57
浏览 89

使用JQuery AJAX请求的WordPress codex错误

I have a link-button on my website to delete a user's "Item". This item translates into an entry in one table, a full separate table, and a WP post.

I'm using SweetAlert to get confirmation from the user to delete their item. This confirmation sends an AJAX request to an external PHP file to remove the table entry, drop the table, and delete the WP post, all without refreshing the page.

My code from the external PHP file works flawlessly when I paste it into a page on my site and post the variables to it, but when I send the values through an AJAX request it fails when it comes to delete the WP Post (using wp_delete_post()) and the only error I can see in the browser's developer tools is a 500 internal error.

Here's my code in question.

HTML

<span id="userId" hidden><?php echo $user_id; ?></span>
<span id="Name" hidden><?php echo $name; ?></span>
<span id="blogId" hidden><?php echo $blog_id; ?></span>
<a id="delete" class="btn-cancel" href="">Delete</a>

JQuery /var/www/html/wp-content/themes/custom/js/my.js

(function($){   
 $('document').ready(function(){
  $('#delete').click(function(event){
   event.preventDefault();

   swal ({
     icon: "warning",
     title: "Confirm Delete Request",
     text: "Are you sure you want to delete '" + $('#Name').html() + "'?
This action is not reversible and all associated data will be lost!",
     closeOnEsc: false,
     closeOnClickOutside: false,
     showCloseButton: false,
     buttons: {
       cancel: {
         text: "NO, Get me Out",
         value: false,
         visible: true,
       },
       confirm: {
         text: "YES, Delete! ME",
         value: true,
         visible: true,
       }
     },
    })
    .then(function(value) {
    if (value == true){ 
      var data = 'name=' + $('#Name').html() + '&user_id=' + $('#userId').html() + '&blog_id=' + $('#blogId').html();

      $.ajax({
        type: 'POST',
        url: '../wp-content/themes/onepress-child/includes/delete.php',
        data: data,
        dataType: 'json',
        success: function(r) {
          if (r=="2") {
             alert('Deleted');
          } else if (r=="1") {
             alert('Not an Admin');
          } else if (r=="0") {
             alert('Bad Name');
          } else {
             alert('Error');
          }
        }
      });               
    } else {
      window.history.back();
    }
 });            
});

PHP /var/www/html/wp-content/themes/custom/includes/delete.php

(I've left the MySQL code out because I don't have a problem with that

include_once "../../../../wp-includes/post.php"
if (wp_delete_post($blog_id, true)){
  echo "2"; //Success
}

Does anyone know why the wp_delete_post() function is failing? The 500 Internal Error does not give me any information for troubleshooting purposes.

I tried including the source file as described in the WP Codex (include_once "wp-includes/post.php";) but that is not the issue.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥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)