Custom PHP Assignments

Custom PHP Publishing Assignments give you the opportunity to display your box for virtually any occassion you can think of. The only thing stopping you is having the PHP knowledge to code it and how far you are willing to go.

With Custom PHP Publishing Assignments you can get all the data available in the variables, URLs and even the database. Then you can do all kinds of checks on these data.

The main point of Custom PHP Publishing Assignments is that you always need to return either a TRUE or a FALSE value. For TRUE the assignment is valid and vice versa. Here's an example:

if ($variable == 'value') {
   return true;
} else {
   return false;
}
Or written in a more compact way:
return ($some_variable == 'some value');

Below are a (growing) collection of ready-to-use PHP assignment scripts. You can also use these as examples and starting-point to create your own custom scripts.

Assign to browser version

// Enter the browser type and version
$browserType = "msie";
$browserMinimumVersion = "9";

// Do not edit below
jimport('joomla.environment.browser');
$browser = JBrowser::getInstance();
return ($browser->getBrowser() == $browserType && $browser->getMajor() <= $browserMinimumVersion);

Assign to browser language

// Comma separated list of accepted languages
$acceptLangs = "en, fr";

// Do not edit below
$browserLang = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) : false;
return in_array($browserLang, explode(',', $acceptLangs));

Assign to articles

The Joomla! Articles Assignment is available as a standalone assignment.

// Comma separated list of allowed articles
$articles = "50, 51, 52";

// Do not edit below
if ($app->input->getCmd('option') != "com_content") { 
	return false; 
}
if ($app->input->getCmd('view') == "article") { 
	return(in_array($app->input->getCmd('id'), explode(",", $articles))); 
}

Assign to categories

The Joomla! Categories Assignment is available as a standalone assignment.

// Comma separated list of allowed categories
$categories = "50, 51, 52";

// Do not edit below
if ($app->input->getCmd('option') != "com_content") { 
	return false; 
}
if ($app->input->getCmd('view') == "category") { 
	return(in_array($app->input->getCmd('id'), explode(",", $categories))); 
}

Assign to certain (parts of) email addresses

// Comma separated list of (parts of) email addresses
$emails = '@yahoo.com,@hotmail.com';

// Do not edit below
$pass = 0;
foreach(explode(',', $emails) as $email) {
    if (!(strpos($user->email, $email) === false)) {
        $pass = 1;
        break;
    }
}
return $pass;

Assign to IP addresses

The IP Assignment is available as a standalone assignment.

// Comma separated list of IP addresses
$ips = '111.111.111.111,222.222.222.222';

// Do not edit below
return (in_array($_SERVER['REMOTE_ADDR'], explode(',',$ips)));

Assign to keyword (string) in page text

// String to be present in content area
$search = 'my search string';

// Do not edit below
$buffer = $doc->getBuffer('component');
if (!is_array($buffer) && !(strpos($buffer, $search) === false)) {
    return true;
} else {
    return false;
}

Assign to number of visits after new session

The Number of Pageviews Assignment is available as a standalone assignment. Pageviews and visits can be considered the same in this context.

// The maximum number of pageloads after a new session
$max = 10;

// Do not edit below
$session = JFactory::getSession();
return ($session->get('session.counter', 0) <= $max);

Assign to certain domain referer

The Referrer Assignment is available as a standalone assignment.

// Comma separated list of allowed domains
$domains = "facebook.com,twitter.com";
// Do not edit below $referer = isset($_SERVER['HTTP_REFERER']) ? parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST) : false; if ($referer) { $domain = str_ireplace("www.", "", $referer); return (in_array($domain, explode(',', $domains))); }

Time Range

The Time Range Assignment is available as a standalone assignment.

// Enter your time range here
$startTime = 'today 09am';
$endTime   = 'today 11am';

// Do not edit below
$startDate = strtotime(JFactory::getDate($startTime));
$endDate   = strtotime(JFactory::getDate($endTime));
$now   = strtotime(JFactory::getDate()->setTimeZone(new DateTimeZone($app->getCfg('offset'))));

return ($now >= $startDate) && ($now <= $endDate);

Homepage

Target homepage only with Custom PHP. The homepage can be selected in the Menu Items assignment as well.

$menu = $app->getMenu();
return ($menu->getActive() == $menu->getDefault());

Weekends (Saturday & Sunday)

Display a popup on weekends only.

// Comma separated list of allowed days. Monday = 1
$allowDays = array(6,7);

// Do not edit below
return in_array(JFactory::getDate()->format('N'), $allowDays);

Target User Cookies

Target users who have certain cookies stored in their browser. Keep in mind that the Cookie Assignment is available as a standalone assignment but it's limited to check a single cookie.

$cookies = ['some_cookie_a', 'some_cookie_b', 'some_cookie_c'];

// DO NOT EDIT BELOW
foreach ($cookies as $cookie)
{
    if (!$pass = (bool) $app->input->cookie->get($cookie))
    {
        return false;
    }
}

return true;
Get Engage Box for Joomla! for just 29€ Get it now!
Don't miss a thing!
Stay up-to-dated with my latest blog articles
Receive updates for my Joomla Extensions
No Spam! It's a promise.