Last active
August 29, 2015 14:27
-
-
Save yurukov/c35d7a7d1c3f646eef67 to your computer and use it in GitHub Desktop.
Permalink update support and optimization of jetpack share module for WP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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