【问题标题】:Deleting WordPress posts in front-end with AJAX使用 AJAX 在前端删除 WordPress 帖子
【发布时间】:2019-07-04 11:46:55
【问题描述】:

我正在尝试使用 AJAX 从前端删除 WordPress 帖子。 我的代码删除了帖子,但显示“成功”的空白页面,当我只想淡出该帖子而不重新加载页面并显示空白页面时。

PHP 代码:

<?php if( current_user_can( 'delete_post' ) ) : ?>
        <?php $nonce = wp_create_nonce('my_delete_post_nonce') ?>
        <a href="<?php echo admin_url( 'admin-ajax.php?action=my_delete_post&id=' . get_the_ID() . '&nonce=' . $nonce ) ?>" data-id="<?php the_ID() ?>" data-nonce="<?php echo $nonce ?>" class="delete-post">delete</a>
    <?php endif ?>

Functions.php 代码:

function my_frontend_script() {
wp_enqueue_script( 'my_script', get_template_directory_uri() . '/js/my_script.js', array( 'jquery' ), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'my_frontend_script' );
wp_localize_script( 'js/my_script.js', 'MyAjax2', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'ajaxnonce' => wp_create_nonce('ajax-nonce') ) );


add_action( 'wp_ajax_my_delete_post', 'my_delete_post' );
function my_delete_post(){

    $permission = check_ajax_referer( 'my_delete_post_nonce', 'nonce', false );
    if( $permission == false ) {
        echo 'error';
    }
    else {
        wp_delete_post( $_REQUEST['id'] );
        echo 'success';
    }

    die();
}

my_script.js 代码:

jQuery( document ).ready( function($) {
    $(document).on( 'click', '.delete-post', function() {
        var id = $(this).data('id');
        var nonce = $(this).data('nonce');
        var post = $(this).parents('.post:first');
        $.ajax({
            type: 'post',
            url: MyAjax2.ajaxurl,
            data: {
                action: 'my_delete_post',
                nonce: nonce,
                id: id
            },
            success: function( result ) {
                if( result == 'success7' ) {
                    post.fadeOut( function(){
                        post.remove();
                    });
                }
            }
        })
        return false;
    })
})

问题是页面正在重新加载到带有“成功”文本的空白页面,此时它应该只是淡出并从当前页面删除帖子,而不重新加载。

似乎根本没有使用 my_script.js :(

非常感谢任何帮助。

【问题讨论】:

    标签: javascript php jquery ajax wordpress


    【解决方案1】:
    <a href="<?php echo admin_url( 'admin-ajax.php?action=my_delete_post&id=' . get_the_ID() . '&nonce=' . $nonce ) ?>" data-id="<?php the_ID() ?>" data-nonce="<?php echo $nonce ?>" class="delete-post">delete</a>
    

    它正在重新加载,因为它首先加载您的 href 属性中的 url,然后执行您的 ajax 调用。那不是你想要的。您只想执行 onclick。这应该可以解决问题。只需在您的 href 中添加一个 #

    <a href=# data-id="<?php the_ID() ?>" data-nonce="<?php echo $nonce ?>" class="delete-post">delete</a>
    

    或制作按钮

    <button data-id="<?php the_ID() ?>" data-nonce="<?php echo $nonce ?>" class="delete-post">delete</button>
    

    编辑:如果它仍然无法正常工作,那是因为你在这里打错了

    if( result == 'success7' )
    

    success7 而不是成功

    【讨论】:

    • 不幸的是,使用 # 或制作按钮什么也没做;/ 点击链接或按钮后什么也没有发生...知道为什么吗?
    • 它应该可以工作。看这里:jsfiddle.net/k8y1ucor/2 另外,你得到一个带有“成功”的“空”页面的事实告诉我,通常你的 ajax 函数正在工作。我认为现在这只是一个错字: if( result == 'success7' ) success7 而不是 success
    猜你喜欢
    • 2015-09-20
    • 2012-11-05
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 1970-01-01
    • 2019-09-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多