删除Buddypress封面图片按钮

时间:2016-02-22 14:03:13

标签: php wordpress buddypress

我想在用户个人资料>上设置一个按钮;封面图片页面简单地说“删除封面图片”。他们使用的主题是封面图像的模板覆盖,如下所示:http://pastebin.com/XQPpiVYs

我知道一旦用户上传封面图片,Buddypress就会有这个选项,然后上传后立即可以选择删除它,但是如何添加一个按钮,允许他们随时删除封面照片而不是直接删除封面照片上传后?

我知道我不想编辑核心,但我发现了对此负责的行为。有没有办法可以创建一个按钮来调用此操作并删除用户封面照片?

在buddypress / bp-core / bp-core-attachments.php中:

/**
 * Ajax delete a cover image for a given object and item id.
 *
 * @since 2.4.0
 *
 * @return string|null A json object containing success data if the cover image was deleted
 *                     error message otherwise.
 */
function bp_attachments_cover_image_ajax_delete() {
    // Bail if not a POST action.
    if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) ) {
        wp_send_json_error();
    }

    $cover_image_data = $_POST;

    if ( empty( $cover_image_data['object'] ) || empty( $cover_image_data['item_id'] ) ) {
        wp_send_json_error();
    }

    // Check the nonce
    check_admin_referer( 'bp_delete_cover_image', 'nonce' );

    // Capability check
    if ( ! bp_attachments_current_user_can( 'edit_cover_image', $cover_image_data ) ) {
        wp_send_json_error();
    }

    // Set object for the user's case
    if ( 'user' === $cover_image_data['object'] ) {
        $component = 'xprofile';
        $dir       = 'members';

    // Set it for any other cases
    } else {
        $component = $cover_image_data['object'] . 's';
        $dir       = $component;
    }

    // Handle delete
    if ( bp_attachments_delete_file( array( 'item_id' => $cover_image_data['item_id'], 'object_dir' => $dir, 'type' => 'cover-image' ) ) ) {

        // Defaults no cover image
        $response = array(
            'reset_url'     => '',
            'feedback_code' => 3 ,
        );

        // Get cover image settings in case there's a default header
        $cover_params = bp_attachments_get_cover_image_settings( $component );

        // Check if there's a default cover
        if ( ! empty( $cover_params['default_cover'] ) ) {
            $response['reset_url'] = $cover_params['default_cover'];
        }

        // Finally send the reset url
        wp_send_json_success( $response );

    } else {
        wp_send_json_error( array(
            'feedback_code' => 2,
        ) );
    }
}
add_action( 'wp_ajax_bp_cover_image_delete', 'bp_attachments_cover_image_ajax_delete' );

最后我知道封面图片通常存储在members / {id} / cover-image /文件夹中。更糟糕的情况是,当他们点击按钮时它只是抓取他们的用户ID并删除相应的文件或目录?

1 个答案:

答案 0 :(得分:0)

我开始了。它的工作原理可以根据需要进行修改和构建,但这对我有用:

<?php 
$ID = get_current_user_id();
if(isset($_GET['delete'])){
    $fileFull = 'coverphoto-full.jpg';
    $fileThumb = 'coverphoto-thumb.jpg';
    $upload_dir = wp_upload_dir();
    $user_dirname = $upload_dir['basedir'].'/avatars/'.$ID.'/';
    unlink($user_dirname . $fileFull);
    unlink($user_dirname . $fileThumb);
}  ?>
<a href="?delete=<?php echo $ID; ?>" onclick="return confirm('Are You Sure?')">Delete Cover Image</a>

注意:我有一个模板,所以我的封面图片存储在uploads/avatars/{ID}/coverphoto-full.jpg。对于普通BP用户,$user_dirname文件路径可能需要更改为buddypress/members/{ID}/cover-image/...或其中的某些变体。