Opened 10 years ago
Last modified 21 months ago
#37972 new defect (bug)
Image size (height/width) arguments are not used in output for custom_logo
| Reported by: | BinaryKitten | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Themes | Version: | 4.6 |
| Severity: | normal | Keywords: | has-patch |
| Cc: | Focuses: |
Change History (3)
This ticket was mentioned in Slack in #themereview by joyously. View the logs.
8 years ago
This ticket was mentioned in PR #1423 on WordPress/wordpress-develop by paulschreiber.
5 years ago
#2
- Keywords has-patch added
22 months ago
#3
To fetch the height and width from the custom logo settings in WordPress and pass them as a size parameter to wp_get_attachment_image(), you can use the following approach:
- Get the Custom Logo ID: Use
get_theme_mod( 'custom_logo' )to retrieve the custom logo ID. - Get the Image Metadata: Use
wp_get_attachment_metadata()to fetch the logo’s width and height. - Pass Dimensions to
wp_get_attachment_image(): Once you have the dimensions, pass them in thesizeparameter when callingwp_get_attachment_image().
Here’s an example code snippet:
// Get the custom logo ID $custom_logo_id = get_theme_mod( 'custom_logo' ); // Check if a custom logo is set if ( $custom_logo_id ) { // Get the logo metadata $logo_metadata = wp_get_attachment_metadata( $custom_logo_id ); // Extract width and height if available $width = $logo_metadata['width'] ?? ''; $height = $logo_metadata['height'] ?? ''; // Pass width and height to wp_get_attachment_image as an array echo wp_get_attachment_image( $custom_logo_id, [ $width, $height ] ); }
<sub><sup style="font-size: 0.1em;">.</sup></sub>
This code checks if a custom logo exists, retrieves its dimensions, and then passes those dimensions as an array to wp_get_attachment_image(). This approach ensures the custom logo appears at its correct size.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
Fetch the height and width from the custom logo settings. If present, pass them in as a size parameter to wp_get_attachment_image().
Trac ticket: https://core.trac.wordpress.org/ticket/37972