Using phplugins in CE4

I am struggling with PHPlugin and using that to implement new Google Analytics in my old CE4 pages. This seems to be correct, in any page’s index.php:

// Begin hook processing

define( 'TTG_ROOT', __FILE__ );

define( 'TTG_HOOK', '/phplugins' );

define( 'TTG_COMP', 'TTG CE4-Gallery 4.0' );

if (file_exists( TTG_HOOK . '/phplugins.php')) {
	require_once TTG_HOOK . '/phplugins.php';
}

if (function_exists('ttg_user_load')) {
	$void = ttg_user_load( TTG_COMP, TTG_ROOT );
}

and then at the end of the head block:

<?php
if ( function_exists('ttg_head_end') ) {
	$void = ttg_head_end( TTG_COMP, TTG_ROOT );
} ?> 

</head> 

Then in phplugins.php file I inserted this

function ttg_head_end( $style, $path ) { 
	echo '
	<!-- Google tag (gtag.js) -->
	<script async src="https://www.googletagmanager.com/gtag/js?id=G-0NTS1TNG09"></script>
	<script>
  		window.dataLayer = window.dataLayer || [];
  		function gtag(){dataLayer.push(arguments);}
	  	gtag(\'js\', new Date());

		gtag(\'config\', \'G-0NTS1TNG09\');
	</script>
';
	return true;
} // END

But when I view the page/gallery that appears on the web, this code isn’t inserted into the page head. What am I doing wrong?

Have you enabled phplugins in the page template?
Did you include the server path to phplugins?

Yes, I checked the box, which generated the phpluginsample directory, which I renamed to phplugin, modified phplugin.php within it to insert my GAnlytics code, and uploaded to server. Then below that checked box in Web module I inserted that URL (https://[mysite].com/pics/phplugin) and once again generated CE4 Gallery. When I open the new index.php for the gallery, I see the core phplugin lines at the top, but the GAnalytics code has not been inserted.

That’s probably the problem. Instead of a URL, you need the server path.

If memory serves, you get that by putting your url to the phplugins file in a browser. The page returns the server path. Copy and paste that into the “Server Path to PHPlugins without trailing slash” field.
It should look something like this

/home2/yoursite/public_html/pics/phplugins

the first part depends on how your host has arranged things.

I just remembered something.
To get the server path, go to this url:
https://yoursite.com/pics/phplugin/path.php
that will return the server path.

I tried it both ways (internal server path discovered that way and external URL) and neither worked.

can you share a link to the gallery in question?

Have you altered the phplugins file in any way, other than to add your Google Analytics code?
Is that code in the User section?

So I started over from scratch. Created new gallery with “Enable PHPlugins” checked and path empty. Generated gallery, renamed ‘phplugins-sample’ subdirectory to ‘phplugins’ and uploaded gallery and all subdirectories to the Web, where it is now visible.

The plugin path is reported from here as /home/tedweins/public_html/pics/2024/20240116_Test/phplugins, which I entered below the “Enable PHPlugins’ checkbox. Then I deleted and regenerated the Gallery (which weirdly created phplugins-sample directory again), renamed /phplugins-sample directory to /phplugins. and then uploaded the full gallery again, where it is visible the same way, but without the red background that the css code within the phplugins/css directory (/public_html/pics/2024/20240116_Test/phplugins/css) says should be visible if things are working properly.

Because my ftp client reports the phplugins directory as
/public_html/pics/2024/20240116_Test/phplugins/
instead of what the path function reports as this
/home/tedweins/public_html/pics/2024/20240116_Test/phplugins
I tried that path instead, but identical results.

The index.php file shows phplugins installed:

?php
/*
 *
 * TTG Core Elements - "PHPlugins" User Hook interface
 * designed by john bishop images (http://johnbishopimages.com)
 * for Matthew Campagna of The Turning Gate (http://theturninggate.net)
 *
 */

// Begin hook processing

define( 'TTG_ROOT', __FILE__ );

define( 'TTG_HOOK', '/public_html/pics/2024/20240116_Test/phplugins' );

define( 'TTG_COMP', 'TTG CE4-Gallery 4.0' );

if (file_exists( TTG_HOOK . '/phplugins.php')) {
	require_once TTG_HOOK . '/phplugins.php';
}

if (function_exists('ttg_user_load')) {
	$void = ttg_user_load( TTG_COMP, TTG_ROOT );
}

?>

and at bottom of head section of index.php it properly includes

<?php
if ( function_exists('ttg_head_end') ) {
	$void = ttg_head_end( TTG_COMP, TTG_ROOT );
} ?>

Since still no red background was visible, then in phplugins.php I manually deleted the first and last lines (DELETE THIS LINE) of this section and reuploaded to server:

/* DELETE THIS LINE
// CUSTOM STYLESHEET
// This function implements a custom stylesheet from which users may provide additional page styling or override existing styling set by the plugin
// Edit the custom.css file in /phplugins/css/ to define styles
// Serves as an example of a function being applied globally
function ttg_head_end( $style, $path ) { 
	echo '
	<link rel="stylesheet" href="/phplugins/css/custom.css" />
	';
} // END
DELETE THIS LINE */

Still no luck - no red background. This is all prior to adding any GAnalytics code - the phplugins functionality doesn’t seem to be working, even within a single Gallery, where everything is pointed to locations and resources in that Gallery’s directories. What am I still doing wrong?

Here are what the directories look like on my hard drive and on my server, via ftp:


If you’re only going to use Google Analytics for this one gallery, then this server path should be the one you use:
/home/tedweins/public_html/pics/2024/20240116_Test/phplugins
Otherwise, place the phplugins folder in the “pics” folder and use this as the server path for all exported galleries
/home/tedweins/public_html/pics/phplugins

so change that server path first, as the one your using (that includes public_html) is incorrect.

Since you’re in a subfolder, you also need to provide the url to the custom.css file. That path assumes that the phplugins/ folder is in the root of the site. Try this instead:

<link rel="stylesheet" href="/pics/2024/20240116_Test/phplugins/css/custom.css" />

don’t worry about that. When it gets regenerate it, you can just leave it or delete it. The one you renamed to phplugins is the only one that matters.

Because I want to use this code on multiple galleries, can/should I have all the references be “…/…/…/phplugins/css/custom.css”?

Wait! Since now the css reference (even if it’s bad link) IS being correctly included, I now replaced that with the GA code, but which now bombs the page when I reload.

Here’s that new section in phplugins.php (pasted directly from GA:

function ttg_head_end( $style, $path ) { 
	echo '
	<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-0NTS1TNG09"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-0NTS1TNG09');
</script>
	';
} 

What’s the error there that causes the php to crash?

you can put the phplugins/ folder anywhere you like an as long as you use the same server path in each gallery, the GA code will be applied.
However, if you’re using custom css, you’ll need to put the path to the css file in the phplugins code that calls the custom css.

Are you exporting and uploading galleries from Lightroom or are you using TTG CE4 Publisher to create the galleries?

It could be the single quotes used in the code. The “echo” statement is followed by a single quote. When the next single quote is encountered, the echo statement ends and what comes after is likely causing the problem.

You can try a number of things. One would be to escape all the single quotes in the GA code by preceding them with a backslash: \
Or you could try changing them all to double quotes.

There’s a warning in the phplugins file at about line 23 about using single quotes.

Sorry - please ignore the css - that’s what’s built in to TTG’s phplugin - I was just using it as a test. I am using TTG Galleries within Lightroom to create and export galleries.

My ONLY need - and what’s causing the gallery pages to bomb - is inserting the new GA tracking code. Happy to send both phplugin.php and index.php, but I think all the relevant code is just this small section in phplugins.php:

function ttg_head_end( $style, $path ) { 
	echo '
	<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-0NTS1TNG09"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-0NTS1TNG09');
</script>
	';
}

Does any punctuation need to be changed? Is there a syntax incompatibility between the GA code they provide and the way TTG’s phplugin system runs?

see my reply just above about single quotes

Thanks - we were replying to each other at same time - that was it: changing
' to \'
solved the problem. Thank you SO much for the help.

:smile:

After that long, winding path to what was in the end a simple solution, just in case anyone else makes their way to this thread as GA4 rolls out and they want to update old, pre-Backlight galleries with new Google Analytics system, here are the key points when enabling and using phplugins and then adding the new GA4 code:

  1. Google-provided tracking code must have each of its single quotes modified from ' to \' when pasting in to ttg_head_end section of phplugins.php.

  2. interestingly, either relative (e.g. /home/yoursite/public_html/pics/phplugins) OR hard-coded URL (e.g. https://yoursite.com/pics/phplugins) will work for phplugins server path in Web module settings

  3. of course editing and placing the new phplugins.php in a site-level location allows simple updating of multiple galleries, which each just need to point to that single location.

Thanks again to Rod for the help.