$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>images - How to implement add_image_size in a plugin|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

images - How to implement add_image_size in a plugin

matteradmin8PV0评论

I created a plugin and I need to use add_image_size in it.

I know how it works in the functions.php file and for a theme, but how can I implement this in a plugin?

I want new images uploaded by WordPress users to be smaller for mobile screens by

add_image_size( 'wp_small', 60, 75, true ); // mobile

I read that you need to use init or admin_init but I don't know how to implement this.

I created a plugin and I need to use add_image_size in it.

I know how it works in the functions.php file and for a theme, but how can I implement this in a plugin?

I want new images uploaded by WordPress users to be smaller for mobile screens by

add_image_size( 'wp_small', 60, 75, true ); // mobile

I read that you need to use init or admin_init but I don't know how to implement this.

Share Improve this question edited Mar 4, 2019 at 10:05 norman.lol 3,2613 gold badges30 silver badges35 bronze badges asked Jun 7, 2012 at 1:40 GinoGino 2023 silver badges10 bronze badges 1
  • Did you finish your plugin yet? And did you release it by any chance? I'm searching for one currently, can't be bothered to code something... :D – Julian F. Weinert Commented Feb 8, 2021 at 13:00
Add a comment  | 

2 Answers 2

Reset to default 6

Just call this function in the init action. This action is fired for both frontend and backend. So it should look like this:

add_action( 'init', 'wpse4378_add_new_image_size' );
function wpse4378_add_new_image_size() {
    add_image_size( 'wp_small', 60, 75, true ); //mobile
}

It's better to call it in after_setup_theme instead of init action:

Codex says you should call add_image_size in functions.php so the closest action is after_setup_theme. Then you have:

add_action( 'after_setup_theme', 'your_theme_setup' );
function your_theme_setup() {
    add_image_size( 'wp_small', 60, 75, true ); //mobile
}

(Also explained in user contributed notes)

Post a comment

comment list (0)

  1. No comments so far