Documentation Customizing Publicize Sharing Behavior

Customizing Publicize Sharing Behavior

Overview #

Publicize makes it easy to automatically share WordPress.com posts on Facebook, Twitter, Tumblr, LinkedIn, and Yahoo!

WordPress.com VIP clients can customize which shortlink Publicize uses, when Publicize shares to which accounts, and other behavior changes using standard filters.

↑ Top ↑

Conditionally allow services or connections to be utilized #

Publicize includes an extremely helpful wpas_submit_post? filter for determining whether a given post should be submitted to a given service or connection. It looks like this:

$submit_post = apply_filters( 'wpas_submit_post?', true, $post_id, $service_name, $connection_data ) )

$service_name will be a value like ‘twitter’ or ‘facebook’, and $connection_data will have metadata about the connection.

Here’s how you might reserve a “@vipwpnews” Twitter account exclusively for posts in the “WordPress” category:

add_filter( 'wpas_submit_post?', 'vipx_wpas_submit_post', 10, 4 );
function vipx_wpas_submit_post( $ret, $post_id, $name, $connection ) {

	$categories = get_the_terms( $post_id, 'category' );
	if ( 'twitter' == $name && '@vipwpnews' == $connection->meta['display_name'] ) {

		if ( is_array( $categories ) )
			$categories = wp_list_pluck( $categories, 'slug' );

		if ( empty( $categories ) || ! in_array( 'wordpress', $categories ) )
			$ret = false;

	}
	return $ret;
}

Note: This filter is applied both when the post is published and in the post metabox to determine which services should appear as available to the writer or editor. It will also let authors add their own services.

↑ Top ↑

Add a checkbox to send new post updates to Publicize #

When liveblogging or making frequent updates to a post, you may want to send multiple updates to Publicize.

Publicize’s default behavior is to only syndicated a post once. You can add a checkbox for writers or editors to publicize a post a second time using this code snippet:

add_action( 'publicize_form', 'vipx_filter_publicize_form' );
function vipx_filter_publicize_form( $form ) {
	global $post;

	// Only add the checkbox if the post was already published
	if ( 'publish' != $post->post_status )
		return $form;

	$checkbox = '<label for="vipx-republicize"><input type="checkbox" id="vipx-republicize" name="vipx-republicize" /> Re-Publicize Post?</label>';

	$form .= $checkbox;

	return $form;
}

add_action( 'save_post', 'vipx_action_save_post_republicize', 5 );
function vipx_action_save_post_republicize( $post_id ) {

	if ( isset( $_POST['vipx-republicize'] ) ) {
		update_post_meta( $post_id, '_publicize_pending', true );
		add_filter( 'wpas_force_publicize', '__return_true' );
	}
}

The message shared to the service will be whatever is in the Publicize textarea.

↑ Top ↑

Implement your own URL-shortening service #

By default the Publicize feature uses wp.me as a URL-shortening service. You can also use any other URL-shortening service that can return a shortened URL.

The easiest way is to filter pre_get_shortlink or get_shortlink (called by wp_get_shortlink). For an example, check out the bit.ly plugin.

The following filter is available to alter the URL sent in your update:

$url = apply_filters( 'wpas_post_url', wp_get_shortlink( $post->ID, true ), $post->ID, $this->slug );

↑ Top ↑

Altering the default messages #

It is also possible to change the default message that is created with the following filters:

apply_filters( 'wpas_default_message', $this->default_message, $this->slug )
apply_filters( 'wpas_default_prefix', $this->default_prefix, $this->slug )
apply_filters( 'wpas_default_suffix', $this->default_suffix, $this->slug )

These filters are used in the following context:

$title = sprintf( $this->default_prefix, $url ) . sprintf( $this->default_message, $post->post_title, $url ) . sprintf( $this->default_suffix, $url );

and allow the wildcards '%title%', %url% within the default_message/prefix/suffix variables.

You can also alter the message that is sent to Twitter / Facebook with a simple function attached to the following filter to Twitter:

$status_update = apply_filters( 'publicize_twitter_message', $message, $post, $url );

and to Facebook:

$status_update = apply_filters( 'publicize_facebook_message', $custom, $post, $url );