0

I bought a WP-Theme which defines a custom image-size in his theme-functions.php like this:

if ( function_exists( 'add_image_size' ) ){
    add_image_size( 'tie-small', 70, 70, true );
}

As you can see the third-parameter is true, which results in hard crop mode (see Wordpress Documentation for add_image_size). This mode isn't taking care of the proportions so some of my thumbnails are resized very badly. I did some tests and changing the crop-Parameter to FALSE will create better results.

I want to overwrite this image-size by using a plugin so that I can update my theme without changing the value above to FALSE on every update. The add_image_size function storing his data in a array with $name as key. So multiplice calls of this function will overwrite the values up to the last call.

My plugin is very simple and looks like the following:

class My_Plugin {
    public function __construct() {
        add_action( 'after_setup_theme', array( $this, 'overwrite_wptheme_settings' ) );
    }

    public function overwrite_wptheme_settings() {
        add_image_size( 'tie-small', 70, 70, false );
    }
}

Its not working, I reuploaded a test-image and it looks like crop is set to true. I cant figure out why because the value was overwritten! I could verify this by going to the function tie_last_posts in the theme-functions.php of the theme. This function is generating the widget with the post-thumbnail image:

[...]
function tie_last_posts($numberOfPosts = 5 , $thumb = true) {
    // [...]
    <?php global $_wp_additional_image_sizes; 
          var_dump($_wp_additional_image_sizes);
    ?>
    <?php the_post_thumbnail( 'tie-small' ) ; ?>

This will output the following before the widget:

array(3) {
  ["tie-small"]=>
  array(3) {
    ["width"]=>
    int(70)
    ["height"]=>
    int(70)
    ["crop"]=>
    bool(false)
  }
  ["tie-large"]=>
  array(3) {
    ["width"]=>
    int(300)
    ["height"]=>
    int(160)
    ["crop"]=>
    bool(true)
  }
  ["slider"]=>
  array(3) {
    ["width"]=>
    int(620)
    ["height"]=>
    int(330)
    ["crop"]=>
    bool(true)
  }
}

As you can see, the attribute crop is set to false in the index tie-small. So it was overwritten by my plugin. But the thumbnail get cropped. I verified this by using a testimage which is getting clearly bad when it gets cropped by wordpress. I always reuploaded the image in the article. AND I have verified that the overwriting itself is working: I inserted add_image_size( 'tie-small', 70, 70, false ); at the end of the theme-functions.php and it worked, but in my plugin it isnt. Whats wrong here?

  • If you are trying this with same image over and over I would check if browser not caching the image and keeps showing something old after the actual file had been regenerated. – Rarst Jun 3 '15 at 21:43
  • This can't be the case. I'm always using STRG + F5 and I've disabled the cache in the network-tab of the chrome developer-tools. And I also tried to set the parameter from true to false directly in the theme-functions.php AND i inserted my add_image_size-call at the end of the theme-functions.php. Both worked like expected, but in my plugin it has bizarrely no effect... – Lion Jun 3 '15 at 21:50
1

Change the action to 'init':

class My_Plugin {
    public function __construct() {
        add_action( 'init', array( $this, 'overwrite_wptheme_settings' ) );
    }

    public function overwrite_wptheme_settings() {
        add_image_size( 'tie-small', 70, 70, false );
    }
}
  • Also has no effect, although the action gets fired, in fact AFTER the add_image_size call in the theme-functions.php! Its very weird... – Lion Jun 4 '15 at 12:23
  • Strange, I've tested the code in a local installation and it works. Anyway yes, to override a theme function, use a child theme. – Benito Lopez Jun 4 '15 at 23:32
0

Solved this by creating a child-theme. In the action after_setup_theme I overwrite the image-size and it works, thumbnails aren't cropped anymore. Not sure why it isn't possible to do this in a plugin, but for me it seems that creating a childtheme instead of a plugin is the better solution anyway. The WP-Doc itself says that this is the recommended way to modify a theme, so I will move my changes made by my plugin to the child-theme I created.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.