If user is not on home page, then load MailChimp subscribe form with Perch Runway

Use Case

I ran into a nice little use case to better understand how to use PerchSystem class. If a visitor is on the home page, then load a Perch layout. And if a visitor is not on the home page, then load the Perch layout in the footer. The layout holds a MailChimp subscribe form. My solution used the PerchSystem class to test the current page a user is on and conditionally load a component in the correct position.

Solution

Use the PerchSystem class with get_var property. Tip: to print available vars use print_r(PerchSystem::get_vars());. In my example, I'll use get_var('url') and get_var('perch_page_path').

Method 1 using perch_page_path var.

Advantage not tied to specific domain

<?php


$pagepath = PerchSystem::get_var('perch_page_path');
if ($pagepath !== "/") {
  perch_mailchimp_form('forms/subscribe');
}
?>

Method 2 using 'url' var

<?php

$url = PerchSystem::get_var('url');
if ($url !== "https://scottgruber.me/") {
  perch_mailchimp_form('forms/subscribe');
}
?>

Links

Level

  • Beginner

Tags:


← Previous How to use Sass to convert hex colors to rgb Color Management Next →