Last active
August 29, 2015 14:12
-
-
Save mohsinrasool/fae3281be30e5265bcff to your computer and use it in GitHub Desktop.
WPML Slug Fixed
This file contains hidden or 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
/** | |
* This script adds a bulk action on post listing screen. When applied, it updates the slugs of selected translations with | |
* the slugs of corresponding english translations. Whole idea is to have same slugs for every translation with only difference | |
* of /language. For example | |
* /test | |
* /fr/test | |
* /de/test | |
* | |
* @author mohsinrasool | |
*/ | |
/** | |
* Bulk action on post listing screen to Synchronize translation slugs with english slugs | |
* | |
* @author mohsinrasool | |
*/ | |
add_action('admin_footer-edit.php', 'pp_bulk_admin_footer'); | |
function pp_bulk_admin_footer() { | |
global $post_type; | |
if( in_array($post_type, array('post','page')) ) { | |
?> | |
<script type="text/javascript"> | |
jQuery(document).ready(function() { | |
jQuery('<option>').val('fix_slugs').text('<?php _e('Fix Slugs')?>').appendTo("select[name='action']"); | |
jQuery('<option>').val('fix_slugs').text('<?php _e('Fix Slugs')?>').appendTo("select[name='action2']"); | |
}); | |
</script> | |
<?php | |
} | |
} | |
/** | |
* Performs the action and updates the slugs. | |
* | |
* @author mohsinrasool | |
*/ | |
add_action('load-edit.php', 'pp_fix_slugs_bulk_action'); | |
function pp_fix_slugs_bulk_action() { | |
// 1. get the action | |
$wp_list_table = _get_list_table('WP_Posts_List_Table'); | |
$action = $wp_list_table->current_action(); | |
switch($action) { | |
// 3. Perform the action | |
case 'fix_slugs': | |
global $wpdb; | |
// 2. security check | |
check_admin_referer('bulk-posts'); | |
$post_ids = $_GET['post']; | |
// if we set up user permissions/capabilities, the code might look like: | |
//if ( !current_user_can($post_type_object->cap->export_post, $post_id) ) | |
// pp_die( __('You are not allowed to export this post.') ); | |
$fixed_slugs = 0; | |
foreach( $post_ids as $post_id ) { | |
$lang_info = wpml_get_language_information($post_id); | |
if($lang_info['native_name'] == 'English') | |
continue; | |
$slug = getWPMLEnPostSlug($post_id); | |
$wpdb->query( "UPDATE `" . $wpdb->posts . "` SET `post_name` = '" . $slug . "' WHERE `ID` = '" . $post_id . "' LIMIT 1" ); | |
$fixed_slugs++; | |
} | |
// build the redirect url | |
$sendback = add_query_arg( array('fixed_slugs' => $fixed_slugs, 'ids' => join(',', $post_ids) ), $sendback ); | |
if(isset($_GET['post_type'])) | |
$sendback = add_query_arg( array('post_type' => $_GET['post_type'] ), $sendback ); | |
if(isset($_GET['lang'])) | |
$sendback = add_query_arg( array('lang' => $_GET['lang'] ), $sendback ); | |
// 4. Redirect client | |
wp_redirect($sendback); | |
break; | |
default: return; | |
} | |
exit(); | |
} | |
/** | |
* Adds the notice on the listing screen | |
* | |
* @author mohsinrasool | |
*/ | |
add_action('admin_notices', 'pp_fix_slugs_bulk_admin_notices'); | |
function pp_fix_slugs_bulk_admin_notices() { | |
global $post_type, $pagenow; | |
if($pagenow == 'edit.php' && isset($_REQUEST['fixed_slugs']) && (int) $_REQUEST['fixed_slugs']) { | |
$message = sprintf( _n( 'Slugs updated.', '%s slugs updated.', $_REQUEST['fixed_slugs'] ), number_format_i18n( $_REQUEST['fixed_slugs'] ) ); | |
echo "<div class='updated'><p>{$message}</p></div>"; | |
} | |
} | |
/** | |
* Returns the slug of english language | |
* | |
* @author mohsinrasool | |
*/ | |
function getWPMLEnPostSlug($post_id=NULL) { | |
global $sitepress; | |
if(is_object($sitepress)) { | |
if($post_id != NULL) | |
$current_post = get_post($post_id); | |
else { | |
global $post; | |
$current_post = $post; | |
} | |
$trid = $sitepress->get_element_trid($current_post->ID, 'post_' . $current_post->post_type); | |
$translations = $sitepress->get_element_translations($trid); | |
if(isset($translations['en'])) { | |
$base_post = get_post($translations['en']->element_id); | |
return $base_post->post_name; | |
} | |
} | |
return false; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment