Using Crayon Manually in PHP

Crayon can be used in any PHP environment so it’s possible to extend to another CMS other than WordPress. Here’s a sample of how you’d do that. This script works from the Crayon plugin directory, so you’d need to update any paths. Also make sure you reference correct urls and paths for the defined settings.

Crayon Tag Editor

I’m happy to announce that in 1.9.0, Crayon will feature a built in Tag Editor in both the Visual and HTML editors of WordPress! Normally, you’d have to type the shortcode in HTML editor and make sure you use the right language ID and so forth. Overriding default settings, like changing the theme, font and toolbar settings was more work than necessary. Now these settings are all built into the new user interface.

How It Works

  1. Use on the Visual Editor toolbar, or on the HTML Editor toolbar to open the Tag Editor.
  2. Use the Tag Editor to add code and change settings. Click “Add” to insert the code into the post.

  3. You can edit the generated <pre> tag in both editors. If you’re using the Visual Editor, you can select the code block with your mouse (it will become blue) and click the Tag Editor button again to open the same window and modify both the code and the settings. Then click “Save” when you’re done.

Inline Tags

You can also create and edit Inline Tags by ticking the “Inline Tag” option.

To edit the Inline Tag, once again select the generated <span> with your mouse cursor (it will become green) and click the Tag Editor button.

Overriding Settings

This makes editing code in WordPress super easy, and even more welcoming is the fact you can now override all of Crayons global settings for a single Crayon using the same interface you’re used to in the Settings screen. Any changes you make show up yellow and are automagically added to the code block, and you can then select the code block with your mouse and edit them later :)

Filter Posts With A Dropdown In WordPress

I made an unusually long answer on StackOverflow, thought I’d share here also. I don’t now about you but Crayon looks prettier to me :)

Almost 1000 views and not a single comment. Well, I also needed this and decided to make it. I’ve shared the JavaScript and WordPress code below for people in the distant future to use. It looks like a lot, but that’s because I’ve defined some jQuery functions you can use later with .extend. All it’s doing is looking for a select element (a dropdown) with CSS class .content-filter.

Once found, it uses the dropdown’s id to set a GET variable to the value currently selected, then it redirects to this the same URL and adds these GET variables. For example, if the id of the dropdown was product_filter, and this had a value set to date, then it would set the GET variable product_filter=date. It’s great because it doesn’t care about your Wordpess details – all it cares about is the select element.

Now the WordPress code. All we really need is to generate the select with some kind of id and set the class to .content-filter. This code asks for a post type like ‘post’ or ‘product’ and makes the select element. It then returns the GET variable for convenience, and if none is set then it defaults to ‘newest’. Notice that the $fields array sets all the different orderby values you’d like to support. You can always access it anywhere in the template with $_GET['product_filter'] or $_GET['post_filter'] depending on what your type is. This means that only one can exist on any given page, but you want that – otherwise jQuery won’t know which to use. You can extend this code to set a custom id or anything you like later.

Now the fun part – putting it together in the content page. All our work pays off with some sweet and short code:

I used the custom post type ‘product’, but if you’re using ‘post’ just replace that. Someone should probably make this into a plugin if they haven’t already :P

Word Limiting With Regular Expressions

Here’s my take on limiting words in a paragraph.

^\s*((?:\S+\s*){0,20})(.*)

That’ll capture the first 20 words and also the rest of the string. Then, you can replace the entire capture with the first capture group (the first 20 words). In PHP:

Removing Unfinished Sentences From Paragraphs

These are helpful after you strip a paragraph for a word limit and want to cut off the last incomplete sentence.

Of course, if you want to completely remove the last sentence, this will suffice: