0
votes

I am trying to have a button on the users Profile > Cover Image page that says simply "Remove Cover Image". The theme they are using does a template override of the cover image as seen here: http://pastebin.com/XQPpiVYs

I know Buddypress has this option once a user has uploaded a cover image, then the option to delete it is available immediately after upload but how can I add a button in that allows them to delete the cover photo at anytime instead of directly after upload?

I know I dont want to edit the core but I found the action responsible for this. Is there a way I can create a button that will call this action and remove the users cover photo?

in 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' );

and lastly I know that cover images are usually stored in the members/{id}/cover-image/ folder, Worse case scenario is when they click the button it just grabs their user ID and deletes the appropriate file or directory?

1

1 Answers

0
votes

I got this started. It works and can be modified and built to suit but this is what worked for me:

<?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>

Note: I have a template over ride so my cover images are stored in uploads/avatars/{ID}/coverphoto-full.jpg. For the average BP user the $user_dirname filepath may need to change to buddypress/members/{ID}/cover-image/... or some variant of that.