Blog

Easy custom plugins

January 11, 2012

Tags: ee, add-on development, plugins

Edit: I've recently (1/17/2011) given a short talk on this at the @TwinCitiesEE user group. Slides from that talk are here. Read on for more detailed discussion of the topic.

Imagine a scenario where you have some special data that you wish to query out of either built in ExpressionEngine tables or out of custom tables added by a third-party package.

Usually what most people would do at this point is resort to using the query plugin to obtain data from a custom query. However, I want to show you a better way to achieve the same kind of access to data in internal tables, but in a much safer and more controllable way.

First a little about why the query plugin isn't such a great idea in this situation. Generally, when we need to query tables in a MySQL database, we need to pass in some information that we have been given by the user or at least which has passed through the user's machine in the form of a link in a URL segment, or in a value posted to a form. For instance, you may want to get some custom member information based on a member ID. You could do this using the Query plugin like in the following example.

Imagine we have a template group named account and a template named member_info which performs this custom query. The root URL of our example site site is http://www.example.com/.

URL: http://www.example.com/account/member_info/45
Template: account/member_info

<h2>Member Info</h2>
{exp:query sql="SELECT * FROM exp_members WHERE member_id={segment_3}"}
	  Screen Name: {screen_name}<br/>
	  Registered Email Address: {email}<br/>
	  Username: {username}<br/>
	  <a href="{...
				

Read more...