JReviews logo
Guides My site
← All guides Community guide

Connect listings with Zapier

r Alejandro
Thread started by radiopol. Contributors Alejandro.
Thread started
Last updated

What you’ll achieve

Send a newly published listing to Zapier, and create a listing from a Zapier webhook.

My site

Platform
JReviews version

Zapier is a service that sends data from one app to another. When you publish a restaurant listing, Zapier adds a row to a Google Sheet with the title, summary, category, and listing URL. A second Zap does the reverse. A new row in a separate import sheet creates a restaurant listing.

You need a Zapier account, two Google Sheets, and FTP or file-manager access to the site's JReviews overrides folder. Create files only in overrides. Do not change JReviews core files. Start each new PHP file with <?php followed by defined('MVC_FRAMEWORK') or die;.

Prepare the example category before you copy any files.

  1. In Setup → Listing Types Manager, create a listing type named Restaurant, or open the type you already use for restaurants.
  2. Set summary, description, custom fields, and photos to optional. Allow your account to create listings of this type.
  3. For the first test, turn off moderation on that type so new listings go live on submit.
  4. In Setup → Categories Manager, create Restaurants and assign that listing type, or open the category you already use.
  5. Copy the category ID. The files below use 23. Replace 23 with that ID.

Send a published listing to a Google Sheet

Create a Catch Hook in Zapier first, so you have the URL for the PHP file you add next. A Catch Hook is a Zapier URL that receives the listing title, summary, category, and URL your site posts.

Create the Catch Hook

  1. In Zapier, create a Zap.
  2. For the trigger, choose Webhooks by Zapier, then Catch Hook.
  3. Copy the webhook URL Zapier shows. It looks like https://hooks.zapier.com/hooks/catch/ followed by two path segments.
  4. Leave this Zap open. After you publish a test listing, you will run Test on this step.

JReviews 6 Early Access

Open the JReviews dashboard, select Manage Early Access, and turn on Listing Form. Use the site listing form for the Harbor Grill and Cedar Cafe tests in this section, so the listing events below run.

Create the listener

This file collects the listing title, summary, category, and URL, then posts them to your Catch Hook.

Joomla. Create templates/jreviews_overrides/app/Listeners/SendNewListingToZapier.php.

WordPress. Create jreviews_overrides/app/Listeners/SendNewListingToZapier.php.

Save this complete file. Put the Catch Hook URL you copied into the $webhook value.

<?php

namespace JReviewsOverrides\App\Listeners;

defined('MVC_FRAMEWORK') or die;

use JReviews\App\Commands\SendWebhookCommand;
use JReviews\App\Enums\ModerationActionEnum;
use JReviews\App\Events\ListingModerationStatusChanged;
use JReviews\App\Events\ListingWasFirstPublished;
use JReviews\App\Models\Listing;

class SendNewListingToZapier
{
	protected $webhook = 'https://hooks.zapier.com/hooks/catch/YOURID/YOURHOOK/';

	public function handle(ListingWasFirstPublished|ListingModerationStatusChanged $event): void
	{
		if ($event instanceof ListingModerationStatusChanged
			&& $event->action !== ModerationActionEnum::Accept) {
			return;
		}

		$this->send($event->listing);
	}

	protected function send(Listing $listing): void
	{
		$listing = Listing::query()
			->with(['directory', 'user', 'media', 'listing_type'])
			->find($listing->id) ?? $listing;

		$fields = [];

		foreach ($listing->getFields() as $field) {
			$text = $listing->getFieldText($field->name);

			if ($text === null || $text === '') {
				continue;
			}

			$fields[$field->name] = is_array($text) ? implode(', ', $text) : (string) $text;
		}

		$payload = [
			'title' => $listing->title,
			'summary' => strip_tags((string) $listing->summary),
			'description' => strip_tags((string) $listing->description),
			'category' => $listing->category_title,
			'directory' => $listing->directory?->title ?? '',
			'owner' => $listing->user?->username ?? '',
			'listing_url' => $listing->url,
			'main_media_url' => $listing->mainPhoto->exists ? (string) $listing->mainPhoto->original : '',
			'fields' => $fields,
		];

		SendWebhookCommand::dispatch($this->webhook, $payload, [
			'origin' => 'zapier',
			'ref' => 'listing.'.$listing->id,
		]);
	}
}

Register the listener

Joomla. Create templates/jreviews_overrides/config/events.php.

WordPress. Create jreviews_overrides/config/events.php.

If config/events.php does not exist, save this complete file.

<?php

use JReviews\App\Events\ListingModerationStatusChanged;
use JReviews\App\Events\ListingWasFirstPublished;
use JReviewsOverrides\App\Listeners\SendNewListingToZapier;

return [
	ListingWasFirstPublished::class => [
		SendNewListingToZapier::class,
	],
	ListingModerationStatusChanged::class => [
		SendNewListingToZapier::class,
	],
];

If events.php already exists, add the three use lines at the top if they are missing. Then add these two keys below the return [ line. If that array already has entries, put a comma after the last existing entry.

return [
	ListingWasFirstPublished::class => [
		SendNewListingToZapier::class,
	],
	ListingModerationStatusChanged::class => [
		SendNewListingToZapier::class,
	],
];

JReviews runs this file when a new listing is published on the form. It also runs when a moderator accepts a listing. It does not run for later edits. To send edits too, add ListingWasUpdated next to the other two names in events.php and point it at SendNewListingToZapier.

The keys in $payload become the fields you map in Zapier. Add or remove keys in $payload when you need different columns. main_media_url is empty unless the listing already has a main photo when it becomes public. Upload the main photo before you publish, or accept the listing from moderation after the photo is in place.

JReviews sends the listing data in the background, so the publish success message does not wait on Zapier. If the host cannot start a background process, JReviews sends it during publish.

In the JReviews control panel, run Clear Cache & File Registry so JReviews loads the new files.

Without Early Access

Create the listener provider

Create this file so JReviews runs the Zapier file when a listing is first published.

Joomla. Create templates/jreviews_overrides/events/providers/zapier_listener_provider.php.

WordPress. Create jreviews_overrides/events/providers/zapier_listener_provider.php.

Save this complete file.

<?php

defined('MVC_FRAMEWORK') or die;

use Clickfwd\Listener\ListenerProvider;

class ZapierListenerProvider extends ListenerProvider
{
	protected $listen = [
		'JReviews\Events\ListingWasFirstPublished' => [
			'JReviews\Listeners\SendNewListingToZapier',
		],
	];
}

JReviews runs this listener when a new listing becomes published, including when a moderator accepts it. It does not run for later edits. To send updates as well, add JReviews\Events\ListingWasUpdated to the same $listen array and point it at the same listener.

Create the listener

This file collects the listing title, summary, category, and URL, then posts them to your Catch Hook.

Joomla. Create templates/jreviews_overrides/events/listeners/send_new_listing_to_zapier.php.

WordPress. Create jreviews_overrides/events/listeners/send_new_listing_to_zapier.php.

Save this complete file. Put the Catch Hook URL you copied into the $webhook value.

<?php

namespace JReviews\Listeners;

defined('MVC_FRAMEWORK') or die;

use Clickfwd\Event\EventInterface;
use Clickfwd\Listener\QueueableListener;
use JReviews\App\Commands\SendWebhookCommand;

class SendNewListingToZapier extends QueueableListener
{
	protected $webhook = 'https://hooks.zapier.com/hooks/catch/YOURID/YOURHOOK/';

	public function handle(EventInterface $event)
	{
		$listing = $event->getListing();

		if (! $listing) {
			return;
		}

		$fields = [];

		foreach ($listing['Field']['pairs'] ?? [] as $name => $field) {
			$text = $field['text'] ?? '';
			$fields[$name] = is_array($text) ? implode(', ', $text) : (string) $text;
		}

		$payload = [
			'title' => $listing['Listing']['title'] ?? '',
			'summary' => strip_tags($listing['Listing']['summary'] ?? ''),
			'description' => strip_tags($listing['Listing']['description'] ?? ''),
			'category' => $listing['Category']['title'] ?? '',
			'directory' => $listing['Directory']['title'] ?? '',
			'owner' => $listing['User']['username'] ?? '',
			'listing_url' => $event->getListingUrl(),
			'main_media_url' => \S2Array::get($listing, 'MainMedia.media_info.image.url', ''),
			'fields' => $fields,
		];

		SendWebhookCommand::dispatch($this->webhook, $payload, [
			'origin' => 'zapier',
			'ref' => 'listing.'.($listing['Listing']['listing_id'] ?? ''),
		]);
	}
}

The keys in $payload become the fields you map in Zapier. Add or remove keys in $payload when you need different columns. main_media_url is empty unless the listing already has a main photo when it is first published. Upload the main photo before you publish, or accept the listing from moderation after the photo is in place.

JReviews sends the listing data in the background, so the publish success message does not wait on Zapier. If the host cannot start a background process, JReviews sends it during publish.

In the JReviews control panel, run Clear Cache & File Registry so JReviews finds the new files.

Publish a test listing

  1. Create a restaurant listing titled Harbor Grill, with a short summary, in the Restaurants category.
  2. If new listings go live on submit, publish Harbor Grill. If new listings wait for approval, submit Harbor Grill, then accept it in moderation.
  3. After you publish, open Browse → Logs. Choose the Webhook channel. Confirm a Webhook sent row for hooks.zapier.com. If that row is missing, wait a few seconds and refresh the log. Then confirm the Catch Hook URL in the listener matches the Zap.
  4. Return to the Zap. Open the Catch Hook step and run Test. You should see title as Harbor Grill, with summary, category, listing_url, and the remaining keys from the listener file. If Zapier reports no sample, publish another listing after the log shows Webhook sent.

Add the Google Sheets row

  1. Create a Google Sheet named Published restaurants with a header row. Use the columns Title, Summary, Category, and Listing URL.
  2. In the same Zap, add a Google Sheets action. Choose Create Spreadsheet Row.
  3. Choose that spreadsheet and worksheet. Map Title to title, Summary to summary, Category to category, and Listing URL to listing_url.
  4. Turn the Zap on.
  5. Publish a second restaurant listing titled Cedar Cafe. A new row appears with Cedar Cafe, its summary, Restaurants, and the listing URL.

Keep this sheet only for rows Zapier adds from published listings. The next Zap uses a different sheet.

Create listings from a Google Sheet

This Zap sends a new sheet row to a URL on your site. That site URL is not the Catch Hook URL from the first Zap. Anyone who knows the site URL and this token can create listings. Choose a token of at least 20 characters, for example n8kR2wQp7LmX4vYc9bHt. Paste that exact string into the file below and use it as a Zapier field name. Keep the URL off public pages. Replace the example before you save the file.

Create the webhook controller

Joomla. Create templates/jreviews_overrides/app/Http/Controllers/CreateZapierListingController.php.

WordPress. Create jreviews_overrides/app/Http/Controllers/CreateZapierListingController.php.

Save this complete file. Replace 23 with your Restaurants category ID. Replace n8kR2wQp7LmX4vYc9bHt with your token.

<?php

namespace JReviewsOverrides\App\Http\Controllers;

defined('MVC_FRAMEWORK') or die;

use FWD\Illuminate\Http\Request;
use JReviews\App\Actions\StoreListingAction;
use JReviews\App\Enums\ListingModerationIntentEnum;
use JReviews\App\Enums\ListingStatusEnum;
use JReviews\App\Events\ListingWasCreated;
use JReviews\App\Events\ListingWasFirstPublished;
use JReviews\App\Models\JReviewsCategory;
use JReviews\App\Models\Listing;
use JReviews\App\Models\User;

class CreateZapierListingController
{
	protected $zapierToken = 'n8kR2wQp7LmX4vYc9bHt';

	public function __invoke(Request $request)
	{
		if (! $request->has($this->zapierToken)) {
			return fwd_response()->json(['success' => false, 'message' => 'Access denied'], 403);
		}

		$title = trim((string) $request->input('title', ''));

		if ($title === '') {
			return fwd_response()->json(['success' => false, 'message' => 'Title is required'], 422);
		}

		$categoryId = 23;
		$category = JReviewsCategory::query()
			->coreExtension()
			->with('listing_type')
			->firstWhere('id', $categoryId);

		if (! $category?->listing_type?->exists) {
			return fwd_response()->json(['success' => false, 'message' => 'Category not found'], 422);
		}

		$user = User::query()->first();

		if (! $user) {
			return fwd_response()->json(['success' => false, 'message' => 'No user account is available to own the listing'], 422);
		}

		$listing = Listing::make();
		$listing->owner_id = (int) $user->id;
		$listing->setRelation('listing_type', $category->listing_type);

		$data = [
			'title' => $title,
			'summary' => (string) $request->input('summary', ''),
			'cat_id' => $categoryId,
			'status' => ListingStatusEnum::Published,
		];

		if (jr_joomla()) {
			$data['owner_alias'] = (string) $request->input('author', '');
		}

		$storeListingAction = new StoreListingAction();
		$storeListingAction->onPublishListingCallback(
			fn (Listing $saved) => ListingWasFirstPublished::dispatch($saved)
		);

		$listing = $storeListingAction->handleWithModerationIntent(
			$user,
			$listing,
			$data,
			ListingModerationIntentEnum::UseAuthorizedSubmittedStatus
		);

		ListingWasCreated::dispatch($listing);

		return fwd_response()->json(['success' => true, 'message' => 'Listing was successfully created']);
	}
}

The token field is the permission for this URL. A request without that field name returns access denied and does not create a listing. The controller publishes the listing in category 23 (your Restaurants ID) even when nobody is signed in. Do not map the sheet Category column. Change the category only by replacing 23 in the file.

Register the route

Joomla. Create templates/jreviews_overrides/routes/web.php.

WordPress. Create jreviews_overrides/routes/web.php.

If routes/web.php does not exist, save this complete file.

<?php

defined('MVC_FRAMEWORK') or die;

$this->app['router']->post('/zapier/listings', [\JReviewsOverrides\App\Http\Controllers\CreateZapierListingController::class, '__invoke']);

If routes/web.php already exists, add the marked line at the end of the file. The copy button copies only that line.

<?php
defined('MVC_FRAMEWORK') or die;

$this->app['router']->post('/zapier/listings', [\JReviewsOverrides\App\Http\Controllers\CreateZapierListingController::class, '__invoke']);

Keep overrides enabled in JReviews configuration. After you save both files, run Clear Cache & File Registry.

Point Zapier at the site URL

Joomla. The site URL is:

https://www.example.com/index.php?option=com_jreviews&format=raw&url=zapier/listings

Replace www.example.com with your site host.

WordPress. The site URL is:

https://www.example.com/index.php?plugin=jreviews&url=zapier/listings

Replace www.example.com with your site host.

Create the webhook controller

Joomla. Create templates/jreviews_overrides/controllers/zapier_listing_feed_controller.php.

WordPress. Create jreviews_overrides/controllers/zapier_listing_feed_controller.php.

Save this complete file. Replace 23 with your Restaurants category ID. Replace n8kR2wQp7LmX4vYc9bHt with your token.

<?php

defined('MVC_FRAMEWORK') or die('Direct Access to this location is not allowed.');

class ZapierListingFeedController extends MyController
{
	protected $zapierToken = 'n8kR2wQp7LmX4vYc9bHt';

	public function index()
	{
		$post = $this->data;

		if (! isset($post[$this->zapierToken])) {
			return $this->response->html()->access_denied();
		}

		\Clickfwd\Hook\Filter::add('can_create_listing', [$this, 'can_create_listing'], 1);
		\Clickfwd\Hook\Filter::add('can_use_editor_in_listing', [$this, 'can_create_listing'], 1);
		\Clickfwd\Hook\Filter::add('trusted_on_create_listing', [$this, 'can_create_listing'], 1);

		$_POST[cmsFramework::getToken()] = 1;

		$categoryId = 23;
		$summary = $post['summary'] ?? '';

		$params = [];
		$params['data']['consent'] = true;
		$params['data']['name'] = $post['author'] ?? '';
		$params['data']['created_by_alias'] = $post['author'] ?? '';
		$params['data']['Listing'] = [
			'catid' => $categoryId,
			'title' => $post['title'] ?? '',
			'introtext' => $summary,
			'fulltext' => '',
		];
		$params['data']['__raw']['Listing'] = [
			'introtext' => $summary,
			'fulltext' => '',
		];

		$output = json_decode($this->requestAction('listings/_save', $params), true);

		if (! empty($output['success'])) {
			return $this->response->html()->success('Listing was successfully created');
		}

		return $this->response->html()->fail('Failed to create listing');
	}

	public function can_create_listing($permission, $params)
	{
		return true;
	}
}

These lines let Zapier create a published listing even when nobody is signed in. They also tick the consent checkbox so a required consent field does not block the save. They apply only to this webhook, not to the public listing form.

The controller always creates the listing in category 23 (your Restaurants ID). Do not map the sheet Category column. Change the category only by replacing 23 in the file.

After you create the controller, run Clear Cache & File Registry.

Point Zapier at the site URL

Joomla. The site URL is:

https://www.example.com/index.php?option=com_jreviews&url=zapier_listing_feed

Replace www.example.com with your site host.

WordPress. The site URL is:

https://www.example.com/index.php?plugin=jreviews&url=zapier_listing_feed

Replace www.example.com with your site host.

  1. Create a second Google Sheet named Restaurant import with the same header row. Title, Summary, Category, and Listing URL.
  2. Create a second Zap. For the trigger, choose Google Sheets, then New Spreadsheet Row, and select Restaurant import.
  3. For the action, choose Webhooks by Zapier, then POST.
  4. Set URL to the site URL for your CMS above.
  5. Set Payload Type to Form.
  6. In Data, add these fields. Use your token as the last field name, not the example token if you replaced it.
Field name Value
title the sheet Title column
summary the sheet Summary column
author a name such as Zapier import
n8kR2wQp7LmX4vYc9bHt 1

The last field name must match the token you pasted into the PHP file. The value can be any non-empty string. Zapier only needs that field to be present. A request without that field name returns access denied and does not create a listing.

  1. In Restaurant import, add a row whose Title is Maple Diner and whose Summary is a short sentence.
  2. In the Zap, run Test on that row. Open Browse → Listings and find Maple Diner in Restaurants. The listing should be published. If Maple Diner is missing, the listing type requires a field the Zap did not send. Make that field optional on the listing type, then test again.
  3. Turn the Zap on. Add a sheet row titled River Loft. River Loft should appear in Restaurants without using the listing form.

If both Zaps watch the same sheet, a published listing adds a row that creates another listing. Keep Published restaurants for outbound rows and Restaurant import for inbound rows.

Learn more