Skip to content

Commit

Permalink
Merge pull request #619 from alicolville/custom-thumbnail-sizes
Browse files Browse the repository at this point in the history
10.16
  • Loading branch information
alicolville authored Oct 18, 2024
2 parents d7a18d2 + 7aa2617 commit eb83e05
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 21 deletions.
28 changes: 28 additions & 0 deletions docs/custom-sizes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Custom sizes

For some photo based shortcodes, you have the ability to pass a "custom-size" argument. In essence, this allows you pass a WordPress image sizes. These can include the standard ones [defined in WordPress core](https://developer.wordpress.org/reference/functions/add_image_size/#reserved-image-size-names) (e.g. ‘thumb’, ‘thumbnail’, ‘medium’, ‘medium_large’, ‘large’, and ‘post-thumbnail’) or ones you have defined yourself.

### Defining your own images sizes

There are various ways to define your own images sizes, in code, themes, plugins, etc. This guide will briefly cover how to do it in code.

In essence, you need a snippet of code like the one below. This defines a custom image size of "wt-photo", with a with of 400px, a height of 600px and for it to be cropped.

```
function wpdocs_theme_setup() {
add_image_size( 'wt-photo', 400, 600, true ); // (cropped)
}
add_action( 'after_setup_theme', 'wpdocs_theme_setup' );
```

There are plenty of guides out there, but I've found [this one pretty handy](https://developer.wordpress.org/reference/functions/add_image_size).

> Please note: When creating new custom sizes, you will need to force WordPress to regenerate all thumbnails in the media library. A good plugin for this is [Regenrate Thumbnails](https://wordpress.org/plugins/regenerate-thumbnails/).
### Using your custom size

Once you have a custom size, you can use the size name within supported Weight Tracker shortcodes. Given the example above, you can use the custom size "wt-photo" in the following way:

```
[wt-photo-oldest custom-fields-to-use="photo" custom-size="wt-photo"]
```
3 changes: 2 additions & 1 deletion docs/shortcodes/wt-photo-oldest.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ The shortcode supports the following arguments:
|--|--|--|--|
|css-class| Specify an additional CSS class for the photo frame.| String (empty by default)| [wt-photo-oldest css-class="a-css-class"]
|custom-fields-to-use| Slugs of one or more photo ([custom field]({{ site.baseurl }}/custom-fields.html)) to display within the shortcode. |All enabled photo fields that aren't hidden from shortcodes. |[wt-photo-recent custom-fields-to-use="front,back" ]
|custom-size| Name of ([image size]({{ site.baseurl }}/custom-sizes.html)) to use when rendering photo. |Blank (default) or string |[wt-photo-oldest custom-fields-to-use="front" custom-size="thumbnail"]
|error-message| Message to display if a relevant photo could not be found. |String. Defaults to an in-build message.| [wt-photo-oldest error-message="No photo!!"]
|height| Allows you to specify the maximum height for the photo. It is best to specify the width argument as well.| Number (default: 200).| [wt-photo-oldest height="400" width="400"]
|hide-date| If set to true, hide the date that is displayed.| True or false (default) |[wt-photo-oldest hide-date=true]|
|maximum| Maximum number of photos to render. |Numeric (defaults to 1) |[wt-photo-recent maximum=3]
|width| Allows you to specify the maximum width for the photo. It is best to specify the height argument as well.| Number (default: 200). |[wt-photo-oldest height="400" width="400"]
|user-id|By default, the shortcode will display the oldest photo for the current user. You can display the oldest photo for another user by setting this argument to the relevant user ID.|Numeric| [wt-photo-oldest user-id="1"]
|user-id|By default, the shortcode will display the oldest photo for the current user. You can display the oldest photo for another user by setting this argument to the relevant user ID.|Numeric| [wt-photo-oldest user-id="1"]
1 change: 1 addition & 0 deletions docs/shortcodes/wt-photo-recent.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The shortcode supports the following arguments:
|--|--|--|--|
|css-class| Specify an additional CSS class for the photo frame.| String (empty by default)| [wt-photo-recent css-class="a-css-class"]
|custom-fields-to-use| Slugs of one or more photo ([custom field]({{ site.baseurl }}/custom-fields.html)) to display within the shortcode. |All enabled photo fields that aren't hidden from shortcodes. |[wt-photo-recent custom-fields-to-use="front,back" ]
|custom-size| Name of ([image size]({{ site.baseurl }}/custom-sizes.html)) to use when rendering photo. |Blank (default) or string |[wt-photo-recent custom-fields-to-use="front" custom-size="thumbnail"]
|error-message| Message to display if a relevant photo could not be found. |String. Defaults to an in-build message.| [wt-photo-recent error-message="No photo!!"]
|height| Allows you to specify the maximum height for the photo. It is best to specify the width argument as well.| Number (default: 200).| [wt-photo-recent height="400" width="400"]
|hide-date| If set to true, hide the date that is displayed.| True or false (default) |[wt-photo-recent hide-date=true]|
Expand Down
35 changes: 19 additions & 16 deletions pro-features/plus/photos.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,16 @@ function ws_ls_photos_shortcode_core($user_defined_arguments) {
}

$arguments = shortcode_atts([
'css-class' => '',
'error-message' => esc_html__('No recent photo found.', WE_LS_SLUG ),
'height' => 200,
'hide-date' => false,
'user-id' => get_current_user_id(),
'recent' => true,
'width' => 200,
'custom-fields-to-use' => '',
'maximum' => 1
'css-class' => '',
'error-message' => esc_html__('No recent photo found.', WE_LS_SLUG ),
'height' => 200,
'hide-date' => false,
'user-id' => get_current_user_id(),
'recent' => true,
'width' => 200,
'custom-fields-to-use' => '',
'maximum' => 1,
'custom-size' => ''
], $user_defined_arguments );

$arguments['user-id'] = ws_ls_force_numeric_argument($arguments['user-id'], get_current_user_id());
Expand All @@ -95,7 +96,7 @@ function ws_ls_photos_shortcode_core($user_defined_arguments) {

// Fetch photo
$photos = ws_ls_photos_db_get_recent_or_latest( $arguments['user-id'], $arguments['recent'], $arguments['width'],
$arguments['height'], $arguments['custom-fields-to-use'], true );
$arguments['height'], $arguments['custom-fields-to-use'], true, $arguments['custom-size'] );

if ( false === empty( $photos ) ) {

Expand Down Expand Up @@ -172,7 +173,8 @@ function ws_ls_photos_db_get_recent_or_latest( $user_id = false,
$width = 200,
$height = 200,
$meta_fields_to_use = '',
$hide_from_shortcodes = false ) {
$hide_from_shortcodes = false,
$custom_wp_size = NULL ) {

$user_id = (true === empty($user_id)) ? get_current_user_id() : $user_id;

Expand Down Expand Up @@ -218,7 +220,7 @@ function ws_ls_photos_db_get_recent_or_latest( $user_id = false,
break;
}

$photo_src = ws_ls_photo_get( $photo['photo_id'], $width, $height );
$photo_src = ws_ls_photo_get( $photo['photo_id'], $width, $height, true, NULL, $custom_wp_size );

if ( false === empty( $photo_src ) ) {

Expand Down Expand Up @@ -410,11 +412,12 @@ function ws_ls_photos_db_count_photos( $user_id = false, $hide_from_shortcodes =
*
* @return bool
*/
function ws_ls_photo_get( $attachment_id, $width = 200, $height = 200, $include_full_url = true, $css_class = NULL ) {

function ws_ls_photo_get( $attachment_id, $width = 200, $height = 200, $include_full_url = true, $css_class = NULL, $custom_wp_size = NULL ) {
$attributes = ( false === empty( $css_class ) ) ? [ 'class' => $css_class ] : '';

$photo['thumb'] = wp_get_attachment_image( $attachment_id, array( $width, $height), false, $attributes );
$size = !empty( $custom_wp_size ) ? $custom_wp_size : [ $width, $height ];

$photo['thumb'] = wp_get_attachment_image( $attachment_id, $size, false, $attributes );

if ( false === empty( $photo['thumb'] )) {

Expand Down
8 changes: 6 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: aliakro
Tags: weight,tracker,chart,history,macronutrient
Requires at least: 6.0
Tested up to: 6.5
Stable tag: 10.15.1
Stable tag: 10.16
Requires PHP: 7.4
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Expand Down Expand Up @@ -166,10 +166,14 @@ Measurements are created using Custom Fields. You can therefore specify the unit

== Upgrade Notice ==

10.9: New user settings for opting in and out of email notifications.
10.16: New feature: Added argument "custom-size" to shortcodes [wt-photo-oldest] and [wt-photo-recent]. Read more at https://docs.yeken.uk/custom-sizes.html.

== Changelog ==

= 10.16 =

* New feature: Added argument "custom-size" to shortcodes [wt-photo-oldest] and [wt-photo-recent]. Read more at https://docs.yeken.uk/custom-sizes.html.

= 10.15.1 =

* Info: Due the changes below, you will see the notifications for Setupp Wizard and message from Yeken again until dismissed.
Expand Down
4 changes: 2 additions & 2 deletions weight-loss-tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Plugin Name: Weight Tracker
* Description: Allow your users to track their weight, body measurements, photos and other pieces of custom data. Display in charts, tables, shortcodes and widgets. Manage their data, issue awards, email notifications, etc! Provide advanced data on Body Mass Index (BMI), Basal Metabolic Rate (BMR), Calorie intake, Harris Benedict Formula, Macronutrients Calculator and more.
* Version: 10.15.1
* Version: 10.16
* Requires at least: 6.0
* Tested up to: 6.5
* Requires PHP: 7.4
Expand All @@ -17,7 +17,7 @@
* Domain Path: /includes/languages
*/

define( 'WE_LS_CURRENT_VERSION', '10.15.1' );
define( 'WE_LS_CURRENT_VERSION', '10.16' );
define( 'WS_LS_ABSPATH', plugin_dir_path( __FILE__ ) );
define( 'WS_LS_BASE_URL', plugin_dir_url( __FILE__ ) );
define( 'WE_LS_TITLE', 'Weight Tracker' );
Expand Down

0 comments on commit eb83e05

Please sign in to comment.