1

Matthew Murphy asks the following on the Joomla Facebook Group:

Anyone know of an extension for content that replaces the name with the person who's logged in?

For example I have been working on a staff training section and I want to have an article like:

$Yourname, if you are reading this and are unclear of the difference between the two types of letters...

$Yourname would say Matthew instead.

  • Could you not persuade Matthew to post a question himself? We need more people to take the plunge. – mickmackusa Nov 4 at 12:56
  • Posting chunks of code doesn't work so well on the Facebook Group and trying to find anything later on Facebook is not easy. Hopefully Joomla Facebook Group users see the value of posting here. – Neil Robertson Nov 4 at 21:46
  • 1
    I am 100% in agreement. I am happy that you've made this page, I just wish the Facebooker would have posted the question here for you to answer. On gitter, I have asked multiple people to transfer their questions to JSE. I think you are a slack user, please route viable questions from there to here when you can. – mickmackusa Nov 4 at 21:49
  • 1
    Hopefully you mean "Slack" rather than "slack". :) – Neil Robertson Nov 5 at 0:17
1

The code required to extract the first name of the currently logged in user is fairly simple. Here's an example:

<?php
  $user = JFactory::getUser();
  if (!$user->guest) {
    echo strtok($user->name, ' ');
  }
?>

Using the free or paid version of Sourcerer you could insert the code directly into an article as follows:

{source}<?php $user = JFactory::getUser(); if (!$user->guest) {echo strtok($user->name, ' ');} ?>{/source}

For a more elegant solution, create a Custom HTML Module using the above code with Sourcerer like this:

{source}
<?php
  $user = JFactory::getUser();
  if (!$user->guest) {
    echo strtok($user->name, ' ');
  }
?>
{/source}

Specify a custom module position e.g. firstname and remember to set Prepare Content = Yes under the Options tab. Save and Close the module.

Inserting the first name of the currently logged in user into content can then be done more simply using: {loadposition firstname}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.