8

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?

3 Answers 3

6

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

0
13

In Craft 3 you can do this:

use craft\helpers\UrlHelper;
UrlHelper::siteUrl();

Craft::$app->sites->currentSite->name;
2
  • What about siteName? Dec 6, 2020 at 17:37
  • 1
    @good_afternoon I just updated my answer to include that. Dec 7, 2020 at 6:13
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').

2
  • 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, 2015 at 19:54
  • Shouldn't. Can you add your craft/config/general.php contents to the original question?
    – Brad Bell
    Oct 2, 2015 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.