如何使用ajax在WordPress后台删除文章?直接上代码,将下面的代码添加到当前启用的WordPress主题的functions.php里面。

// 后台 文章列表 ajax删除文章

add_action( \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’admin_footer\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’dahuzi_custom_internal_javascript\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ );

function dahuzi_custom_internal_javascript(){

echo \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”<script>

jQuery(function($){

$(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’body.post-type-post .row-actions .trash a\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’).click(function( event ){

event.preventDefault();

var url = new URL( $(this).attr(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’href\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’) ),

nonce = url.searchParams.get(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’_wpnonce\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’), // MUST for security checks

row = $(this).closest(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’tr\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’),

postID = url.searchParams.get(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’post\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’),

postTitle = row.find(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’.row-title\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’).text();

row.css(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’background-color\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’#ffafaf\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’).fadeOut(300, function(){

row.removeAttr(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’style\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’).html(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'<td colspan=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’5\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ style=\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’background:#fff;border-left:1px solid #FF5722;border-left-width:4px;color:#555\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’><strong>\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ + postTitle + \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'</strong> 已被移动到回收站</td>\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’).show();

});

$.ajax({

method:\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’POST\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,

url: ajaxurl,

data: {

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’action\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ : \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’moveposttotrash\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’,

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’post_id\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ : postID,

\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’_wpnonce\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ : nonce

}

});

});

});

</script>\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\”;

}

add_action(\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’wp_ajax_moveposttotrash\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’, function(){

check_ajax_referer( \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’trash-post_\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’ . $_POST[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’post_id\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’] );

wp_trash_post( $_POST[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’post_id\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’] );

die();

});

如果你想将这个功能用于自定义文章分类,将代码中的 .post-type-post,替换成 .post-type-{post-type-name}。

以上就是小编为大家分享的使用ajax在WordPress后台删除文章。

相关新闻

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

联系我们

联系我们

151-1101-0018

在线咨询:
邮件:30115776@qq.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信
关注微信
分享本页
返回顶部