close
Skip to content

Add Support for Custom Image Sizes in wp_get_attachment_image to Reduce HTML Bloat#6930

Open
geekofshire wants to merge 1 commit intoWordPress:trunkfrom
geekofshire:fix/refactor-wp_get_attachment_image
Open

Add Support for Custom Image Sizes in wp_get_attachment_image to Reduce HTML Bloat#6930
geekofshire wants to merge 1 commit intoWordPress:trunkfrom
geekofshire:fix/refactor-wp_get_attachment_image

Conversation

@geekofshire
Copy link
Copy Markdown

Summary

This PR introduces a new feature to the wp_get_attachment_image function, allowing developers to specify a custom list of image sizes to be included in the HTML output. This helps in reducing HTML bloat by including only the necessary image sizes.

Changes

  • New Parameter: Added $use_custom_sizes parameter to the wp_get_attachment_image function.
    • Type: bool
    • Default: false
    • Purpose: When set to true and an array of sizes is provided in the $size parameter, only those specified sizes are included.
  • Function Logic: Updated the function to handle custom sizes if $use_custom_sizes is true.
    • If $use_custom_sizes is true and $size is an array, it processes each specified size individually.
    • If $use_custom_sizes is false, it behaves as before, using the single size provided.
  • Documentation: Updated the function docblock to include details about the new parameter and its usage.

Benefits

  • Reduced HTML Bloat: By allowing selective inclusion of image sizes, the function generates more efficient HTML, leading to faster page load times.
  • Improved Clarity: Separating the custom sizes logic into a distinct parameter makes the function easier to understand and use.
  • Backward Compatibility: Existing functionality remains unchanged for users who do not need custom sizes, ensuring backward compatibility.

Example Usage

// Default behavior (single size)
echo wp_get_attachment_image(123, 'large');

// Custom sizes usage
$sizes = ['thumbnail', [1200, 630], 'medium', 'large'];
echo wp_get_attachment_image(123, $sizes, false, '', true);

Trac ticket: https://core.trac.wordpress.org/ticket/56056

@github-actions
Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @rohanjha@Rohans-MacBook-Air.local.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant