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').

Links

Level

  • Beginner

<?php
# Method 1 using perch_page_path var. 
# Advantage not tied to specific domain

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


<?php
# Method 2 using 'url' var

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

Posted