Skip to content

Instantly share code, notes, and snippets.

@yurukov
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yurukov/c35d7a7d1c3f646eef67 to your computer and use it in GitHub Desktop.
Save yurukov/c35d7a7d1c3f646eef67 to your computer and use it in GitHub Desktop.
Permalink update support and optimization of jetpack share module for WP
I updated my permalink structure from .../Y/m/d/title to .../Y/title.
Naturaly the share button count was set to 0 due to the new URL. My
solution was to use the old URL structure for the share buttons only
for posts before the update. The downside of this is that old posts
will be shared with the old url structure. Thus when shared, there'll
be another 301. Upside - share count is kept.
Also, I wanted to optimize the scripts written to the footer by the
share module of jetpack. The registered js file includes all the
scripts for opening the share dialogs in a new window. These are the
ones I have enabled. They are the same for all posts and I use minify,
so this script is posted with the others in one file.
The following snipet is part of functions.php of my theme.
var windowOpen;
jQuery(document).on( 'ready post-load', function(){
jQuery( 'a.share-facebook' ).on( 'click', function() {
if ( 'undefined' !== typeof windowOpen ){
windowOpen.close();
}
windowOpen = window.open( jQuery(this).attr( 'href' ), 'wpcomfacebook', 'menubar=1,resizable=1,width=600,height=400' );
return false;
});
jQuery( 'a.share-twitter' ).on( 'click', function() {
if ( 'undefined' !== typeof windowOpen ){
windowOpen.close();
}
windowOpen = window.open( jQuery(this).attr( 'href' ), 'wpcomtwitter', 'menubar=1,resizable=1,width=600,height=350' );
return false;
});
jQuery( 'a.share-linkedin' ).on( 'click', function() {
if ( 'undefined' !== typeof windowOpen ){
windowOpen.close();
}
windowOpen = window.open( jQuery(this).attr( 'href' ), 'wpcomlinkedin', 'menubar=1,resizable=1,width=580,height=450' );
return false;
});
jQuery( 'a.share-google-plus-1' ).on( 'click', function() {
if ( 'undefined' !== typeof windowOpen ){
windowOpen.close();
}
windowOpen = window.open( jQuery(this).attr( 'href' ), 'wpcomgoogle-plus-1', 'menubar=1,resizable=1,width=480,height=550' );
return false;
});
jQuery( 'a.share-pocket' ).on( 'click', function() {
if ( 'undefined' !== typeof windowOpen ){
windowOpen.close();
}
windowOpen = window.open( jQuery(this).attr( 'href' ), 'wpcompocket', 'menubar=1,resizable=1,width=450,height=450' );
return false;
});
});
<?php
....
function customize_legacy_permalinks($permalink, $post_id) {
if (!$post_id || is_nan(intval($post_id)) || intval($post_id)>16791)
return $permalink;
else {
$date = get_the_date("/Y/m/d/",$post_id);
$year = substr($date,0,6);
return str_replace($year,$date,$permalink);
}
}
function customize_sharing_display_link($url) {
$post_id = get_the_ID();
return customize_legacy_permalinks($url, $post_id);
}
function disable_jetpack_open_sharing_in_new_window() {
return false;
}
function enqueue_jetpack_share_custom_newwindow() {
wp_register_script('jetpack-share-custom-newwindow', ("[PATH TO THEME]/jetpack_sharenewwindow.js"), array('jquery','sharing-js'), null, true);
wp_enqueue_script('jetpack-share-custom-newwindow');
}
add_filter( 'sharing_permalink', 'customize_legacy_permalinks', 10, 2);
add_filter( 'jetpack_sharing_display_link', 'customize_sharing_display_link', 10, 1);
add_filter( 'jetpack_open_sharing_in_new_window', 'disable_jetpack_open_sharing_in_new_window', 10, 0);
add_action( 'wp_enqueue_scripts', 'enqueue_jetpack_share_custom_newwindow');
...
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment