Media Categories 1.2, Now Supports Gallery Shortcode

A few months ago I received suggested it might be a good idea to allow the gallery shortcode to retrieve attachments from a category as part of my Media Categories plugin.

So here it is, Media Categories 1.2 extends the native WordPress Gallery Shortcode to allow you to select a category to display via a newly added ‘category’ parameter which can take either a term_id OR a slug.

[gallery category='slug']

Normally the gallery shortcode only shows you images that have been attached to the current post, or a post id passed as an argument. This normal behavior is preserved with Media Categories 1.2, however when invoking the ‘categories parameter, the current post is ignored, unless its id is explicitly passed.

[gallery category='slug' id='12']

In the example above, the gallery shortcode will create a gallery of any images that in the category called ‘slug’ AND are attached to post id 12. If the id is not explicitly passed, then it will simply return all images from the category.

As I’ve stated, if you don’t invoke the category parameter, the gallery shortcode will behave as normal, so this plugin will not interfere with how galleries already in use will behave.

I have a few additional updates coming soon for this plugin, per another request I received via the plugin directory forums, version 1.3 will include a filter so that developers can change the taxonomy at will.

Thanks for using the plugin, as always I appreciate bug reports and feature requests, I hope you find good use for this new feature.


Plugin Update: Category Template Hierarchy 1.3.2.1

Download Category Template Hierarchy Version 1.3.2.1 from the WordPress Plugin Repository

This plugin has seen a flurry of bug fixes and updates since 1.3 was released and we’re now on version 1.3.2.1. I am really sorry for having so many updates back-to-back.

Everything since 1.3 is bug fixes, here is a summery of what was fixed.

  • `category.php` templates were not being loaded (somewhere down the line I must have removed it by mistake)
  • In cases where the current category was neither parent nor child, the plugin would fail to load anything from the hierarchy other
  • Lastly, I screwed up a commit, and the changes for 1.3.2 didn’t go out, hence 1.3.2.1

I really appreciate the feedback I’ve received from people using this plugin. These bug reports not only help me make my plugin better for you guys, but also for myself and my job  – where every one of my publicly released plugins is being used on production websites. These bug reports and public feedback really help to justify releasing our code publicly when we can, and open source as a whole.
Thanks you all!

Download Category Template Hierarchy Version 1.3.2.1 from the WordPress Plugin Repository


Plugin Update: Simple Hook Widget 2.0

Download Simple Hook Widget from the WordPress Plugin Repository

The Simple Hook Widget just got a little bit less simple.

Since I released this originally, it seems people have a hard time understanding what this plugin could possibly be good for. I can understand that given the vague nature of it being a  plugin for hooks.

Additionally, there is a signficant security problem in having this plugin turned on for a server where other non-developers have access to the widgets panel – primarily thats because this plugin lets you do ANYTHING !! Including the use of hooks that are part of core, and could cause serious problems if they are run at the wrong time (such as in the sidebar.

This update introduces the ability for developers to provide an array of hooks, and have those hooks show up as a drop down menu in the widget. Additionally, there is another hook which allows for a default value for that hook.

There is also a complete example packaged with this plugin, for how to setup your own list of hooks, how to set a default, and an exceedingly simple example of how to actually do something with a chosen hook.

Have fun, and please please please be CAREFUL using this plugin.

Download Simple Hook Widget from the WordPress Plugin Repository


Plugin Update: Category Template Hierarchy 1.3

Download Category Template Hierarchy Version 1.3 from the WordPress Plugin Repository

I’ve release an update to the Category Template Hierarchy plugin, a particular bug report prompted the update but there are a couple of other changes as well.

I use this plugin for three sites I’ve worked on for Sears / Kmart, so over time I make  minor changes to resolve issues I notice or improve how it works, but I don’t always have time to release those changes publicly.

A recent bug report forced to release a fix, and gave me an opportunity to update the plugin. Some of these changes effect how the plugin behaves, so it could impact your site if you depend on this plugin to keep your theme working. Here are the changes

  1. Improved performance be removing completely unnecessary logic to determine current category while building the list of potential templates to use. (No behavioral effect)
  2. Changed the filter used by the plugin. Before it used the very broad and general ‘template_redirect’ filter, but that caused obvious problem with other parts of the hierarchy, so not it uses the ‘category_template’.
  3. In addition to changing which filter is being used, the plugin used to `include()` the `get_query_template()` function, but now returns `locate_template`. This should resolve a bug where global variables such as $post were not available from within the loop.
  4. Logic in the `is_child_of_category()` and `is_parent_of_category()` has changed to check `!isset($child_category->parent) || !isset($parent_category)` instead of `empty($child_category) || empty($parent_category)`. This could effect your code if you use these conditional tags for anything in your theme.

The only item that does not effect any behavior is #1. Items #2 and #3 change the way the template hierarchy effect other parts of the native Template Hierarchy, so if you’ve seen strange behavior with non-category templates then you’ll definitely benefit from these changes. Item #4 is important as well, since it simply makes the conditional results much more accurate.

I don’t know how often people use the conditional functions, or how many of the couple thousand people who’ve downloaded this plugin actually use it at all – but I really appreciate the bug reports and any feedback. As I’ve said, I use these plugins myself on productions sites – so the public QA is always appreciated.

Download Category Template Hierarchy Version 1.3 from the WordPress Plugin Repository

Enjoy!


Simple Hook Widget – WordPress Plugin

Download: WordPress Plugin Directory

This widget allows the user to insert a hook, with a name of their choosing, in any sidebar.

The hook can be anything, an existing hook from the WordPress Core, a plugin, a theme, or something you’ve come up with on the fly. Once the hook exists, your plugins, your theme, or the WordPress Core to make something happen with that hook.

This can be used in conjunction with other more complex plugins, to give the user control of then things occur (yes, that is intentionally vague). It can also serve as a quick alternative to making very simple widgets tied to code from a theme. Say you have a chunk of code which already exists on your site, you’d like to also have it placed in a sidebar, but don’t want to make a widget out of it (since its entirely theme-centric). You could simply hook this chunk of code to a custom hook and use the Simple Hook Widget to place that custom hook in the sidebar. This is absolutely not an endorsement toward that strategy for widget development, but there may be times where such on option is useful to a developer in a pinch.

This widget also contains an internal hook, which will be your hook, prefixed with simplehookupdate_. So if you use this plugin to create a hook name ‘testhook’, the widget, aside from creating the ‘testhook’ in the chosen sidebar location, will also create a hook called ‘simplehookupdate_testhook’. This hook occurs within the update method of the WP_Widget class, immediately before $instance is returned.

All in all its very simple, and intentionally so. I hope developers can find some useful uses for it – I know I already have a very complex plugin in the works that will utilize this. Please feel free to make suggestions or ask questions.

 


Incorrect Datetime Bug Plugin – WordPress

Download: WordPress Plugin Directory

This plugin was create to work around a problem that -I have to assume- only occurs after a database migration, where certain configurations in MySQL lead to several odd symtoms including but not limited to:

  • Loss of ability to create new posts
  • Loss of ability to update posts
  • Admin interface reverts to lowest settings such as the ‘Publish’ button says ‘Submit Draft’.
  • Errors regarding invalid timestamps
In addition, this problem is often accompanied by an error like the following:
WordPress database error: [Incorrect datetime value: ‘0000-00-00 00:00:00’ for column ‘post_date_gmt’ at row 1]

INSERT INTO `mg_posts` (`post_author`,`post_date`,`post_date_gmt`,`post_content`,`post_content_filtered`,`post_title`,`post_excerpt`,`post_status`,`post_type`,`comment_status`,`ping_status`,`post_password`,`post_name`,`to_ping`,`pinged`,`post_modified`,`post_modified_gmt`,`post_parent`,`menu_order`,`guid`) VALUES (‘1′,’2011-08-23 03:32:43′,’0000-00-00 00:00:00′,”,”,’Auto Draft’,”,’auto-draft’,’post’,’closed’,’open’,”,”,”,”,’2011-08-23 03:32:43′,’0000-00-00 00:00:00′,’0′,’0′,”)

The inability to pass zero dates breaks the ability to actually create a new post, but it also seems to have the odd secondary effect of confusing the heck out of nonces (noces are a security feature), probably because they too rely on timestamps.

This plugin strips out the sql_modes listed below from @@SESSION, thereby eliminating the problem while only effecting database sessions WordPress starts and not the whole database.

Explanation and history of the problem

The cause of this problem stems from an old (soon to be deprecated) odd behavior/feature of MySQL which WordPress came to depend on. My understanding is that it stems from problems in MySQL with regard to using the MySQL NOW() function in some contexts – so as a workaround it was decided that if a date of all zeros was entered on a column that was set as NOT NULL, then that date would not be rejected, but instead would be converted to the current date – to mimic NOW().

Subsequently this method was marginalized as generally a bad idea – and now values exist for a setting called sql_mode, which turn this behavior off and force errors when a null value is passed to a NOT NULL field. In many cases when there is an actual DBA in charge of the database, they will assign these values to the production database to force what outside of MySQL would be normal SQL behavior.

There are 3 sql_modes which will trigger the error by disabling the ability to enter zero dates.

  • NO_ZERO_DATE
  • NO_ZERO_IN_DATE
  • TRADITIONAL
It’s worth mentioning that the ALLOW_INVALID_DATES mode does NOT solve the problem, even though by the name of it, it would seem that it might.

The solution

This plugin solves the problem with minimal effect to the database environment. All this plugin does it check to see if the sql_modes in question are set, and if they are, it removes them.

Importantly, this plugin changes @@SESSION.sql_mode and not @@GLOBAL.sql_mode. By changing sql_mode via the session, it only takes effect on each specific connections WordPress makes with the database rather than changing it for the entire database permanently – as such this will have no effect whatsoever on any applications that might happen to be using the same database.

Other manifestations, alternative solutions.

WARNING: Whenever applicable, consult your DBA before making any changes to production databases.

This problem may (I have no idea) in fact be more common during installation. The errors are different, and although I have seen them, I do not have them handy at this time. In any case, if the sql_modes are set during installation – the process will fail from the start – because of this, it is my presumption that anyone encountering the errors as described above – has probably recently migrated to a new database, or perhaps recently acquired an enthusiastic new DBA who has made some changes.

This plugin DOES NOT FIX this problem for those encountering during installation – thats impossible because WordPress ignores all plugins during installation – so there is no way to hack around it via plugin and fix it before installation occurs.

If you are encountering this problem during installation you will need to find an alternative way to remove the modes. If this is a production environment this may require the cooperation of a DBA who may not like the idea. If met with resistance, I suggest the asking for the modes to be temporarily removed for installation, and then set back after its dont – at which point this plugin will handle the problem from then on.

If your encountering this locally, or on a environment whose database you have access to modify, you will want to make as few changes as possible, so go to whatever interface you use (command line, phpMyAdmin, whatever) and run this line of SQL:

SELECT @@GLOBAL.sql_mode;

This will show you all the sql modes currently defined. Copy all them them but if you have NO_ZERO_DATE, NO_ZERO_IN_DATE, or TRADITIONAL remove them. Then update sql_mode with the remaining modes as such:

SET @@GLOBAL.sql_mode = "STRICT_TRANS_TABLES,STRICT_ALL_TABLES,ERROR_FOR_DIVISION_BY_ZERO";

Please don’t simply use the sql_mode definition I have shown above, this is only an example – be sure to use the modes from your database configuration. Remember to keep everything together NO SPACES ALLOWED in the definition.

Finally, if your just working locally and don’t care about these sql_modes, you can just clear them all.

SET @@GLOBAL.sql_mode = "";

This will do the trick but is absolutely not recommended as a fix for production sites that might share environments.

Irrelevant Note: I struggled with naming this plugin, and settled on the terminology seen in the error message.