Sharing: Filter share URL per service
We'd like to provide a patch to allow filtering share URLs per service. The purpose is to add a UTM tracking code to the shared URL.
Before we submit a PR, we wanted to get pre-clearance as there are a couple of ways to do it:
- We could add a filter to each share service (or perhaps the parent class).
- We could add a parameter to
get_share_url()and pass in the service from each instance.
The first option feels cleanest to me. Is there a third option we are not thinking of?
I just came across https://wordpress.org/support/topic/how-to-use-utm-codes-with-jetpack-sharing/ yesterday, which might be of interest to you.
@rvdsteege Thanks, the issue is that we want to do a separate tracking code per service. The filter he notes would universally apply to all services.
@jeherve If someone can comment on the best architecture, I'm willing to put together a pull request for this.
@clifgriffin that's a very interesting problem. We have discussed it a little bit, and I think there are several ways you can achieve the desired result. As you can see in the thread, there is a way to see which sharing service the filter is being called for using the third parameter. You could do a simple string match and based on that modify the URL to your needs. Will that solve your problem?
@zinigor Man, don't know how I missed that. I'll give it a try!
@zinigor You were right. I should read better next time.

However I did find a crucial problem with the example used in the link above. Here's my working code:
add_filter('sharing_permalink', 'obj_share_link_utm', 10, 3);
function obj_share_link_utm($permalink, $post_id, $sharing_id) {
$ga_params = array(
'utm_source' => $sharing_id,
'utm_medium' => 'Social',
'utm_campaign' => 'ShareBar',
);
return add_query_arg( $ga_params, $permalink );
}
The example here https://wordpress.org/support/topic/how-to-use-utm-codes-with-jetpack-sharing/ used esc_url before returning the filtered permanlink, but this leads to double encoding of some characters causing Twitter to be unable to parse the URL.
Re-opening this. The email share button bypasses get_share_url(), so it doesn't run the URL through sharing_permalink.
Instead it uses this chain:
do_action( 'sharing_email_send_post', $data );
sharing_email_send_post() runs sharing_email_send_post_content() to get the content. It passes in $data from above.
This is sharing_email_send_post_content():
function sharing_email_send_post_content( $data ) {
/* translators: included in email when post is shared via email. First item is sender's name. Second is sender's email address. */
$content = sprintf( __( '%1$s (%2$s) thinks you may be interested in the following post:', 'jetpack' ), $data['name'], $data['source'] );
$content .= "\n\n";
$content .= $data['post']->post_title."\n";
$content .= get_permalink( $data['post']->ID )."\n";
return $content;
}
I was just going to patch this to send the filtered URL in $data, but there are some backwards compatibility issues to consider. I'm going to submit a PR of how I think this could work and reference this issue.
See #6342 and #6124