通用API文档

💡 云策文档标注

概述

本文档提供了一个完整的 WordPress 前端删除文章示例,涵盖权限检查、数据验证、安全输入输出和非ce使用。通过函数和 Hook 实现,确保操作安全可靠。

关键要点

  • 使用 current_user_can 检查用户权限,仅允许具有 edit_others_posts 能力的用户执行删除操作。
  • 通过 wp_create_nonce 和 wp_verify_nonce 实现非ce验证,防止 CSRF 攻击。
  • 使用 esc_url 和 esc_html__ 进行安全输出,避免 XSS 漏洞。
  • 结合 add_query_arg 构建 URL,并通过 init Hook 注册请求处理函数。
  • 在删除前验证文章 ID 和文章存在性,使用 wp_trash_post 安全删除文章。

代码示例

function wporg_generate_delete_link( $content ) {
    if ( is_single() && in_the_loop() && is_main_query() ) {
        $url = add_query_arg(
            [
                'action' => 'wporg_frontend_delete',
                'post'   => get_the_ID(),
                'nonce'  => wp_create_nonce( 'wporg_frontend_delete' ),
            ], home_url()
        );
        return $content . ' <a href="' . esc_url( $url ) . '">' . esc_html__( 'Delete Post', 'wporg' ) . '</a>';
    }
    return null;
}

function wporg_delete_post() {
    if ( isset( $_GET['action'] )
         && isset( $_GET['nonce'] )
         && 'wporg_frontend_delete' === $_GET['action']
         && wp_verify_nonce( $_GET['nonce'], 'wporg_frontend_delete' ) ) {
        $post_id = ( isset( $_GET['post'] ) ) ? ( $_GET['post'] ) : ( null );
        $post = get_post( (int) $post_id );
        if ( empty( $post ) ) {
            return;
        }
        wp_trash_post( $post_id );
        $redirect = admin_url( 'edit.php' );
        wp_safe_redirect( $redirect );
        die;
    }
}

function wporg_add_delete_post_ability() {    
    if ( current_user_can( 'edit_others_posts' ) ) {
        add_filter( 'the_content', 'wporg_generate_delete_link' );
        add_action( 'init', 'wporg_delete_post' );
    }
}
add_action('plugins_loaded', 'wporg_add_delete_post_ability');

注意事项

  • 确保仅在单篇文章页面和主查询中生成删除链接,避免误操作。
  • 使用 wp_safe_redirect 进行安全重定向,防止开放重定向漏洞。
  • 在删除后调用 die 终止脚本执行,确保重定向生效。

📄 原文内容

Complete example using capability checks, data validation, secure input, secure output and nonces:

/**
 * Generate a Delete link based on the homepage url.
 *
 * @param string $content   Existing content.
 *
 * @return string|null
 */
function wporg_generate_delete_link( $content ) {
	// Run only for single post page.
	if ( is_single() && in_the_loop() && is_main_query() ) {
		// Add query arguments: action, post, nonce
		$url = add_query_arg(
			[
				'action' => 'wporg_frontend_delete',
				'post'   => get_the_ID(),
				'nonce'  => wp_create_nonce( 'wporg_frontend_delete' ),
			], home_url()
		);

		return $content . ' <a href="' . esc_url( $url ) . '">' . esc_html__( 'Delete Post', 'wporg' ) . '</a>';
	}

	return null;
}


/**
 * Request handler
 */
function wporg_delete_post() {
	if ( isset( $_GET['action'] )
         && isset( $_GET['nonce'] )
         && 'wporg_frontend_delete' === $_GET['action']
         && wp_verify_nonce( $_GET['nonce'], 'wporg_frontend_delete' ) ) {

		// Verify we have a post id.
		$post_id = ( isset( $_GET['post'] ) ) ? ( $_GET['post'] ) : ( null );

		// Verify there is a post with such a number.
		$post = get_post( (int) $post_id );
		if ( empty( $post ) ) {
			return;
		}

		// Delete the post.
		wp_trash_post( $post_id );

		// Redirect to admin page.
		$redirect = admin_url( 'edit.php' );
		wp_safe_redirect( $redirect );

		// We are done.
		die;
	}
}


/**
 * Add delete post ability
 */
add_action('plugins_loaded', 'wporg_add_delete_post_ability');
 
function wporg_add_delete_post_ability() {    
    if ( current_user_can( 'edit_others_posts' ) ) {
        /**
         * Add the delete link to the end of the post content.
         */
        add_filter( 'the_content', 'wporg_generate_delete_link' );
      
        /**
         * Register our request handler with the init hook.
         */
        add_action( 'init', 'wporg_delete_post' );
    }
}