5

I'm having an exceedingly difficult time trying to retrieve the Site URL and Name in a custom plugin I am developing.

My research has led me to believe this would work:

$envVar = craft()->config->get('environmentVariables');
$siteUrl = $envVar['siteUrl'];
$siteName = $envVar['siteName'];

But in practice this hasn't worked. Does anyone know the correct way?

6

You can get them via craft()->getSiteUrl() and craft()->getSiteName().

  • Perfect. Thank you. – Zack Brady Oct 2 '15 at 18:35
7

In Craft 3 you can do this:

use craft\helpers\UrlHelper;

UrlHelper::siteUrl()
1

What you've done should work fine for actual environment variables, but both siteUrl and siteName are proper config settings that you can access via craft()->config->get('siteUrl') and craft()->config->get('siteName').

  • I am still have issues retrieving the values in this way. Note: I am accessing them via an Event, specifically when an entry is saved. Would that have an impact on this? – Zack Brady Oct 1 '15 at 19:54
  • Shouldn't. Can you add your craft/config/general.php contents to the original question? – Brad Bell Oct 2 '15 at 0:38

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.