<p>This should do it for you:</p>

<pre><code>$entry-&gt;$fieldHandle-&gt;last()-&gt;uri;
</code></pre>

CC BY-SA 3.0
2015-07-03T01:24:09.927Z
2015-07-03T01:24:09.927Z
Brad Bell
<p>I'm trying to make a plugin more generally usable (so others could use it). For my own use, it uses a category field called 'mainCategory' but I'd like that the field handle could be specified in the plugin settings.</p>

<p>What I imagine is:</p>

<pre><code>$url = $entry-&gt;mainCategory-&gt;last()-&gt;uri;
</code></pre>

<p>becomes:</p>

<pre><code>$plugin = craft()-&gt;plugins-&gt;getPlugin('myPlugin');
$settings = $plugin-&gt;getSettings();
$fieldHandle = $settings-&gt;categoryFieldHandle;

$url = $entry-&gt;getField($fieldHandle)-&gt;last()-&gt;uri;
</code></pre>

<p>Any suggestions?</p>

CC BY-SA 3.0
2015-07-02T16:46:42.760Z
Wouldn't `$entry->$fieldHandle->last()->uri` work?
Brad Bell
CC BY-SA 3.0
2015-07-02T18:03:55.807Z
It does indeed. I had no idea you could put a variable after the `->`. Thank you.
kr37
CC BY-SA 3.0
2015-07-03T01:23:50.867Z
Cool... I'll go ahead and add that as an answer if you want to mark it as accepted.
Brad Bell
<plugin-development><entrymodel><entries-field>
How to use a dynamically defined field handle in a plugin?
# How to use a dynamically defined field handle in a plugin?

(Asked by: username_0 on 2015-07-02)

I'm trying to make a plugin more generally usable (so others could use it). For my own use, it uses a category field called 'mainCategory' but I'd like that the field handle could be specified in the plugin settings.
What I imagine is:
$url = $entry->mainCategory->last()->uri;

becomes:
$plugin = craft()->plugins->getPlugin('myPlugin');
$settings = $plugin->getSettings();
$fieldHandle = $settings->categoryFieldHandle;

$url = $entry->getField($fieldHandle)->last()->uri;

Any suggestions?

**Tags:** plugin-development, entrymodel, entries-field

> **Comments:**
> **username_1**: Wouldn't `$entry->$fieldHandle->last()->uri` work?  
> **username_0**: It does indeed. I had no idea you could put a variable after the `->`. Thank you.  
> **username_1**: Cool... I'll go ahead and add that as an answer if you want to mark it as accepted.  

---

## 1 answers

### Answer by username_1 on 2015-07-03. Score: 3 | Accepted answer

This should do it for you:
$entry->$fieldHandle->last()->uri;


---

