WordPress : post_name or slug

 

Since release 2.5, editing post_name or slug of a post or a page is not possible if “permalink” is not used. It’s not good for webmastering (i.e. when preparing or cloning a website between a server with or without apache rewriting). So we decide to write a plugin to temporary solve the lack and continue to control the unique name of a post or page.

Here the code of the plugin “as is”:

<?php
/*
Plugin Name: xili post-slug editor
Plugin URI: https://dev.xiligroup.com
Description: An postname (slug) editor in post or page when permalink is not activated
Version: 0.8 alpha - 080828
Author: Michel S. 
Author URI: https://dev.xiligroup.com
*/

/* alpha version - reserved to developer only */

add_action('admin_menu', 'xili_slug_add_custom_box');

function xili_slug_add_custom_box() {
if (function_exists('add_meta_box'))  {
	//This starts with WP 2.5
	// test if permalink - only used if permalink is not activated//
	$permatemplate = get_option('permalink_structure');
	if (empty($permatemplate)) :
		add_meta_box( 'xili_slug_editor', 'xili Slug editor', 'xilipostslug_inner_custom_box', 'post', 'normal' );
    	add_meta_box( 'xili_slug_editor', 'xili Slug editor', 'xilipostslug_inner_custom_box', 'page', 'normal' );

	load_plugin_textdomain('xili-slug-edit', PLUGINDIR.'/'.dirname(plugin_basename(__FILE__)), dirname(plugin_basename(__FILE__)));

	endif;
	}
}

/* Prints the inner fields for the custom post/page xili Slug editor section */
function xilipostslug_inner_custom_box() {
  	global $post_ID;
  	$post = &get_post($post_ID);
	$post_name = $post->post_name;

  // Use nonce for verification

  echo '<input type="hidden" name="xili_slug_editor_nonce" id="xili_slug_editor_nonce" value="' . 
    wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
	if (!empty($post_ID)) :
  // The actual fields for data entry

  /*inspired from wp-admin/include/post.php */
  $title = __('Edit');
	if (strlen($post_name) > 30) {
		$post_name_abridged = substr($post_name, 0, 14). '&hellip;' . substr($post_name, -14);
	} else {
		$post_name_abridged = $post_name;
	}

  $post_name_html = '<span id="editable-post-name" title="'.$title.'">'.$post_name_abridged.'</span><span id="editable-post-name-full">'.$post_name.'</span>';
	$display_link = $post_name_html;
	$return =  '<div id="edit-slug-box"><span id="sample-permalink">' . $display_link . "</span>\n";
	$return .= '<span id="edit-slug-buttons"><a href="#post_name" class="edit-slug" onclick="edit_permalink(' . $post_ID . '); return false;">' . __('Edit') . "</a></span>\n</div>";
	$return .= '<br />('.__('Post_name or slug is definitively saved - and sanitized - when post is saved.','xili-slug-edit').')';
	echo $return;

endif;
}

?>


(copy the source and create a xili-slug-editor.php file and put it in plugin folder and test after activation)

If the WordPress version is >= 2.5 and permalink is not activated, a meta box is added in admin write form under categories box.

 

Admin post snapshot

Admin post snapshot

 

 

Info : Post_name or slug is definitively saved – and sanitized – when post is saved.

If the demand is confirmed, this plugin will be published…

Ce contenu a été publié dans News, avec comme mot(s)-clé(s) . Vous pouvez le mettre en favoris avec ce permalien.

2 réponses à WordPress : post_name or slug

  1. Konisto dit :

    Hi, is there a way/plugin to automatically filter out certain unnecessary words from the slug? I mean, can I have words like « and », « is », or « or » have excluded from the slug by default?

    Thanks!

  2. xiligroup dev dit :

    With the hooking technique of WordPress, I seem it will be possible to insert during the slug creation that described filtering of unecessary words but do you think it is very necessary to do that?

Laisser un commentaire