JReviews logo
Guides My site
← All guides Community guide

Create a custom shortcode type

M Alejandro
Thread started by MelB. Contributors Alejandro.
Thread started
Last updated

What you’ll achieve

Display a separate company logo for the listing owner with a custom shortcode and supported listing hooks.

My site

Platform
JReviews version

Display a company logo separately from the listing owner's headshot, cover, and image gallery. The example creates an externalimage shortcode and places the same logo below the listing's custom fields.

Choose the image explicitly for each owner. This example does not add an upload field or change the images that JReviews already manages.

Choose the owner's company logo

Save a file named company-logo.php in your JReviews overrides folder. Use the code for your CMS below.

Use templates/jreviews_overrides/company-logo.php.

In EasySocial, upload the company logo to an ordinary photo album belonging to the listing owner. Keep the existing profile picture as the headshot. Find the owner's Joomla user ID and the logo's EasySocial photo ID. Replace 123 => 456 with that user ID and photo ID. Add another entry for each owner who has a logo.

The code loads the chosen photo through EasySocial. It checks the owner and the current visitor's profile, album, and photo access before returning an image. A missing, unpublished, blocked, or inaccessible image produces no output. EasySocial supplies the image URL, including its configured remote storage URL.

<?php

defined('_JEXEC') or exit;

class GuideCompanyLogo
{
	public static function render(array $attributes): string
	{
		$ownerId = filter_var($attributes['user'] ?? null, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
		$photos = [123 => 456];
		$photoId = $photos[$ownerId] ?? null;
		$bootstrap = JPATH_ADMINISTRATOR.'/components/com_easysocial/includes/easysocial.php';

		if (! $ownerId || ! $photoId || ! is_file($bootstrap)) {
			return '';
		}

		require_once $bootstrap;
		$owner = ES::user($ownerId);
		$viewer = ES::user();
		$photo = ES::table('Photo');

		if (! $owner->id || $owner->isBlock() || $owner->isBanned()
			|| ! $viewer->canView($owner)
			|| ! $photo->load($photoId)
			|| $photo->type !== SOCIAL_TYPE_USER
			|| (int) $photo->uid !== $ownerId
			|| (int) $photo->user_id !== $ownerId
			|| (int) $photo->state !== SOCIAL_STATE_PUBLISHED) {
			return '';
		}

		$library = ES::photo($photo->uid, $photo->type, $photo);

		if ($library->isblocked() || ! $library->albumViewable() || ! $library->viewable()) {
			return '';
		}

		$url = $photo->getSource('large');

		if (! is_string($url) || ! preg_match('~^(?:https?://|/(?!/))~i', $url)) {
			return '';
		}

		return '<img src="'.htmlspecialchars($url, ENT_QUOTES, 'UTF-8')
			.'" alt="Company logo" width="180" loading="lazy">';
	}
}

If photo access differs between visitors, exclude these listing pages from shared full-page caches. Otherwise a cached page can show an image that EasySocial would hide on a fresh request.

Use jreviews_overrides/company-logo.php.

Use a company logo that you have deliberately published as a public image, such as a file uploaded through the Media Library. Copy its public URL. Replace 123 with the listing owner's WordPress user ID and replace the example URL. Add another entry for each owner who has a logo.

Use only public company images in this map. This version does not read private profile photos or grant access to protected media. An unmapped or missing user produces no output.

<?php

defined('ABSPATH') or exit;

class GuideCompanyLogo
{
	public static function render(array $attributes): string
	{
		$ownerId = filter_var($attributes['user'] ?? null, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
		$logos = [123 => 'https://www.example.com/wp-content/uploads/company-logo.png'];
		$url = $logos[$ownerId] ?? '';
		$owner = $ownerId ? get_userdata($ownerId) : false;

		if (! $owner || ! preg_match('~^https?://~i', $url)) {
			return '';
		}

		return '<img src="'.esc_url($url)
			.'" alt="Company logo" width="180" loading="lazy">';
	}
}

Register the shortcode and display the logo

Add the code marked with + to hooks.php in the same overrides folder. Enable the Shortcodes Early Access feature to process the registered shortcode. Enable Listing Detail Page for the automatic placement below the listing's custom fields.

Use templates/jreviews_overrides/hooks.php.

Use jreviews_overrides/hooks.php.

  • If hooks.php already 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;

require_once __DIR__.'/company-logo.php';

class GuideExternalImageShortcode
{
	public function render(array $attributes, string $content = ''): string
	{
		return GuideCompanyLogo::render($attributes);
	}
}

fwd_config(['shortcodes.externalimage' => GuideExternalImageShortcode::class]);

fwd_add_action('jreviews:listing.below-fields', function ($listing): void {
	echo GuideCompanyLogo::render(['user' => (string) $listing->owner_id]);
}, 10, 1);

The listing hook passes the actual listing owner's ID. It does not use the logged-in visitor's ID. The handler also accepts [jreviews type="externalimage" user="123"] on a page where JReviews processes shortcodes. Replace 123 with an owner in your image map.

If you only want manual shortcode placement, omit the fwd_add_action block. Keep the handler class and its registration.

Register the shortcode and display the logo

Create the shortcode type file below.

Use templates/jreviews_overrides/includes/shortcodes/types/externalimage.php.

Use jreviews_overrides/includes/shortcodes/types/externalimage.php.

<?php

defined('_JEXEC') or defined('ABSPATH') or exit;

require_once dirname(__DIR__, 3).'/company-logo.php';

class ExternalimageShortCode
{
	public function render(array $attributes, string $content = ''): string
	{
		return GuideCompanyLogo::render($attributes);
	}
}

Keep the filename externalimage.php, class name ExternalimageShortCode, and shortcode type externalimage exactly as shown. JReviews finds this override automatically; no JReviews file needs editing.

To place the logo automatically before the listing description, add the code marked with + to the override filter_functions.php file.

Use templates/jreviews_overrides/filters/filter_functions.php.

Use jreviews_overrides/filters/filter_functions.php.

  • If filter_functions.php already 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;

use Clickfwd\Hook\Action as GuideCompanyLogoAction;

require_once dirname(__DIR__).'/company-logo.php';

GuideCompanyLogoAction::add('template_listings.detail:before-description', function (array $listing): void {
	echo GuideCompanyLogo::render(['user' => (string) ($listing['Listing']['created_by'] ?? 0)]);
});

The listing hook passes the actual listing owner's ID. You can also insert [jreviews type="externalimage" user="123"] where JReviews processes shortcodes. Replace 123 with an owner in your image map. Omit the action block if you only want manual shortcode placement.

Check the image on two owners' listings

Clear the JReviews file registry and page cache after adding the files. Open a listing whose owner has a mapped company logo. The company image should appear separately from the headshot, cover, and gallery. Open a listing owned by another member and confirm that it uses that owner's image or shows no company logo.

Remove a map entry and check that no broken image appears. Test as a guest and as a signed-in visitor.

Make the EasySocial photo or album private and confirm that an unauthorized visitor cannot see the image after clearing page caches. Replace a mapped photo ID with a different owner's photo ID and confirm that it produces no output.

Learn more