Custom_Image_Header::step_3() WordPress Method

The Custom_Image_Header::step_3() method is responsible for displaying the final form for setting up a custom image header. This is the last step in the process, and it allows the user to select an image and crop it to their liking. Once the image is selected, the user can then click the "Save Changes" button to save their settings.

Custom_Image_Header::step_3() #

Display third step of custom header image page.


Source

File: wp-admin/includes/class-custom-image-header.php

998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
public function step_3() {
    check_admin_referer( 'custom-header-crop-image' );
 
    if ( ! current_theme_supports( 'custom-header', 'uploads' ) ) {
        wp_die(
            '<h1>' . __( 'Something went wrong.' ) . '</h1>' .
            '<p>' . __( 'The active theme does not support uploading a custom header image.' ) . '</p>',
            403
        );
    }
 
    if ( ! empty( $_POST['skip-cropping'] )
        && ! current_theme_supports( 'custom-header', 'flex-height' )
        && ! current_theme_supports( 'custom-header', 'flex-width' )
    ) {
        wp_die(
            '<h1>' . __( 'Something went wrong.' ) . '</h1>' .
            '<p>' . __( 'The active theme does not support a flexible sized header image.' ) . '</p>',
            403
        );
    }
 
    if ( $_POST['oitar'] > 1 ) {
        $_POST['x1']     = $_POST['x1'] * $_POST['oitar'];
        $_POST['y1']     = $_POST['y1'] * $_POST['oitar'];
        $_POST['width']  = $_POST['width'] * $_POST['oitar'];
        $_POST['height'] = $_POST['height'] * $_POST['oitar'];
    }
 
    $attachment_id = absint( $_POST['attachment_id'] );
    $original      = get_attached_file( $attachment_id );
 
    $dimensions = $this->get_header_dimensions(
        array(
            'height' => $_POST['height'],
            'width'  => $_POST['width'],
        )
    );
    $height     = $dimensions['dst_height'];
    $width      = $dimensions['dst_width'];
 
    if ( empty( $_POST['skip-cropping'] ) ) {
        $cropped = wp_crop_image(
            $attachment_id,
            (int) $_POST['x1'],
            (int) $_POST['y1'],
            (int) $_POST['width'],
            (int) $_POST['height'],
            $width,
            $height
        );
    } elseif ( ! empty( $_POST['create-new-attachment'] ) ) {
        $cropped = _copy_image_file( $attachment_id );
    } else {
        $cropped = get_attached_file( $attachment_id );
    }
 
    if ( ! $cropped || is_wp_error( $cropped ) ) {
        wp_die( __( 'Image could not be processed. Please go back and try again.' ), __( 'Image Processing Error' ) );
    }
 
    /** This filter is documented in wp-admin/includes/class-custom-image-header.php */
    $cropped = apply_filters( 'wp_create_file_in_uploads', $cropped, $attachment_id ); // For replication.
 
    $attachment = $this->create_attachment_object( $cropped, $attachment_id );
 
    if ( ! empty( $_POST['create-new-attachment'] ) ) {
        unset( $attachment['ID'] );
    }
 
    // Update the attachment.
    $attachment_id = $this->insert_attachment( $attachment, $cropped );
 
    $url = wp_get_attachment_url( $attachment_id );
    $this->set_header_image( compact( 'url', 'attachment_id', 'width', 'height' ) );
 
    // Cleanup.
    $medium = str_replace( wp_basename( $original ), 'midsize-' . wp_basename( $original ), $original );
    if ( file_exists( $medium ) ) {
        wp_delete_file( $medium );
    }
 
    if ( empty( $_POST['create-new-attachment'] ) && empty( $_POST['skip-cropping'] ) ) {
        wp_delete_file( $original );
    }
 
    return $this->finished();
}


Top ↑

Changelog

Changelog
VersionDescription
4.4.0Switched to using wp_get_attachment_url() instead of the guid for retrieving the header image URL.
2.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.