WordPress.org

Make WordPress Core

Opened 7 months ago

Last modified 7 months ago

#51908 new defect (bug)

add_image_size not working with custom plugin code

Reported by: arpitgshah Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Media Keywords: reporter-feedback
Focuses: docs Cc:

Description

Hey,

add_image_size() not working within the custom plugin code that we can change the the_post_thumbnail() with any custom size.

Thanks,
Arpit G Shah

Change History (8)

#1 @audrasjb
7 months ago

  • Keywords reporter-feedback added

Hi @arpitgshah and welcome to WordPress Trac,

Can you please elaborate on the issue and provide example of code/additional context?

#2 @SergeyBiryukov
7 months ago

  • Component changed from General to Media
  • Focuses docs added

Hi there, welcome to WordPress Trac! Thanks for the report.

Some context from #core on Slack:

add_image_size() should be called on the after_setup_theme action hook. It's not clear from the inline documentation, but there's a comment on the developer hub detailing that.

Let's use this ticket to clarify the inline documentation :)

#3 follow-up: @arpitgshah
7 months ago

  • Component changed from Media to General
  • Focuses docs removed

@audrasjb here we have some sample code,

<?php
add_action( 'init', 'custom_post_size', 0 );

function custom_post_size() {
add_image_size( 'custom-size', 220, 180, true );
the_post_thumbnail( 'custom-size' ); 
}

#4 @audrasjb
7 months ago

  • Focuses docs added

Got it! Makes sense to me, thanks @SergeyBiryukov for the context 👍

#5 @audrasjb
7 months ago

  • Component changed from General to Media

#6 @arpitgshah
7 months ago

@audrasjb @SergeyBiryukov

It's also not working with the code as well. We also need to find something alternative. is it possible to work it around?

This ticket was mentioned in Slack in #core by xhtmlpoint. View the logs.


7 months ago

#8 in reply to: ↑ 3 @azaozz
7 months ago

Replying to arpitgshah:

@audrasjb here we have some sample code,

<?php
add_action( 'init', 'custom_post_size', 0 );

function custom_post_size() {
    add_image_size( 'custom-size', 220, 180, true );
    the_post_thumbnail( 'custom-size' ); 
}

Hmmm, looking at this example, what is the purpose of the_post_thumbnail() call there? This function is used to display an image (usually) on the front-end, see https://developer.wordpress.org/reference/functions/the_post_thumbnail/. Calling it on init is "wrong" :)

It's also not working with the code as well. We also need to find something alternative.

As far as I understand the bug here is that when calling add_image_size() from a theme's functions.php, it has to be done on the after_setup_theme action hook. That needs to be added to the docblock there.

Is there another problem/bug?

Note: See TracTickets for help on using tickets.