hooks - add_image_size not working with after_switch_theme

This is a weird one - this exact code works with after_setup_theme but not with after_switch_theme. Of course I would ra

This is a weird one - this exact code works with after_setup_theme but not with after_switch_theme. Of course I would rather use after_switch_theme because after_setup_theme runs all the time - but it just will not work.

add_action( 'after_setup_theme', 'wpdocs_theme_setup' );

function wpdocs_theme_setup() {
    add_theme_support( 'post-thumbnails' );

    add_image_size( 'hero_image', 1920, 800, true);
    add_image_size( 'hero_image_md_lg', 991, 490, true);
    add_image_size( 'hero_image_md', 767, 460, true);
    add_image_size( 'hero_image_sm', 549, 575, true);
    ...
}

For reference: yes, I am switching the theme. Yes, I am regenerating the thumbnails. And in my wpdocs_theme_setup function I'm also adding custom roles that work regardless of which hook is being used. Any thoughts?

This is a weird one - this exact code works with after_setup_theme but not with after_switch_theme. Of course I would rather use after_switch_theme because after_setup_theme runs all the time - but it just will not work.

add_action( 'after_setup_theme', 'wpdocs_theme_setup' );

function wpdocs_theme_setup() {
    add_theme_support( 'post-thumbnails' );

    add_image_size( 'hero_image', 1920, 800, true);
    add_image_size( 'hero_image_md_lg', 991, 490, true);
    add_image_size( 'hero_image_md', 767, 460, true);
    add_image_size( 'hero_image_sm', 549, 575, true);
    ...
}

For reference: yes, I am switching the theme. Yes, I am regenerating the thumbnails. And in my wpdocs_theme_setup function I'm also adding custom roles that work regardless of which hook is being used. Any thoughts?

Share Improve this question asked Mar 20, 2019 at 1:24 rickibarnesrickibarnes 1578 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The reason adding custom roles works on after_switch_theme is because roles are saved in the database. In fact, because it saves to the database, it should not run on after_setup_theme.

add_image_size(), on the other hand, does not save anything to the database, so needs to run on every page load, and needs to be hooked on a hook that runs for every page load. So after_setup_theme is the correct hook to use.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745674183a4639584.html

相关推荐

  • hooks - add_image_size not working with after_switch_theme

    This is a weird one - this exact code works with after_setup_theme but not with after_switch_theme. Of course I would ra

    5小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信