Page specific PHP code

Phplugins is very powerful as it can be used to customize your Backlight 2 based site to an extend not possible with the Backlight admin interface. Sometimes, it is not desired, that such a customization targets the entire site, but only a single page or a group of pages. In this short post, I show you how to do this.

First I need a simple helper function to keep my code clear and legible:

function dlp_page_match($page) {
if (substr($_SERVER["REQUEST_URI"], 0, strlen($page)) == $page) {
      return 1;
   } else {
      return 0;

This helper function returns 1 when the target page path starts with $page. Otherwise, 0 is returned.

In following example, I show how I use it to introduce page specific code using the main_top() API function:

function main_top( ){
if ( $this->dlp_page_match('/about') ) {
      echo '....';
   } elseif ( $this->dlp_page_match('/contact') ) {
      echo '....';
   } elseif ( $this->dlp_page_match('/galleries/') ) {
      echo '....';
   } elseif ( $this->dlp_page_match('/galleries/san-francisco') ) {
      echo '....';
   } elseif ( $this->;dlp_page_match('/galleries/europe/') ) {
      echo '....';
   } elseif ( $this->dlp_page_match('/backlight/search') ) {
      echo '....';

As you can see, this can be used to select a single page or several pages on a lower level (eg, galleries) where some page(s) specific code is inserted. On my site I use this add breadcrumbs to my standard pages and to introduce gallery specific code.

See the documentation to learn more about phplugins or browse around my Backlight 2 phplugins blog posts

Did you like this post? Did you use the given code? Please consider supporting me by buying me a coffee!

Buy Me A Coffee


Leave a Reply

Your email address will not be published. Required fields are marked *

Web Design Consulting

Do you need help designing your web site or getting Backlight working the way you want? Contact me to discuss your idea or project.

Buy me a Coffee

If you like what I share here, please consider buying me a coffee or a print. This helps keeping me motivated to continue posting. Thank you!

Buy Me A Coffee