This is a test
...because you shouldn't have to pay a dime to keep your identity safe
Home » Computers & Internet » Wordpress Stuff » Jtags - A Wordpress Plugin by Jeremy Duffy
Jtag diagnostic: Image http://www.jeremyduffy.com/images/nonexistant.jpg was not found

What is Jtags?

Jtags is a plugin for Wordpress that allows you to call php functions from your posts using ordinary XML-like tags. A jtag is a series of name=value pairs where the first one listed (by convention) is the function name to call and the rest are parameters.

At its heart, it's a simple substitution routine. You write a jtag and it runs some code, does some formatting or whatever else you want, and outputs the result in place of where you had the jtag originally.

Lost? That's ok, keep reading.

What does it do?

It can do almost anything you want. Besides calling any php code you can think of, it's a Wordpress plugin so it has access to all the Wordpress functions, global variables, and template tags too.

I designed this to be a simple method for php coders like me to do more advanced things with their wordpress pages without having to create a custom template file or resort to weird hacks just to have some simple functionality in the middle of their post.

For example, take my jtags_link function. I wanted to be able to easily insert a link to any of my other pages without having to use the full url. By using a jtag to launch a function that looks up the url every time, not only do I write less code, but I prevent self pingbacks, and, most importantly, the links won't break if I change my permalink structure, the title of the article, the location of the article or anything else as long as the post_name (a.k.a slug) stays the same.

So in the jtags.php plugin file, I wrote a function called jtags_link. All jtags functions only take one parameter which is an associative array of name value pairs (this is to prevent code from breaking when you add new functionality later). The link function takes whatever value was passed to it, looks up the post/page using it's permalink, and writes the standard <a href=url_of_post/page>Page_Title</a> code for me.

All I need to do now is put a jtag in my post somewhere that looks like this:

<jtag link=contact/>

Which automatically does this for me:

Note: Based on user request, I now support the standard method of closing tags like thus: <jtag link=contact-me></jtag>. This resolves an issue that showed up when using the WYSIWYG editor.

Simple right?

Now, I quickly realized that I don't always want the page title to be the printed text for the link so I added a parameter.

<jtag link=contact write="let me know!"/>

Which let's me control what is written instead of using the link/post/page title:

The best part is that because this isn't a true function call (all parameters are passed in a single associative array), the new functionality doesn't break my previous posts. The main jtags function that extracts the parameters doesn't have to be modified either since it will automatically parse all name=value pairs in a tag and pass them together to the function.

Therefore, the only change I had to make was in the jtags_link function itself where I added a simple if statement to either write my sent text or the default of the page title if I hadn't sent any text. Of course, JTAGS can do many more useful things.

Jtags files

jtags.php

The main plugin file. Like all other plugins, it must be in your wp-content/plugins folder somewhere. You can edit anything in this file that you want, but be warned that you'll need to be cautious when upgrading later so that the new jtags file doesn't erase all your customizations. Merging will have to be done manually with you deciding which changes you made that you want to keep and which you don't.

But as long as you only want to add new functionality, you can just edit the jtags_user_functions.php file and leave jtags.php alone.

jtags.css

One thing you'll notice is that most Jtag functions involve formatted text and/or images in a box. For my page, the box looks like this:

The formatting of the Jtags box

There are custom styles that pertain to how Jtags functions look. These are in a file called jtags.css which must be placed in your theme root folder. While I could have easily left it in the same folder as the jtags plugin, doing so prevents you from editing the jtags.css file with Wordpress.

jtags_user_functions-default.php

This needs to be renamed to jtags_users_functions.php before it can be used. This contains global settings and any custom functions you've created. This file was created so that when you upgrade jtags, you won't over-write your customizations or personal functions.

I tried several times to move this file to the theme root like the css file, but it failed each time for some reason. For now, if you need to edit this, you'll have to do it the normal way (offline) isntead of through Wordpress editing functions.

Main Features

Error Notification

It can be easy to mess up a tag. You might change the way they function, have a typo, or point to an image you forgot to upload to your live server. Besides printing a custom error message right on your page (examples below of what happens when your tag is bad), I added an error table and corresponding function that will log errors into a database table called wp_jtags_error (assuming wp_ is your table prefix).

When people browse your site, any time they load an article with a bad tag, an entry to the error table will be made. This way, if you just check the error table now and then (or write a function to do it for you), you will be able to see which posts/pages need some repair.

Note that you don't have to worry about creating the table. My code checks to see if the table is there and will automatically create it if it's not.

Inline HTML and nested Jtags Support

In most Jtags parameter values, you can put HTML and nested jtags (to a degree). For example:

<jtag callout="Never get <jtag link=credit-monitoring write='credit monitoring'/> because it's a complete rip off!">

This will only work if you don't try to use quotes inside quotes (thus breaking everything). Using single quotes for the nested Jtags will work though.

Call like a function

If you directly code information into a template (as I do sometimes), but you still want to use jtags in the text, you can just call it like any other function like this <?php echo jtags("<jtag link=something write=something/>");?>. This is necessary because jtags is only auto-called when printing the-content.

General Jtags Information

Some basic information you'll want to know about how this stuff works.

  • The first name=value pair must be set the function name you want to call – This way you can have a link function, but also a link parameter for a different function.
  • Any single tag can't have a repeated name – This might be obvious, but you can't have <jtag link=something write="something" write="something_else"/>. The code will still work, but the jtags function will first set write=something and then will overwrite it by setting write=something_else as soon as it reads the next name=value pair.
  • Don't use spaces in values unless you quote them – If you want something like this : <jtag my_name=Jeremy Duffy/>, the "Duffy" will get lost because there's a space and my function can't tell that it belongs to my_name. Enclose it in single or double quotes and all will be well.

    Because Wordpress replaces quotes and single quotes with character codes, Jtags converts all character codes back to quotes. This may cause strange behavior in your pages if you weren't expecting it, but I haven't found a fix yet.

Also, the jtags box model that I talked about before has some common customizations available. These values will work on any jtags function that uses a box:

  • width=x – Sets a width for the box.
  • height=x – Sets a height for the box.
  • align=x – Sets an alignment for the box.
  • text_align=x – Sets the text alignment for the box.

Anyway, enough explanation. Here's what the base version of Jtags includes. Who knows? Maybe these functions are all you want and you won't have to code a single thing…

Included Functions

My personal copy of Jtags has more functions than this, but here are the ones that I figure would be useful to more people than just me.

JTAGS_CODE_CHUNK

It's nothing fancy, but the point is abstraction. If I have this particular disclaimer on 100 pages and I decide to change its color, I only have to change the code_chunk function since every one of those 100 pages uses the jtag instead.

Jtag<jtag code_chunk=partial_post/>
Output<div class='disclaimer'>This post under construction. This notice will disappear when the post is completed.</div>
Result
This post under construction. This notice will disappear when the post is completed.

For the most part, a code_chunk is just a bit of html. If you try to do something complex using php, function calls, and database queries, you're better of just making a new jtags function instead of using this.

Error Example

Jtag<jtag code_chunk=nonexistant/>
Error textJTAG ERROR: No nonexistant index defined

Note that this assumes that there's no code_chunk with an index of "nonexistant".

JTAGS_LINK

Looks up a post, page, or blogroll link and provides a link to it. Because it looks up the location every time, any changes to the link are automatic and won't break no matter what you do to it as long as you don't change the post_name value.

Parameter list

<jtag link=x [parameters]>
  • link=x, If you want to link to a blog entry or page, x must be the post/page's post_name value (called a Page Slug in the "edit page" screen for some reason).

    If you want to link to one of your blogroll entries, then x must be the link-notes or description field.

    Important! A recent feature that I added lets you specify a "default write" value for posts/pages by adding a custom field called "default_write". Whatever you put for the value will be printed as a link's "write" value by default when you don't select a custom write value. If you don't specify a write value or "default_write" value, the link function will use the title of the post/page instead.

  • write=x, where x is any word or phrase you want to printed. If the words you want printed includes a space, you'll need to surround x with single or double quotes. Ex. write=good, write=this-is-ok-too, write="Whoops! Need quotes now".
  • target=x, where x is a valid link target like "_new", "_self", or other (whatever would work in an anchor tag). Note for pages that are on your domain (your own site), the default target is always "_self". For pages that are off-site, you can choose the default by altering the default_target value in the jtags_user_functions.php file. If you leave the setting alone (which is "_new"), then links will open new pages when the link is pointing off-site, and "_self" for pages that are on your same domain.
  • name=x, where x is the value pointing to a named link (ex http://www.something.com/file.php#name).

Examples

Jtag<jtag link=portfolio/>
Output<a href='http://www.jeremyduffy.com/portfolio/' >My Web Portfolio</a>
ResultMy Web Portfolio

The two most important reasons to use this instead of just an href tag (besides it takes less writing) are:

  1. You won't get a pingback to yourself from linking to your own site because this isn't a standard link
  2. If you change the location of the file, this will automatically point to the new location because it looks it up each time.

So what if you don't want it to print the post_title? What if you want it to say something else? Use the "write" parameter like this:

Jtag<jtag link=portfolio write="See my awesome web gallery here!" />
Output<a href='http://www.jeremyduffy.com/portfolio/' >See my awesome web gallery here!</a>
ResultSee my awesome web gallery here!

Which does the exact same thing, but it uses your specified text instead of the default. Note that you can specify a default_write value on a page using the "Custom Field" function so that a link will always write the value you want (for cases where the write value you want isn't the page title).

The default write value for this page. Now I can just do a jtag link=jtags to point to this page and it will write ''Jtags'' instead of the page title.
The default write value for this page. Now I can just do a jtag link=jtags to point to this page and it will write ''Jtags'' instead of the page title.

So if you specify a write value, that will be written. If not, it will use the default_write value for the post/page. If that isn't set, it will use the post/page title.

Note that this will also work for links in your blogroll, but since the links table has no equivalent to post_name, I used the link_notes field as a "post_name" equivalent. In other words, every time I add a link to someone (say cnn.com), I put a value in the link_notes field that makes it easy to call with my jtag function (probably just "cnn" in this case). Then I just call <jtag link=cnn/> and it's done!

One warning though. If you have a post/page with a post_name of "cnn" already, you'll need to come up with something else for the link or it will just point to the post/page every time.

Because it's easier and makes more sense, you can use the "description" field as the index for the blogroll instead of the link notes field. For backwards compatibility and because I couldn't think of a reason not to, it will work either way (in the notes or description field).

For links that you use frequently, but aren't one of your posts/pages, make a blogroll entry for it using the description or link-notes field as the trigger for the jtags_link function (in this case, <i>jtags-plugin</i> will link to the download file for the plugin)
For links that you use frequently, but aren't one of your posts/pages, make a blogroll entry for it using the description or link-notes field as the trigger for the jtags_link function (in this case, jtags-plugin will link to the download file for the plugin)

Error Example

Jtag<jtag link=nonexistant/>
Error textJTAG ERROR: nonexistant doesn't appear to be a post_name, link_note, or url

Once again, if I actually had a post, page, or link that used "nonexistant" as it's post_name/link_note, this link would suddenly work instead of showing the error

JTAGS_SMILE

Jtag<jtag smile=':*('/>
Output<img src='/images/smiles/smile_cry.gif'/>
Result

The piont of this is that Wordpress does change smile shortcuts to smile pics, but only for some. There are plenty of smiles they don't cover and for that, I have this function .

Note that this could be easily changed to a more generic "icon" function and used for any number of pictures other than just smiles.

Honestly I don't use this function much, but you might….

Error Example

Jtag<jtag smile=*(())*/>
Error textJTAG ERROR: *(())* not defined as a smiley.

JTAGS_HIDDEN

This is a fairly useful thing. It creates a hidden block of code that can be toggled on and off with a [+] and [-] sign. I use it to keep stuff out of the way that I think someone may not be interested in (but they can open it if they are).

Parameter List

<jtag hidden=unique_name [parameters]>
A bunch of text and code. Can be any combination of text, html, images, jtags, or anything else because it's just a regular part of the page that happens to be hidden temporarily.
<jtag hidden=end/>

hidden=x – This must be a unique id because it's what the javascript functions use to know which section to open when it's clicked. If you find that a hidden section isn't opening or is opening a different section, you probably didn't use a unique id for all the hidden sections. Generally single words or multiple words separated by underscores (not dashes!) is good as long as there are no two hidden sections on the same page with the same hidden value at the same time.

title=x – What you want the hidden section to be titled

h=x – A heading value to use for the title. The default is 3 I think, but you can make it anything you want. If you don't typeically use H tags for styling headers, all this means is that the lower the number, the bigger the text (down to 1).

closed_text=x – Any amount of text, html, and even jtags that you want displayed when the hidden section is closed. You can leave this blank if you want and it will still work well. For examples of how this works, check out this page.

And of course it supports a width and alignment value as do all jtag functions that use boxes.

Examples

Jtag<jtag hidden=uniqueID1 title="Test Hidden Area" closed_text="This area is hidden, click the [+] to open it!" width=90%/>
<jtag callout="Code, text, or anything else. Hide it all!"/>Use a hidden section to make parts of your page hidden from view until the user decides they want to see it. This can make it cleaner and more professional looking as well as keeping your content from overwhelming them all at once.
<jtag hidden=end/>
Output<div id='uniqueID1_off' class='hidden_closed_section' >
<div class='hidden_title'
onclick="document.getElementById('uniqueID1_off').style.display='none'; document.getElementById('uniqueID1_on').style.display='block'" width=90%>
[+] Test Hidden Area
</div>
<div>This area is hidden, click the [+] to open it!</div>
</div>
<div class='hidden_open_section'
id='uniqueID1_on' style='display:none'> <div class='hidden_title' onclick="document.getElementById('uniqueID1_off').style.display='block';
document.getElementById('uniqueID1_on').style.display='none'" width=90%>[-] Test Hidden Area</div>
<jtag callout="Code, text, or anything else. Hide it all!"/>Use a hidden section to make parts of your page hidden from view until the user decides they want to see it. This can make it cleaner and more professional looking as well as keeping your content from overwhelming them all at once.

Also keep in mind that you can use any amount of code between the opening and closing hidden tags.
</div>
Result

[+] Test Hidden Area

This area is hidden, click the [+] to open it!

The neat part about this is that you can have any kind of code or html or other jtags that you want in between the two tags. Just make sure to call the hidden=end part or the whole rest of your page will disappear.

Error Example

Note that there's really no error code that I can put for this. If I tried hard enough, I could make it keep track of the uniqueIDs and warn you when you tried to reuse them, but I didn't think it was really worth the trouble.

If you forget and use the same id twice, you'll figure it out pretty fast when your page doesn't work.

JTAGS_RELATED

It's useful to point people to related information now and then. Use this to make a nice box that works just like all the others. The width and alignment are optional, but the default is 100% width and centered.

Jtag<jtag related=about,portfolio,contact width=200 align=left/>
Output<div class='related' style='float:left;margin-right:5px; width:200px'>
<div class='related_header'>Related topic(s):</div>
<a href='http://www.jeremyduffy.com/about/' >About Me and This Site</a>
<a href='http://www.jeremyduffy.com/portfolio/' >My Web Portfolio</a>
<a href='http://www.jeremyduffy.com/contact/' >Contact</a>
</div>
Result

It's cake to use. You can use anything the jtag_link function understands in the comma separated list. By default, it will just print the url (since it doesn't have anything to look up to use instead), but you can specify a custom write value by using a special notation (shown below):

Jtag<jtag related="contact,http://www.jeremyduffy.com,http://www.jeremyduffy.com>Custom write" width=250 align=left/>
Output<div class='related' style='float:left;margin-right:5px; width:250px'>
<div class='related_header'>Related topic(s):</div>
<a href='http://localhost/contact/' >Contact</a>
<a href='http://www.jeremyduffy.com'>http://www.jeremyduffy.com</a>
<a href='http://www.jeremyduffy.com'>Custom write</a>
</div>
Result

Error Example

Jtag<jtag related=about,nonexistant,contact width=200 align=left/>
Error text

Since this uses jtags_link, the error will be the same.

JTAGS_CALLOUT

Jtag<jtag callout="Here's a "callout". It's used to call attention to something in your text or to help people quickly see what this area is about. Note that it supports nested <jtag link=jtags/>." width="300" align=left/>
Output<div class='callout' style='float:left;margin-right:7px; width:300px'>Here's a "callout". It's used to call attention to something in your text or to help people quickly see what this area is about. Note that it supports nested <a href=http://www.jeremyduffy.com/{url to jtags page}>Jtags</a>.</div>
Result
Here's a ''callout''. It's used to call attention to something in your text or to help people quickly see what this area is about. Note that it supports nested Jtags


As with all box functions, this will support width,height,align, and text_align.

Error Example

Since callout will just print out whatever you send, there's no error defined for it.

JTAG_FIGURE

This is one of my favorites and I use it constantly. It creates a "figure" which is an image sourrounded by a box with some text (a "caption"). You can have the image link to something such as a url or a larger image, you can set the width of the image or the box, and you can use it with or without any of these parameters.

Parameters

  • figure=x, where x is the url to an image to use. Because it saves a lot of time, figure will use relatives urls from your graphics_root (a value you set in your jtags_user_functions.php file). So if your graphics root is the default (http://yoursite.com/images) and your image was in http://yoursite.com/images/something.gif, you could write figure=something.gif instead of a full url.
  • caption=x – Caption will be the text listed under the image. Note that by default, the caption is also used as the "alt" text for the image.
  • url/link=x – Url (or link) can be anything that Jtags_link understands. For example, a post_name, a blogroll entry, or a regular fully formed url. Note that you can use either url or link. The result is the same.
  • img_width=x In some cases, you will use an image, but it's too big for what you want on a particular page. Use img_width to modify the width of the image itself.
  • img_height=x Used to manually set the height of the image.
  • th – Used to manually specify that the figure function should search for thumbnailed versions of images. If this is set (or if you set the thumb_search value in jtags_user_functions.php), it will look for a thumbnailed file named exactly the same but with a _th or _s suffix and point to that instead (explained in an example below).

And of course box values work as well (height, width, align, and text-align).

Examples

Jtag<jtag figure="books.jpg" caption="This links to my books page" url="books" align=left/>
Output<div class='figure' style='float:left;margin-right:5px; width:78px'>
<a href='http://www.jeremyduffy.com/books/'>
<img class=figure_img src='http://www.jeremyduffy.com/images/books.jpg' alt="This links to my books page" width='78px' />
</a>
<div class=figure_text>This links to my books page</div>
</div>
End result
This links to my books page
This links to my books page


One neat thing figure can do is automatically check for and use thumbnails.

Jtag<jtag figure="game_computer/xoxide_computer_case.jpg" caption="Since I set the th flag, this will automatically show a thumbnail that uses _th or _s in its name and link to the larger image" align=left th=1/>
Output<div class='figure' style='float:left;margin-right:5px; width:150px'>
<a href='http://localhost/images/game_computer/xoxide_computer_case.jpg'>
<img class=figure_img src='http://localhost/images/game_computer/xoxide_computer_case_th.jpg' alt="Since I set the th flag, this will automatically show a thumbnail that uses _th or _s in its name and link to the larger image" width='150px' />
</a>
<div class=figure_text>Since I set the th flag, this will automatically show a thumbnail that uses _th or _s in its name and link to the larger image</div>
</div>
End result
Since I set the th flag, this will automatically show a thumbnail that uses _th or _s in its name and link to the larger image
Since I set the th flag, this will automatically show a thumbnail that uses _th or _s in its name and link to the larger image


There are three keys for this to work properly.

  1. The thumbnailed image must have _th or _s in the name. So if "big.jpg" is your image, your thumbnail must be named "big_th.jpg" or "big_s.jpg".
  2. The thumbnail must be in the same folder as the big image.
  3. Either the th=1 flag must be set in the jtag or the thumb_search value in jtags_user_functions.php must be set to 1.
Note that the default behavior of the function is to automatically link to the larger file from the thumbnail unless you specify a different url/link for the link to go to.

If you want to avoid thumbnail search (to save processing time) for an individual image, use th=0. If you want to avoid it as a rule, change the value of thumb_search in the jtags_user_functions file.

Error Example

Jtag<jtag figure=nonexistant.jpg caption="Dude! Where's my image?" align=left url="http://www.jeremyduffy.com"/>
Error
Dude! Where's my image?
Dude! Where's my image?


Note that in this case, the majority of the code works fine, just that it prints a blank image instead. The error is still logged in the error table, but the code will automatically substitute a "no image" of your choosing. For me, the file is called "no_image.gif" and is in my graphics root.

I've included the no_image.gif file in the plugin, but you can use any custom "missing image" pic you want.

JTAGS_HT

I was originally using code_chunk for this, but this works better. To set this up, put a link in your blogroll like this:

setting up your blogroll to work with jtags
setting up your blogroll to work with jtags

Once you have a link set up (with either the "description" or "notes" field as the "index/slug"), use HT like this:

Jtag<jtag ht=slashdot/>
OutputH/T to <a href=http://slashdot.org>Slashdot</a> for the link!
Result(H/T to Slashdot for the link)

So one more time, this jtag looks for a link in your blogroll with either a description or "notes" field entry of "slashdot". When it finds it, it prints a link with the url being the blogroll url and the printed text being the blogroll name.

Note that you can use the write= modifier just like everything else:

Jtag<jtag ht=slashdot write="Dem guys!" />
OutputH/T to <a href=http://slashdot.org>Dem guys!</a> for the link!
Result(H/T to Dem guys! for the link)

Error Example

Jtag<jtag ht=nonexistant/>
Error text(H/T to JTAG ERROR: nonexistant doesn't appear to be a post_name, link_note, or url for the link)

Download the plugin

Feel free to modify the plugin as much as you like so long as you include author credit for me (please don't be a theiving shmuck).

Here's the link to download the plugin files. Enjoy!

Download the Jtags plugin by clicking this image
Download the Jtags plugin by clicking this image

[+] CHANGELOG

History of changes. This is also at the top of the Jtags.php file, though I think that in future versions, it will just be the most recent changes instead of the whole list.


Did this post help you?


Vote This Post DownVote This Post Up
Loading ... Loading ...
If this information helped improve your privacy, security, or peace of mind, please consider providing a donation to support more work like this! OR Have Jeremy come speak to your group, team, or organization! For more information about the seminar, click here.

Article at Random

Unlocker - Force Delete of Files Windows XP Won't Let You

It's hard to describe the pure, violent RAGE I feel every time I try to delete a file and Windows says you can't without any way to over-ride it and MAKE IT DELETE. (I'm getting mad just thinking about it now) Enter Unlocker. A simple, free utility that makes Windows work the way they should have...