Search
Other Sourcerer questions
Forum

How to add addCustomTag to head

Trevor Smith's Avatar Trevor Smith
I am trying to add the User ID to the head using addCustomTag to add a meta tag with little success.

The documentation indicates this might be possible, is there an example anywhere?

Thanks
Peter van Westen's Avatar Peter van Westen ADMIN
Yes, you can simply do:
{source}<?php
   $doc->addCustomTag('<whatever ...>');
?>{/source}

If you are placing this in a module of type 'Custom', make sure you switch on the 'Prepare content' option in the module.
Please post a rating at the Joomla! Extensions Directory
Trevor Smith's Avatar Trevor Smith
Thanks for the quick reply.

Yes, spotted the prepare content, and it is enabled.

I think the issue is getting the syntax correct for adding the user id variable into the meta tag content...

{source} <?php
$doc =& JFactory::getDocument();
$doc->addCustomTag('<meta name="userid" content="'$user->id'" ');
?>
{/source}
Trevor Smith's Avatar Trevor Smith
I have also tried...

{source} <?php
$doc =& JFactory::getDocument();
$doc->addCustomTag('<meta name="userid" content= "'<?php echo $user->id; ?>'" ');
?>
{/source}

but I think because of the nested php tag it gets changed to the below when saved, which breaks the page...

{source} <?php
$doc =& JFactory::getDocument();
$doc->addCustomTag('<meta name="userid" content="'<?php echo $user->id; ?>
'" '); ?&gt; {/source}
Peter van Westen's Avatar Peter van Westen ADMIN
{source}<?php
$doc->addCustomTag('<meta name="userid" content="' . $user->id .'">');
?>{/source}

If you do not know how php works, you shouldn't be using a power tool like Sourcerer.
PHP gives you a lot of power, enough to destroy your website.
Please post a rating at the Joomla! Extensions Directory
Trevor Smith's Avatar Trevor Smith
Thanks for the helpful advice 👎

I'm aware of the power of php, I have written plenty of php scripts, I'm just trying to add a very standard Joomla function to get the user id into a meta tag, via the Sourcerer plugin.

I think some of the issue lies with the combination of JCE Editor and Sourcerer, which I am getting resolved, but don't worry I'll figure it out myself then.
Peter van Westen's Avatar Peter van Westen ADMIN
Please read the documentation. There is a lot in there that might and probably will help you.
Please post a rating at the Joomla! Extensions Directory
Trevor Smith's Avatar Trevor Smith
Thanks, yes I have been through the documentation fairly thoroughly, but I couldn't find anything related to adding meta tags specifically

Obviously my error was mistakenly thinking this forum was a helpful resource that may get a response from a another member who has solved a similar problem in the past, apologies for my error.
Peter van Westen's Avatar Peter van Westen ADMIN
I am not sure why you are getting offended. I have tried to help by giving you the correct code to do what you want.

You give no feedback on what the result of that correct code is.

Reason why I assumed you do not have a high level of php knowledge:
You try to nest php tags (for starters).
Also you are using very old/outdated Joomla php code:
$doc =& JFactory::getDocument();

Reason why I assumed you did not read the documentation:
You are initialising the $doc variable, whilst the documentation says you don't need to do that.
And you say something about maybe JCE Editor being an issue. So probably your code gets messed up.
Either you are placing the code in the html view of the editor, instead of the WYSIWYG view.
Or JCE is setup so that it is not happy with the <...> tags. Which is also covered in the documentation.

Sorry if you took my technical advise and assumptions as something personal. That was not my intention.
Please post a rating at the Joomla! Extensions Directory
Trevor Smith's Avatar Trevor Smith
Ok, no problem , it's just a little frustrating when trying to troubleshoot an issue to effectively be told to go away you don't know what you're doing, but no offense taken.

I don't claim to be an expert, but have reasonable experience with coding and php, I just have a requirement I haven't needed to implement in the past and was looking for some advice, after many failed attempts, regarding inserting a meta tag via Sourcerer using the user id variable from Joomla into the content, I seem to be close but haven't successfully fixed it.

I turned to the forum as the plugin implements closely what I need to do and thought it likely somebody would have done something similar in the past. 🙂
Peter van Westen's Avatar Peter van Westen ADMIN
So again, with this code: what's the result and problem?
{source}<?php $doc->addCustomTag('<meta name="userid" content="' . $user->id .'">'); ?>{/source}
Please post a rating at the Joomla! Extensions Directory
Peter van Westen's Avatar Peter van Westen ADMIN
PS: Just tried it on my test setup and works fine.
Please post a rating at the Joomla! Extensions Directory
Trevor Smith's Avatar Trevor Smith
Thanks, that's helped a lot, interesting it didn't display anything for me using the above, but did using...
{source} <?php $doc->addCustomTag('<meta name="userid" content= "' . $user->id .'" '); ?> {/source}
which results in the following displayed in the head...

<meta name="userid" content="235" <="" head="">

Despite your code looking correct with the closing meta >
Peter van Westen's Avatar Peter van Westen ADMIN
...

Can you give me (super) admin access so I can take a look?
And tell me where you are placing this code and on what page you expect the result.
You can use the 'Hidden text' button in the forum editor to hide the confidential information.
Please post a rating at the Joomla! Extensions Directory
Trevor Smith's Avatar Trevor Smith
Sure...

Confidential information:
(hidden)


Thanks
Peter van Westen's Avatar Peter van Westen ADMIN
Please read this again:
Either you are placing the code in the html view of the editor, instead of the WYSIWYG view.
And read:
docs3.regularlabs.com/sourcerer
It's important to note that you should place the code in the WYSIWYG view of the editor, NOT in the HTML/Code view of the editor.
...
You can also use the Sourcerer editor button to make the process even easier.
Please post a rating at the Joomla! Extensions Directory
Trevor Smith's Avatar Trevor Smith
OK, thanks, yes the editor button seems to be the easiest way, and the code you illustrated does work inserted that way, great.

Thanks for persevering 😎
Manuela Babilon's Avatar Manuela Babilon
Hello, I'm trying to add the above example to my post. Unfortunately, the meta entry is not displayed in the source code. what am i doing wrong
Manuela Babilon's Avatar Manuela Babilon
Hi there,
So I managed it with the following code.

{source raw="true"}<?php $doc->addCustomTag('<meta name="userid" content="' . $user->id .'">'); ?>{/source}

Is there now a way to read the description of a specific zoo item from it.
Peter van Westen's Avatar Peter van Westen ADMIN
That is something you will need to ask Zoo support about.

Sourcerer is simply a tool to help you place PHP code and execute it.
What that PHP code is, is down to you.
Please post a rating at the Joomla! Extensions Directory
You can only post on the extension support forum if you have an active subscription and you log in

Buy a Pro subscription