【发布时间】:2016-02-22 14:03:12
【问题描述】:
我想在用户个人资料 > 封面图片页面上有一个按钮,上面写着“删除封面图片”。他们使用的主题对封面图像进行了模板覆盖,如下所示: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 并删除相应的文件或目录?
【问题讨论】:
标签: php wordpress buddypress