Things I learned while developing the million eyez WordPress plugin — part 1

Working with featured photos

Million eyez is all about quality photos for your posts, so it’s only natural you would want to use one of these photos as your post’s featured photo. One of the first tasks I had for our plugin was exactly that.

  • Line 14 — we get the current value of the _thumbnail_id property of the handled post.
  • Lines 16–18 — if the _thumbnail_id property has a value we finish the hook interception by returning whatever value that might be (this usually means the featured photo was already set in WordPress itself or by using other plugins we don’t want to override)
  • Lines 24–26 — by using the preg_match function we check whether the post content contains the million eyez short code, and if it does we return it (in the format for me-[photobox id]) as the _thumbnail_id property.
  • Line 4 — performs a check on the $attachment_id variable to make sure its a valid million eyez ID.
  • Lines 5–9 — These 5 lines determine the width for the featured photo. If the $size variable is an array, we take the first value of the array (located at index 0), if its not an array, we use $size as the index for the global array $_wp_additional_image_sizes and from that array we take the ‘width’ property. To better understand $_wp_additional_image_sizes take a look at these docs.
  • Line 10 — At million eyez, we use an aspect ratio of 4/3 for all our photos, so in order to calculate the height of the featured photo, we divide its width by 4 and multiply it by 3.
  • Line 11 — We end the function by returning an array containing the photo URL (which the /featuredPhoto endpoint redirects to), height and width.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Johnny Tordgeman

Johnny Tordgeman

Core Team SE @getapril_tax | Head Dev @DropoutUniv | Loves to talk about Rust 🦀 / TypeScript / JavaScript / WebAssembly | Web3 is AWESOME