Use the first photo as a cover or logo
- Thread started
- Last updated
What you’ll achieve
Use the first uploaded listing photo as its cover or logo.
My site
Make the main photo uploaded with a listing its cover or logo. Later uploads keep their normal role.
Choose the photo role
Choose cover or logo. Register only one callback for the same upload. These examples do not convert existing media.
Keep the customization in your JReviews overrides. Start a new PHP file with <?php followed by defined('MVC_FRAMEWORK') or die;. In an existing file, add the callback without repeating the opening tag or guard. If a snippet includes those lines, keep each only once.
JReviews 6 Early Access
Enable Listing Form in Early Access. This callback runs when photos uploaded during submission attach to the saved listing. Media-manager uploads use a separate process.
Add the cover callback
Joomla. Use templates/jreviews_overrides/hooks.php.
WordPress. Use jreviews_overrides/hooks.php.
Add the callback marked with +. It makes a photo the cover when JReviews marks it as the main upload for that listing.
- If
hooks.phpalready exists, add the marked lines at the end of the file. The copy button copies only those lines. - If you are creating
hooks.php, type the two dimmed lines first, then add the marked lines below them.
<?php
defined('MVC_FRAMEWORK') or die;
fwd_add_filter('jreviews:media.data_before_save', function ($mediaData, $listing, $user) {
if (($mediaData['media_type'] ?? null) === 'photo' && (int) ($mediaData['main_media'] ?? 0) === 1) {
$mediaData['media_function'] = 'cover';
}
return $mediaData;
}, 20, 3);
Use a logo instead
To assign a logo, replace cover with logo in the callback. Keep its conditions and register the callback once.
JReviews 5 and v6 Legacy
Add the cover callback
Joomla. Use templates/jreviews_overrides/filters/filter_functions.php.
WordPress. Use jreviews_overrides/filters/filter_functions.php.
Add the function and filter marked with +. The code applies only to a newly saved photo marked as the main media. It does not assign cover or logo roles to videos. If a video is already the main media, this callback does not promote a later photo. Choose the cover or logo manually for that listing.
- If
filter_functions.phpalready exists, add the marked lines at the end of the file. The copy button copies only those lines. - If you are creating
filter_functions.php, type the two dimmed lines first, then add the marked lines below them.
<?php
defined('MVC_FRAMEWORK') or die;
function media_use_cover_as_default($media, $params)
{
if (! empty($params['is_new'])
&& ($media['Media']['media_type'] ?? null) === 'photo'
&& (int) ($media['Media']['main_media'] ?? 0) === 1) {
$media['Media']['media_function'] = 'cover';
}
return $media;
}
Clickfwd\Hook\Filter::add('media_save_pre', 'media_use_cover_as_default', 10);
Use a logo instead
For a logo, add the function and filter marked with + instead of the cover callback.
<?php
defined('MVC_FRAMEWORK') or die;
function media_use_logo_as_default($media, $params)
{
if (! empty($params['is_new'])
&& ($media['Media']['media_type'] ?? null) === 'photo'
&& (int) ($media['Media']['main_media'] ?? 0) === 1) {
$media['Media']['media_function'] = 'logo';
}
return $media;
}
Clickfwd\Hook\Filter::add('media_save_pre', 'media_use_logo_as_default', 10);
Save and reopen the listing to check the stored photo role and its visible position.
Check first and later uploads
Upload a first photo during listing submission. Save and reopen the listing. Confirm the photo has the chosen cover or logo role and appears in that position. Upload another photo and confirm it keeps its normal role. Check media-manager uploads separately before relying on the same behavior there.
Learn more
- Media Storage (v5)
- Hooks (v5)
- Media Storage (v6)
- Hooks (v6)