2

I'm trying to auto update a theme only when it's a minor update. I know I can use the auto_update_{$type} filter where $type = theme. The only examples of using that filter I've seen have been using the callback __return_true and not utilizing the second parameter $item.

add_filter( 'auto_update_theme', '__return_true' );

My problem is that I don't want to auto upgrade on major version changes. I was hoping the second parameter passed to the filter would contain some information on the versions, but I can find nothing in the source code.

2

It is one element of the response Array that's a property of the object stored in the update_plugins or update_themes site transient.

That transient looks like this (var_dump()ed):

object(stdClass)#1 (4) {
  ["last_checked"]=>
  int(1519305016)
  ["response"]=>
  array(1) {
    ["akismet/akismet.php"]=>
    object(stdClass)#2 (11) {
      ["id"]=>
      string(21) "w.org/plugins/akismet"
      ["slug"]=>
      string(7) "akismet"
      ["plugin"]=>
      string(19) "akismet/akismet.php"
      ["new_version"]=>
      string(5) "4.0.3"
      ["url"]=>
      string(38) "https://wordpress.org/plugins/akismet/"
      ["package"]=>
      string(56) "https://downloads.wordpress.org/plugin/akismet.4.0.3.zip"
      ["icons"]=>
      array(3) {
        ["1x"]=>
        string(59) "https://ps.w.org/akismet/assets/icon-128x128.png?rev=969272"
        ["2x"]=>
        string(59) "https://ps.w.org/akismet/assets/icon-256x256.png?rev=969272"
        ["default"]=>
        string(59) "https://ps.w.org/akismet/assets/icon-256x256.png?rev=969272"
      }
      ["banners"]=>
      array(2) {
        ["1x"]=>
        string(61) "https://ps.w.org/akismet/assets/banner-772x250.jpg?rev=479904"
        ["default"]=>
        string(61) "https://ps.w.org/akismet/assets/banner-772x250.jpg?rev=479904"
      }
      ["banners_rtl"]=>
      array(0) {
      }
      ["tested"]=>
      string(5) "4.9.4"
      ["compatibility"]=>
      object(stdClass)#3 (0) {
      }
    }
  }
}

So the $item for a plugin update will look like the akismet/akismet.php element in the response Array, meaning you can access the version with $item->new_version.

It is however important to note that there's zero guarantee that plugins and themes all use the same version numbering system. For example, in a WordPress version X.Y.Z, Y is a major version and Z is a minor version (and X is just for show, apparently), but in WooCommerce X is a major version and Y is a minor version. Every plugin and theme could be different.

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.