Category Archives: Custom Post Types

Notes that relate to the use of custom post types

Custom post types, taxonomies , capabilities, roles

Are you you getting the browser messages when saving a new custom post type draft?

  • Firefox: “This page is asking you to confirm that you want to leave – data you have entered may not be saved.”
  • Internet Explorer: “Are you sure you want to leave this page”
  • Chrome: “The changes you have made will be lost if you navigate away from this page”
  • “Leave page or Stay on page” when you click save draft on a new custom post type?
What happens when you do not have the right capability
What happens when you do not have the right capability
Leave or Stay message in chrome
Leave or Stay message in chrome

What happens?

You opt to leave and the post was still saved but the custom fields etc do not save.  BUT then you CAN edit the post and then add in the custom fields / meta box without any further problem (what the?)

And of course admin’s have no problem!

Why?

This appears to be because we have said that the custom post type supports the default taxonomies and possibly wordpress is trying to do something with javascript (maybe assign a default category to the custom post type) but the user is not allowed to.  However it also happens when the custom post type has tags but not categories?  But it does not happen if it just has the custom taxonomy (with the assign_terms or assign_terms=>edit_events capability).    The behaviour only occurs with default taxonomies.

Key finding:

If you are allowing your custom post type to be assigned categories and tag taxonomies like normal posts, then ANY role that is required to be able to create the custom post type while default taxonomies are supported, must also have ‘edit_posts’ capability. (Unless you have code to change the default taxonomies, but then you will be messing with the standard posts capabilities)

Arguably the wordpress default capability ‘assign_terms’ should not be mapped by default to ‘edit_posts’ as this is what is making life complicated.  Possibly a contributor should have ‘assign_terms’ capability directly?   Or one might say that it’s the sharing of taxonomies that is causing the complication.

Background

I wanted a role that could create events but NOT also create posts.  Similarly with editors.
Custom roles can be setup to manage custom post types (in this case events) and one can isolate these capabilities.  However when one adds taxonomies (default and custom) into the mix, things get more interesting.

Default taxonomy capabilities for categories and tags

$default_caps = array(
 'manage_terms' => 'manage_categories',
 'edit_terms'   => 'manage_categories',
 'delete_terms' => 'manage_categories',
 'assign_terms' => 'edit_posts',
 );

While one could overwrite these capabilities with say ‘assign_terms’=>’assign_terms’, then one would not be able to assign categories and tags to standard post types without also ensuring that the default roles were assigned the capability ‘assign_terms’.  All achievable, it just seems wrong to have to mess with standard wp and risk breaking other plugins.

Registering custom taxonomy means that in this case ‘edit_events’ capability is enough to be able to assign this custom taxonomy to this single custom post type

'capabilities' => array (
 'manage_terms'=> 'manage_categories',
 'edit_terms'=> 'manage_categories',
 'delete_terms'=> 'manage_categories',
 'assign_terms' => 'edit_events'  // strictly speaking need one for each new posttype ?
 )

 

TIP: while you are testing this all out, a role manager plugin like Justin Tadlock’s members plugin is very helpful.  Roles and their capabilities are saved in the database and unless you ‘remove’ the role and add it again, the capabilties will not update as you might want them to.

I was actually trying to create a custom role that can assign categories, tags and custom taxonomies to your custom post type. If you give these roles ‘manage_categories’, they’ll be able to add new categories BUT still not assign a post to existing categories.  They have to have ‘edit_posts’ or obe has to override the default wp setup.

 

V1.9 (wp 3.1 and wpml compatible!) – woohoo BBQ

Jumping for Joy

After a bit of OMG! and digging into wp 3.1 code, I figured out why pages (like your calendar page yes?) with category and taxonomy parameters would fail with a 404….. yes exactly WTF!…..  No parameters, no 404…. hmmm?

Only hit the taxonomy widget, but ….

This was NO good for the taxonomy widget… I think not many of you are using it, but I am a perfectionist so it had to be fixed.   You want more gory details see the wordpress trac ticket on pages, custom taxonomy queries and 404’s, which has the workaround – do not panic is is achievable with one of those great wordpress actions, so no core code changes!

Please read the changelog

There are quite a few code changes (change is always risky).  It has been tested on a variety of platforms (3.0, 3.1 and wp 3.1 with wmpl). There are two public  test systems which you are welcome to login to:

If you encounter any strange behaviour and have done normal debugging to isolate whether it is the plugin, or your theme etc..,please log it asap at forum.anmari.com.

I will continue to work on the plugin as there are a number of feature requests which did not make it in as I needed to get a stable version up asap.  Please add  your thoughts at the feature section of the forum.

1.9 Changelog

  • Add: Added ability to define the custom post name yourself.  This is still somewhat experimental, so please test thoroughly and report any problems asap at forum.anmari.com.  Some internal aspects will retain the ‘event’ name (eg: capability), however in general, the user visible aspects should follow your new name.  Existing users should change only with caution.  Converting existing events will require a mysql update through phpmyadmin.
  • Add: Added check for a lang query and if exists, passes it through to links generated – needed for WPML to know which language to serve up.
  • Add: add a filter to get around wp 3.1 possible bug where it tries to apply taxonomy parameters to a page query (which results in a 404).  Filter added will get around this for pages only.
  • Add: added a check for a plugin domain locale language file in wp-content/languages. This means you can have a very tailored to you translation with whatever colloquial or slang vocabulary you wish to have and it will NOT get overwritten by standard languages files.  Is that cool or ‘sick’ or what!
  • Add: added additional check when creating taxonomies. It will check if the term is already in use somewhere, to prevent very confusing permalink behaviour if the same term is used in multiple places.
  • Add: add additional calls to translation functions and comment on event info screen for WPML users.  If you want the text to be multi-lingually accessible, do not edit it in settings. Rather translate it.  Plain html  with no text is okay to edit.  Alternatively you will have to create the source strings yourself.
  • Change: Separated out the listing code (basically the free version) to simplify translation etc.  Nesting it within the amr-events version was confusing translators and other plugins. So now it will look like 2 plugins but will function as before.
  • Change: If you were still using default css file, the plugin should have updated the saved css location for you to new default file.
  • Change: Changed the text domain to plugin name to avoid confusion with translator plugins, renamed lang files accordingly (still have .mo problem)
  • Fix: Fixed bug in js which prevented one from editing the before/after fields in the event info template, if you had js enabled. (Disable js was the work around).

Listing files (3.9 Changelog)

  • Change: Table body and row html slightly re-organised to more sensible structure
  • Fix: Cleaned up some hcalendar markup to make it totally valid microformat markup
  • Fix: Fixed spacing on new ++ — pagination options
  • Fix: Fixed very specific bug when using numeric bydays (eg: last sunday ) and wanting the small calendar to link to the calendar page, with the 1 day listing.  The logic was cutting off the dates generated before the “contract” to the last x.
  • Fix: Fixed the ignore_query logic so it ignores what it should and responds to what it should (eg: for calendars, it must still respond to ‘start’ at least.

Download

Version 1.7, event post thumbnail images and some RDATE or EXDATE bug fixes

Amr-events Version 1.7 is now available here: http://webdesign.anmari.com/2078/amr-events-download/

Event excerpt with post thumbnail
Event excerpt with post thumbnail in hover area instead of post content
  • Fixed situation where if there where no parameters entered in widget, then no events were listed in the widget.  The widget will now pickup default parameters from the listtype like it used to.
  • Added possibility for CURL to follow redirections in case an exernal ics host has moved your ics file while you were not looking!
  • Timed certain day repeating dates (RDATES) will now show the correct time
  • Timed events with excluded dates (EXDATES) will now have events on the excluded days excluded.  Note: excluded days overwrite RDATES above if the dates and times are the same.  This is because it is possible according to the spec to have RRULES and RDATES in same event and one may wish to have an excluded date for the RRULE.  So it is possible to have the 3 co-existing, and thus a priority must be assigned.
  • If your theme supports post thumbnails or feature images, you may now include these separately in your list type.  For example, if the image is not already in your post content or added to your excerpt, then you may wish to add the post thumbnail to your event display.  Using the list type settings, you can add the post thumbnail separately to a agenda, calendar or widget listing.  Note that this stage the size will be the thumbnail size, as used by your theme.  If there is a demand to be able to create one’s own event thumbnail image size, please add your thoughts here http://forum.anmari.com/forum.php?id=7. Note that you can change your website thumbnail size via wordpress settings and plugins do exist to allow you to regenerate your old thumbnails.
Using excerpt and post thumbnail instead of post content
Using excerpt and post thumbnail instead of post content

How to use custom post types as events

The amr-events plugin events can be

Which to use? some points to note:

  • Custom post types are a way of keeping a type of post “separate” from other posts.
  • Many Some plugins may work with standard posts, but not with custom posts.  (this has improved ) Eg: social sharing buttons, map/location plugins ,  filter plugins etc.  So investigate whether you expect them to work with your events or not, before deciding which to use.
  • Even if other plugins work with custom post types, you probably need to go back to their configuration once you have added a custom post type and add the new post type in to their configuration.
  • Alternatively if you want another plugin to work with your events, but NOT with your posts – eg: a geo tagging plugin, then maybe use custom posts instead.
  • Once you have added a new custom type type (or indeed a new taxonomy) you may need to flush your permalink rewrite rules.  It is not appropriate for the plugin to do this automatically as it can be computationally expensive – it is better if you decide when to do it.  It only needs to be done after configuration changes that affect the rules.  Goto your permalinks and resave your settings.

This plugin does many things, but specifically for custom post types:

  • it adds custom post type events to your rss feed and home page if you want – just tick the check box in the settings (from version 1.1 onwards).
  • it can add taxonomies if you wish – all the code you need is there, including special taxonomy widgets.

Settings > post types

Settings specific to custom post types
taxonomies
Taxonomies Examples

Further Information

Caution:

In researching custom post types and in particular how to add them to blog post home pages and rss feeds, I found complaints of lost custom menus, thumbnails when adding custom post types.  You can see some in the comments on Justin ‘s post on showing custom post types on the  home page. I believe that the reason for this is:

Most of the suggested code examples show setting (thus over-writing) the post types in the query, rather than checking if there any post types there already (remember  ‘post’ as a default of course) and adding to the list.

If one is using multiple plugins and themes that filter the wp-query to add post types (could be pages, attachments for thumbnail, custom menus etc)  eg with:

add_filter( 'pre_get_posts', 'a_function')

…in that case, plugins could overwrite each other’s filter criteria – it would depend on which filter got executed last.

This plugin checks first.  Also you can switch it’s version of the filter off completely.

For further information on how to use the plugin generally, see

Feedburner plugins and ical feeds

If you are using feedburner and a ical plugin that generates feeds, then please use the http://flagrantdisregard.com/feedburner/, rather than the feedsmith plugin.

The FD plugin allows for the possibility of some other feed (like an ical feed naturally!) and so will not hijack your ics feed like the feedsmith one does.

Alternatively

  • a simple hack to the google feedsmith plugin to check the global $feed variable and bail if it = ‘ics’ would also do the trick!
  • or if you maintain good coding standards and don’t like editing external plugins (not a good idea generally!), then you could muck around with the wordpress action template_redirect and see if you can remove the feedburner redirect IF it is a ics feed.

Event editing plugin with full ics/ical feed

The amr-events plugin is in an almost alpha state, suitable for some testing (Still some debug statements lurking around!) .  Get a preview by testing it yourself at http://icalevents.com/test/.

It aims to provide full implementation of the ical RFC 5445 specification, while reatining the ability to be scaled down for more basic use.  It has either simple built in location to map link functionality, or will integrate with a number of “geo” plugins.

The ics feed has been tested with a number of calendar programs including google.

For more details:

Event locations, geo tags for custom post types

I am finalising my new plugin that will allow creation of Ical events within wordpress.  The Ical spec provides for a location and geotags for an event.  The existing event list plugin  shows a google map link for the event if these components exist.

So the new plugin will allow entry of a location and/or integrate with other location plugins. There are some sweet geo or location plugins available, so I thought I would keep my location meta box input simple and allow integration with some of these other plugins if anyone required more sophistication.    I have tested the new plugins see if they play nicely with custom post types (although my plugin will also work with normal posts too), and use the wordpress databse appropriately.  Custom Post Type support is actually quite easy to add, so please check the latest versions.

So far I have looked at:

WP Geo

wp-geo metabox
  • Ben Huson is currently working on optional custom post type support.
  • His plugin uses straight forward post-meta which makes it easy to integrate
  • Optional control over where location data shows if at all in post view.

Geomashup

geomashup metabox
  • This plugin uses custom tables, so will only be able to access their data if their plugin is active (uses their DB class to extract the data)
  • It appears to automatically support custom post types(ie the metabox appears on all post types). Ideally this should be optional as per Ben Huson’s version above (dev version for now).
  • Has a fairly unique feature of allowing one to save and reuse locations.  Does some cacheing.

Geolocation

Gelocation plugin metabox
  • Also stores it geo-data nicely in the user meta table, so will integrate with this too.
  • Map shows on hover on the address in the post.  There is some option as to where/how this location link appears.
  • Does not support custom post types yet.
  • works with the wordpress iphone app.
  • v0.1.1 breaks some of the admin JavaScript, not disastrously – affects the arrow down of menu show/hide. Common problem when a plugin includes latest UI.  Very minor – one can still click them the menu title to open it up.

gpress removed.

Others?

If anyone would like to suggest other post location plugins for consideration in offering integration, please comment below.

Since there are a number of these similar plugins, the location plugin that will come with the ical events plugin will be much simpler.  It will probably not have the map displayed every time you edit the post, or display the post. It will have a link to verify that the address is adequate to bring up the correct map view and/or get the latitude and longitude.

Draft version of the “simple” post location plugin metabox