<p>Completely untested, but something like this should work:</p>

<pre><code>public function getPageUrl($slug)
{
    $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
    $criteria-&gt;slug = $slug;
    $entry = $criteria-&gt;first();

    if ($entry)
    {
        return UrlHelper::getUrl($entry-&gt;uri);
    }
}
</code></pre>

CC BY-SA 3.0
2014-11-19T10:44:27.243Z
Thanks again Brad, that's the second time you've pointed me in the direction of the Helpers. http://buildwithcraft.com/classreference#craft.app.helpers
Mark Notton
CC BY-SA 3.0
2015-07-14T13:05:59.363Z
You can even simplify that by just invoking the `getUrl()` method on the `$entry` object. There's no need for calling the `UrlHelper` (the `getUrl()` method is already using it). So, you can shorten the last line to this:
`return $entry->getUrl();`
Arvid
CC BY-SA 3.0
2014-11-18T18:40:16.277Z
2014-11-18T18:40:16.277Z
Brad Bell
<p>Wondered what the best approach would be to get the url for a given entry page using only a slug for reference? I imagined it being something like this:</p>

<pre><code> public function getPageURL($slug) {
    $page = craft()-&gt;elements-&gt;slug($slug);
    @return getURL($page);
  }
</code></pre>

<p>Usage example:</p>

<pre><code>&lt;a href="{{ craft.PLUGINNAME.getPageURL("staff") }}"&gt;Staff&lt;/a&gt;
</code></pre>

<plugin-development><elementcriteriamodel><slug>
Get a page URL using only the Slug?
# Get a page URL using only the Slug?

(Asked by: username_0 on 2014-11-18)

Wondered what the best approach would be to get the url for a given entry page using only a slug for reference? I imagined it being something like this:
 public function getPageURL($slug) {
    $page = craft()->elements->slug($slug);
    @return getURL($page);
  }

Usage example:
Staff

**Tags:** plugin-development, elementcriteriamodel, slug


---

## 1 answers

### Answer by username_1 on 2014-11-18. Score: 2 | Accepted answer

Completely untested, but something like this should work:
public function getPageUrl($slug)
{
    $criteria = craft()->elements->getCriteria(ElementType::Entry);
    $criteria->slug = $slug;
    $entry = $criteria->first();

    if ($entry)
    {
        return UrlHelper::getUrl($entry->uri);
    }
}

> **Comments:**
> **username_0**: Thanks again Brad, that's the second time you've pointed me in the direction of the Helpers. http://buildwithcraft.com/classreference#craft.app.helpers  
> **username_2**: You can even simplify that by just invoking the `getUrl()` method on the `$entry` object. There's no need for calling the `UrlHelper` (the `getUrl()` method is already using it). So, you can shorten the last line to this: `return $entry->getUrl();`  

---

