Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8815585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:24:59+00:00 2026-06-14T04:24:59+00:00

When I upload a featured image, I want to give it the a width

  • 0

When I upload a featured image, I want to give it the a width of “100%” but only if it is over 1170px. If the width is between 1170px and 770px, I want it be get a width of “770px”, otherwise the width will not be changed.

So far this code is doing what I want:

if (intval($width) >= 1170) {
        $hwstring = 'width=100%';
    } elseif ( (intval($width) < 1170) && (intval($width) >= 770) ) {
        $hwstring = 'width=770px';
    } else {
        $hwstring = image_hwstring($width, 0);
    };

However I have modified the media.php file inside the “wp-includes” folder which is apparently not the right way to do it. So how could I create a function that does the same thing without modifying the existing WordPress code?

function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') {

$html = '';
$image = wp_get_attachment_image_src($attachment_id, $size, $icon);
if ( $image ) {
    list($src, $width, $height) = $image;
    $hwstring = image_hwstring($width, $height);
    if ( is_array($size) )
        $size = join('x', $size);
    $attachment =& get_post($attachment_id);
    $default_attr = array(
        'src'   => $src,
        'class' => "attachment-$size",
        'alt'   => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first
        'title' => trim(strip_tags( $attachment->post_title )),
    );
    if ( empty($default_attr['alt']) )
        $default_attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption
    if ( empty($default_attr['alt']) )
        $default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title

    $attr = wp_parse_args($attr, $default_attr);
    $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment );
    $attr = array_map( 'esc_attr', $attr );

    if (intval($width) >= 1170) {
        $hwstring = 'width=100%';
    } elseif ( (intval($width) < 1170) && (intval($width) >= 770) ) {
        $hwstring = 'width=770px';
    } else {
        $hwstring = image_hwstring($width, 0);
    };

    $html = rtrim("<img $hwstring");
    foreach ( $attr as $name => $value ) {
        $html .= " $name=" . '"' . $value . '"';
    }
    $html .= ' />';
}

return $html;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:25:00+00:00Added an answer on June 14, 2026 at 4:25 am

    Unfortunately there aren’t any hooks for you to use to do this exactly in that function, but you can construct it yourself without modifying the core WordPress files (which you don’t want to do lest you overwrite your custom code when you upgrade). I’m kind of surprised that the wp_get_attachment_image_src() function doesn’t pass the return values through filter to do exactly what you are talking about.

    If you look at the top of this function, it gets the and array of $src, $width, $height by calling $image = wp_get_attachment_image_src($attachment_id, $size, $icon); You can make this same call yourself and construct your custom widths – basically copying the function into a custom version in functions.php or a custom functions plugin.

    You can create a new ticket at http://core.trac.wordpress.org/newticket if you want to request this functionality to be added in a future version. The addition would be to wp_get_attachment_image_src() on line 515 (of the current trunk version of WP):

    return apply_filters( 'wp_get_attachment_image_src', array( $src, $width, $height ), $attachment_id, $size, $icon );
    

    EDIT: Ticket already exists, patch is a little weird, I submitted a new one but no guarantees as to when it will go in if it’s approved..

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want my site to have a feature for users to upload images, but
Okay, I have a question guys. I want to remote upload (copy an image
Want to add a nice multiple image upload feature to a site so I
I want to add an avatar image upload feature to my project, and I
I'm intending to use JCrop for a standard image upload feature. Everything is all
I upload image files using aspupload component. I was wondering if its possible to
I've been struggling for quite a while to get this feature working: I want
What I'm trying to do: I want to give the user the ability to
I am using TinyMCE. It works great. But I want the feature in TinyMCE
I use Windows and I display a file extention. When I upload an image

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.