If you are writing a hook that needs to run on a single page like product_info.php you would:
echo $OSCOM_Hooks->call('product_info', 'injectBodyContentEnd');
If you are writing a hook that needs to run on all pages then you would do this:
echo $OSCOM_Hooks->call('siteWide', 'injectBodyContentEnd');
However, if you want the hook to only run on a few pages like the create_account.php and password_forgotten.php pages, then you should write something like the following:
<?php /** * $Id: zipurSelect2.php * $Loc: catalog/includes/hooks/shop/siteWide/ * * zipurSelect2 * Version: 0.1.0 * Author: Preston Lord * aka zipurman / zipur.ca / plord@inetx.ca * * Revision Date: 08/05/2020 * Created to add https://select2.org to Phoenix * * Copyright (c) 2020: Preston Lord - @zipurman * All rights reserved. * Released under the GNU General Public License */ class hook_shop_siteWide_zipurSelect2 { public $pages_to_load = [ 'create_account.php', 'password_forgotten.php', ]; function listen_injectSiteStart() { if ( $this->show_areas() ) { return <<<files <link href="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/css/select2.min.css" rel="stylesheet" /> <script src="https://cdn.jsdelivr.net/npm/select2@4.1.0-beta.1/dist/js/select2.min.js"></script> files; } } /** * Check if this hook should be loaded on page * * @return bool */ function show_areas() { global $PHP_SELF; if ( in_array( basename( $PHP_SELF ), $this->pages_to_load ) ) { return true; } return false; } }
The above example shows the following:
- We have a hook file with a class of hook_shop_siteWide_zipurSelect2 which means that we should place the file in includes/hooks/shop/siteWide/ and we should call it zipurSelect2
- The listen_injectSiteStart method means that we are injecting into any of the injection points in the core that call using
echo $OSCOM_Hooks->call('siteWide', 'injectSiteStart');
- Notice that we also have a public array variable in the class called $pages_to_load
- This is an array of the files/pages that we want to load this hook into
- We then add another method of show_areas() to our class which simply compares the loaded page against the $pages_to_load array and returns true/false depending on if a match is found.
- Finally we add an IF around our hook logic if ( $this->show_areas() ) to limit the code to the array list of pages.
There is really no limit to the depth of how this logic could be setup.