Posts 2 Posts: Version 1.3

Improved API

In this release, I focused on making the API more uniform; everything works for both posts-to-posts and posts-to-users connections now. This is possible because you can pass whole post and user objects, instead of just IDs.

Previously:

$connected = get_users( array(
	'connected_type' => 'posts_to_users',
	'connected_items' => $post->ID
) );

Now:

$connected = get_users( array(
	'connected_type' => 'posts_to_users',
	'connected_items' => $post
) );

This small change gives P2P more information to work with, which means fewer database queries are needed.

Shortcodes!

The plugin now comes with a pair of shortcodes for conveniently listing connected items anywhere.

Say you defined a connection type called ‘posts_to_pages’. Here’s how the shortcode would look like:

[p2p_connected type=posts_to_pages]

If you insert that into a post, it will display a list of connected pages. If you insert it into a page, it will display a list of connected posts.

And there’s one for related posts too:

[p2p_related type=posts_to_pages]

Shortcodes and widgets also have filters for modifying the HTML output.

Default field values

You can now set default values for the connection fields in the metabox, likes so:

p2p_register_connection_type( array(
	'name' => 'posts_to_pages',
	'from' => 'post',
	'to' => 'page',
 
	'fields' => array(
		'color' => array(
			'title' => 'Color',
			'type' => 'checkbox',
			'values' => array( 'green', 'yellow', 'blue', 'white' ),
			'default' => 'blue'
		),
	)
) );

In total, there were 16 resolved issues in this release.

How To Become A WordPress Guru

tl;dr: Read the effin’ source!

If a function or a parameter isn’t documented on the Codex, that doesn’t mean it doesn’t exist. Look for it in the source code. Doubly so for actions and filters.

When a template tag doesn’t behave properly, don’t just jump over to WordPress StackExchange. Dig up it’s definition and try to figure it out for yourself first.

If your job title includes the word “WordPress” + any of “developer”, “freelancer”, “ninja”, “rockstar”, “mutant turtle”, jumping into Core code should feel as natural as breathing.

Set up your development environment so that it’s only one click or a few keystrokes away.

Posts 2 Posts: Version 1.2

The main attraction in this release is the new admin screen that gives an overview of all the connection types found in the system:

Connection Types screen

Another thing to note is that queries like this no longer work:

$query = new WP_Query( array(
  'connected_items' => get_queried_object_id()
) );

You have to set the ‘connected_type’ parameter:

$query = new WP_Query( array(
  'connected_type' => 'posts_to_pages',
  'connected_items' => get_queried_object_id()
) );

Also, you can pass an array of connection types and P2P will pick the ones that make sense.

$query = new WP_Query( array(
  'connected_type' => array( 'posts_to_pages', 'actors_to_movies' ),
  'connected_items' => get_queried_object_id()
) );

As with every release, it contains a bunch of smaller enhancements and bug fixes. See the changelog for more details.

Plugin Dependencies: Version 1.2

In previous versions of the plugin, to define a depedency to BuddyPress, you’d have to write:

Depends: buddypress/bp-loader.php

As a user pointed out, using the plugin path like that is not a good idea.

It turns out that there’s a much more stable piece of information that can be used to identify plugins with: their name. D’oh!

So now you can just write this instead:

Depends: BuddyPress

Besides being a lot more user-friendly, since plugin names rarely change, it’s more reliable as well. Win!

Of course, for super-formal specs, the Provides: header still works.

Contribuie la traducerea WordPress-ului

Se pare că nu există niciun ghid despre cum poți contribui la traducerea în limba română a platformei WordPress așa că m-am decis să scriu unul.

Mai demult, existau mai multe traduceri neoficiale, găzduite pe diferite site-uri. La ora actuală, există un loc centralizat pentru traducerile în toate limbile, și anume translate.wordpress.org.

Traducerea în limba română poate fi găsită aici: http://translate.wordpress.org/projects/wp/dev/ro/default

Oricine poate adăuga sau modifica traducerile din orice limbă. Nu ai nevoie decât de un cont gratuit pe wordpress.org, care se poate crea de aici: http://wordpress.org/support/register.php

Înainte de a fi incluse în traducerea oficială, string-urile traduse trebuie aprobate de un validator. Asta se întâmplă de obicei cam înainte de lansarea unei noi versiuni majore a software-ului, cum ar fi WordPress 3.3.

Mai multe informații despre WordPress în română se află aici: http://ro.wordpress.org/

Conditional Script Loading Revisited

In 2010 I wrote a tutorial for loading JavaScript using the WordPress API. The main problem was how to enqueue a script only when needed; for example, when a certain shortcode was present on the page.

Since WordPress 3.3, you can skip all the Yoda acrobatics; just call wp_enqueue_script() inside the shortcode handler:

add_shortcode('myshortcode', 'my_shortcode_handler');
 
function my_shortcode_handler($atts) {
	wp_enqueue_script('my-script', plugins_url('my-script.js', __FILE__), array('jquery'), '1.0', true);
 
	// actual shortcode handling here
}

If you’re interested in the gory details, here’s the trac ticket.

Front-end Editor: Version 2.2

Editable groups

It’s now possible to create arbitrary groups of editable elements. This opens the door for a lot of possibilities. For example, you can now edit all fields of a post at once, instead of one at a time. Go to the FEE settings page to enable it.

Post creation

One feature that was often requested was the ability to create posts from the front-end, not just to edit them. This is now possible by using the fee_inject_dummy_post() template tag.

Aloha 0.20

This release features the most recent version of Aloha Editor available, 0.20.0-RC9. This fixes the jQuery compatibility issues, as well as a bunch of other bugs. Thanks again to Jotschi for lending a hand with this.

Thanks to AppThemes and Jason Buksh for supporting this release financially.

Happy holidays!

The Magic of WP_User

In previous versions of WordPress, when you called get_userdata() or get_user_by(), you got a plain stdClass object, filled with all the fields from both wp_users and wp_usermeta tables. This had two disadvantages:

  • dashes were removed, so that ‘my-custom-field’ became ‘mycustomfield’
  • all fields were loaded and kept in memory, wasting precious resources

Since WordPress 3.3, all user-related functions return WP_User instances. This class has some magic methods that make it behave as if it contained all the custom fields.

Accessing a single custom field

For example, let’s try to display the user description of the currently logged in user:

$current_user = wp_get_current_user();
 
if ( isset( $current_user->bio ) )
	echo '<p>' . $current_user->bio . '</p>';

This code works fine both in WP 3.2 and in WP 3.3. The difference is that in WP 3.3 that code is equivalent to this:

if ( $current_user->__isset( 'bio' ) ) )
	echo '<p>' . $current_user->__get( 'bio' ) . '</p>';

Internally, the __isset() and __get() methods call get_user_meta().

If you have a variable key or a key with dashes, there are prettier aliases which you can use:

if ( $current_user->has_prop( 'my-field' ) ) )
	echo '<p>' . $current_user->get( 'my-field' ) . '</p>';

Accessing all custom fields

Unfortunately, iterating over all the custom fields by casting to an array isn’t possible anymore:

foreach ( (array) $user as $key => $value ) {
	var_dump( $key, $value );
}

It will have to be replaced with this:

foreach ( get_user_meta( $user->ID ) as $key => $values ) {
	var_dump( $key, $values );
}

Note that, in the later case, $values is an array.

Posts 2 Posts: Version 1.1

I’m pleased to announce that the Posts 2 Posts plugin now also supports posts-to-users connections. (It supports users-to-users connections as well, but there’s no UI for them).

This is made possible by a new p2p_type column on the wp_p2p table, with which we can clearly distinguish between user ids and post ids. If you’re upgrading from a previous version of Posts 2 Posts, all the existing connections will be automatically updated to have the correct value for this column.

For the next version, I’m thinking of supporting connections between posts on different blogs in a multisite network.

Posts 2 Posts: Version 1.0

It’s been a year and a half since the initial 0.1 release. Posts 2 Posts has come a long way since then and it’s been a very interesting journey.

Here’s what’s new in version 1.0:

Indeterminate connection types

Initially, connection types were organised exclusively around post types. Since version 0.9.5, you can use arbitrary query variables to define the sides of a connection type.

And with this release, there’s a clear distinction between regular connection types and indeterminate connection types, which are a lot easier to control now.

Improved support for ordered connections

You can now order connections both ways. Also, you can get the previous and next post in the list. See Connection ordering.

Related posts

If you have actors and movies, it’s a lot easier now to get other actors that have played in the same movies as a particular actor. See the wiki page about related posts.

Additionally, you can show related posts using the widget.

More connection field types

In previous versions, a connection field could be either a text input or a dropdown. Now, you can also add radio buttons, checkboxes and textareas. See Connection information.

You can see all the tickets closed for this release on github:

https://github.com/scribu/wp-posts-to-posts/issues?milestone=2&state=closed