Today I’m going to tweak the marvellous P2 Theme by Automattic.
P2 is one of my all time favourite themes – not only because it transforms WordPress into something completely different, but also because it’s an innovative way we communicate internally here at WP Hosting. If you don’t know P2 yet, do check it out at p2theme.com and watch the video.
At the time of writing P2 is at Version 1.3.3 and has been since November last year. Code changes quickly, so by the time you’re reading this my ramblings may be out of date.
Let’s have a look at how we can add and amend the default categories (i.e. Status Update, Blog Post, Quote and Link).
Changing the Labels
We’ll start by looking at how to change the labels at the top here:

P2 default categories - these are hard coded in Version 1.3.3
Take a look at a file called post-form.php in the root of the P2 directory. In it you’ll find this cryptic block of code:
<div id="postbox"> <ul id="post-types"> <li><a id="status"<?php if ( $post_type == 'status' ) : ?><?php endif; ?> href="<?php echo site_url( '?p=status' ); ?>" title="<?php esc_attr_e( 'Status Update', 'p2' ); ?>"><?php _e( 'Status Update', 'p2' ); ?></a></li> <li><a id="post"<?php if ( $post_type == 'post' ) : ?><?php endif; ?> href="<?php echo site_url( '?p=post' ); ?>" title="<?php esc_attr_e( 'Blog Post', 'p2' ); ?>"><?php _e( 'Blog Post', 'p2' ); ?></a></li> <li><a id="link"<?php if ( $post_type == 'link' ) : ?><?php endif; ?> href="<?php echo site_url( '?p=link' ); ?>" title="<?php esc_attr_e( 'Link', 'p2' ); ?>"><?php _e( 'Link', 'p2' ); ?></a></li> </ul>
This is an unordered list which displays the tabs, let’s call it the Post Menu for reference.
Notice how each item gets referred to a number of times here. The last two items in each line (for example ‘Status Update’) are the ID and displayed text. If all you want to do is change those labels, then go ahead and replace them with anything you like:
<li><a id="status"<?php if ( $post_type == 'status' ) : ?><?php endif; ?> href="<?php echo site_url( '?p=status' ); ?>" title="<?php esc_attr_e( 'My Label', 'p2' ); ?>"><?php _e( 'My Label', 'p2' ); ?></a></li>
It’ll result in something like this:

Status Update now reads My Label - but it stills posts into 'status'
If you post something with your new option highlighted, it will still be posted in the “status” category. The same goes for all four options – you can change the label with this option, but not the actual post category.
Adding new Categories
You’ll probably want to add your own category buttons, or perhaps change the existing categories to something else.
If you just go ahead and tweak the existing values, you’ll notice that P2 ignores your changes and posts everything in “status”. Let me assure you: you’re not mad, it’s just how P2 rolls:
<li><a id="mycategory"<?php if ( $post_type == 'mycategory' ) : ?><?php endif; ?> href="<?php echo site_url( '?p=mycategory' ); ?>" title="<?php esc_attr_e( 'My Label', 'p2' ); ?>"><?php _e( 'My Label', 'p2' ); ?></a></li>
The reason for this is that there’s an array defined somewhere and the theme checks if you’re posting in any of those “valid” categories. Thanks to Nobble for mentioning this over a year ago on the WordPress Forum.
Let’s take a look at ajax.php (it’s in a sub-folder called inc). Halfway down the file we’ll see the array being defined:
$accepted_post_cats = apply_filters( 'p2_accepted_post_cats', array( 'post', 'quote', 'status', 'link' ) ); $post_cat = ( in_array( $_POST['post_cat'], $accepted_post_cats ) ) ? $_POST['post_cat'] : 'status';
The first line defines the array of valid categories, and the second line makes sure that the category gets set to “status” if yours does not exist.
I always think it’s best to leave things as they are and just add to stuff for compatibility, so I’m adding ‘mycategory’ to the array like so:
$accepted_post_cats = apply_filters( 'p2_accepted_post_cats', array( 'post', 'quote', 'status', 'link', 'mycategory' ) );
Wonderful! Notice that this will now create a new category or post in the existing one. Exactly what we want!
But how do we add our own items to the Post Menu? Let’s find out next.




{ 6 comments… read them below or add one }
UPDATE: Tweaking categories and labels this way no longer works since Version 1.4.0. See here for all the latest features: http://wpguru.co.uk/2012/04/new-features-in-p2-140/
Sadly this means that we have to go back to the drawing board here…
But the good news is I’ve found a way to back-port categories into v1.4.0:
http://wpguru.co.uk/2012/04/how-to-bring-back-post-categories-in-p2/
Hey Jay, thanks a lot for this. I’ve been racking my head for the last 24 hours trying to edit a child theme with P2. With this and the newer post for back-porting cats I’m almost set.
I do have a final question though. For people who change the theme’s color scheme. How would I go about changing the background color of notifications? My bg is no longer white, but every time I post a new comment or status I get the slow animation that fades to white. Please help!
Hi Luis,
that’s a really tricky one – I know what you mean though, a new post always fades from yellow to white. I’ve been through all the CSS and the ajax.php but can’t find where this is being done, sorry I can’t help on this occasion. If you find out please let me know, I’ll add it to http://p2guide.wordpress.com
It’s worth asking that question in the P2 Forum: http://wordpress.org/tags/p2
very useful post. P2 should have had this in built. i spent hours before i found both of your posts.
Thank you Nav
{ 2 trackbacks }