Vous connaissez sans doute le plugin WP Sitemap Page de Tony Archambeau. Ce plugin, outre qu’il est gratuit, est vraiment très pratique. Il vous permet de paramétrer et d’afficher votre plan de site grâce à des shortcodes et leurs divers attributs. Je remercie Tony car j’utilise son plugin sur pas mal de mes sites, ce qui est plutôt pas mal pour le SEO.
J’ai récemment eu besoin d’améliorer un peu le plugin sur un de mes sites car certaines catégories d’articles s’affichaient sur le plan de site et je ne le voulais pas. J’ai regardé les paramètres natifs du plugin et j’ai vu comment je pouvais exclure des pages du plan (avec leurs ID’s) mais pas des catégories.
Je me suis donc permis de retoucher un peu le plugin et voici donc le plugin WP Sitemap Page amélioré. Le code est commenté de sorte que vous puissiez voir les modifications apportées. Bien entendu, ces modifications seront supprimées à la première mise à jour du plugin, c’est pourquoi je vous conseille de bien sauvegarder le fichier principal du plugin améliorer de façon à, si vous en avez besoin, pouvoir appliquer les améliorations à toute nouvelle version.
Le code du plugin WP Sitemap Page amélioré
{title}';
add_option( 'wsp_posts_by_category', $wsp_posts_by_category );
// by default deactivate the ARCHIVE and AUTHOR
add_option( 'wsp_exclude_cpt_archive', '1' );
add_option( 'wsp_exclude_cpt_author', '1' );
}
/**
* Uninstall this plugin
*/
function wsp_uninstall() {
// Unregister an option
delete_option( 'wsp_posts_by_category' );
delete_option( 'wsp_exclude_pages' );
delete_option( 'wsp_exclude_cpt_page' );
delete_option( 'wsp_exclude_cpt_post' );
delete_option( 'wsp_exclude_cpt_archive' );
delete_option( 'wsp_exclude_cpt_author' );
delete_option( 'wsp_add_nofollow' );
delete_option( 'wsp_is_display_copyright' );
delete_option( 'wsp_is_display_post_multiple_time' );
delete_option( 'wsp_is_exclude_password_protected' );
unregister_setting('wp-sitemap-page', 'wsp_posts_by_category');
}
/***************************************************************
* UPGRADE
***************************************************************/
// Manage the upgrade to version 1.1.0
if (get_option('wsp_version_key') != WSP_VERSION_NUM) {
// Add option
// by default deactivate the ARCHIVE and AUTHOR
add_option( 'wsp_exclude_cpt_archive', '1' );
add_option( 'wsp_exclude_cpt_author', '1' );
// Update the version value
update_option('wsp_version_key', WSP_VERSION_NUM);
}
/***************************************************************
* Menu + settings page
***************************************************************/
/**
* Add menu on the Back-Office for the plugin
*/
function wsp_add_options_page() {
if (function_exists('add_options_page')) {
$page_title = esc_html__('WP Sitemap Page', 'wp-sitemap-page');
$menu_title = esc_html__('WP Sitemap Page', 'wp-sitemap-page');
$capability = 'administrator';
$menu_slug = 'wp_sitemap_page';
$function = 'wsp_settings_page'; // function that contain the page
add_options_page( $page_title, $menu_title, $capability, $menu_slug, $function );
}
}
add_action('admin_menu', 'wsp_add_options_page');
/**
* Add the settings page
*
* @return boolean
*/
function wsp_settings_page() {
$path = trailingslashit(dirname(__FILE__));
if (!file_exists( $path . 'settings.php')) {
return false;
}
require_once($path . 'settings.php');
}
/**
* Additional links on the plugin page
*
* @param array $links
* @param str $file
* @return array
*/
function wsp_plugin_row_meta($links, $file) {
if ($file == plugin_basename(__FILE__)) {
$settings_page = 'wp_sitemap_page';
$links[] = '' . esc_html__('Settings','wp-sitemap-page') . '';
$links[] = ''.esc_html__('Donate', 'wp-sitemap-page').'';
}
return $links;
}
add_filter('plugin_row_meta', 'wsp_plugin_row_meta',10,2);
/**
* Manage the option when we submit the form
*/
function wsp_save_settings() {
// Register the settings
register_setting( 'wp-sitemap-page', 'wsp_posts_by_category', 'wsp_esc_some_html_tags' );
register_setting( 'wp-sitemap-page', 'wsp_exclude_pages', 'sanitize_text_field' );
register_setting( 'wp-sitemap-page', 'wsp_exclude_cpt_page', 'sanitize_text_field' );
register_setting( 'wp-sitemap-page', 'wsp_exclude_cpt_post', 'sanitize_text_field' );
register_setting( 'wp-sitemap-page', 'wsp_exclude_cpt_archive', 'sanitize_text_field' );
register_setting( 'wp-sitemap-page', 'wsp_exclude_cpt_author', 'sanitize_text_field' );
register_setting( 'wp-sitemap-page', 'wsp_add_nofollow', 'sanitize_text_field' );
register_setting( 'wp-sitemap-page', 'wsp_is_display_copyright', 'sanitize_text_field' );
register_setting( 'wp-sitemap-page', 'wsp_is_display_post_multiple_time', 'sanitize_text_field' );
register_setting( 'wp-sitemap-page', 'wsp_is_exclude_password_protected', 'sanitize_text_field' );
// Get the CPT (Custom Post Type)
$args = array(
'public' => true,
'_builtin' => false
);
$post_types = get_post_types( $args, 'names' );
// list all the CPT
foreach ( $post_types as $post_type ) {
// extract CPT object
$cpt = get_post_type_object( $post_type );
// register settings
register_setting( 'wp-sitemap-page', 'wsp_exclude_cpt_'.esc_html($cpt->name), 'sanitize_text_field' );
}
// Get the Taxonomies
$args = array(
'public' => true,
'_builtin' => false
);
$taxonomies_names = get_taxonomies( $args );
// list all the taxonomies
foreach ( $taxonomies_names as $taxonomy_name ) {
// register settings
register_setting( 'wp-sitemap-page', 'wsp_exclude_taxonomy_'.esc_html($taxonomy_name), 'sanitize_text_field' );
}
}
add_action( 'admin_init', 'wsp_save_settings' );
/***************************************************************
* General function to escape or sanitize
***************************************************************/
/**
* It sanitize the string (never trust user input). Return a string with only "0" or "1".
*
* @param str $str content
* @return str
*/
function wsp_esc_to_keep_0_or_1($str='') {
return preg_replace('/[^01]/', '', $str);
}
/**
* It sanitize the string (never trust user input). Return a string with only "0" or "1".
*
* @param str $str content
* @return str
*/
function wsp_esc_to_keep_numeric_or_coma($str='') {
return preg_replace('/[^,0-9]/', '', $str);
}
/**
* Remove nearly all HTML tags except
+ avoid some unexpected attribute
*
* @param str $str content
* @return str
*/
function wsp_esc_some_html_tags($str='') {
// allow only specific HTML tags
$str = strip_tags($str, '
');
// escape HTML with WordPress function
$arr_tag_a = array(
'href' => array(),
'title' => array(),
'class' => array(),
'style' => array()
);
$arr_tag_usual = array(
'title' => array(),
'class' => array(),
'style' => array()
);
$arr = array(
'a' => $arr_tag_a,
'br' => $arr_tag_usual,
'strong' => $arr_tag_usual,
'b' => $arr_tag_usual,
'em' => $arr_tag_usual,
'i' => $arr_tag_usual,
'ul' => $arr_tag_usual,
'li' => $arr_tag_usual,
'h1' => $arr_tag_usual,
'h2' => $arr_tag_usual,
'h3' => $arr_tag_usual,
'h4' => $arr_tag_usual,
'h5' => $arr_tag_usual,
'h6' => $arr_tag_usual,
'p' => $arr_tag_usual,
);
return wp_kses($str, $arr);
}
/***************************************************************
* Manage the option
***************************************************************/
/**
* Fonction de callback
*
* @param array $matches
*/
function wsp_manage_option( array $matches = array() ) {
global $the_post_id;
if (isset($matches[1])) {
$key = strtolower( $matches[1] );
switch ($key) {
// Get the title of the post
case 'title':
return get_the_title($the_post_id);
break;
// Get the URL of the post
case 'permalink':
return get_permalink($the_post_id);
break;
// Get the year of the post
case 'year':
return get_the_time('Y', $the_post_id);
break;
// Get the month of the post
case 'monthnum':
return get_the_time('m', $the_post_id);
break;
// Get the day of the post
case 'day':
return get_the_time('d', $the_post_id);
break;
// Get the day of the post
case 'hour':
return get_the_time('H', $the_post_id);
break;
// Get the day of the post
case 'minute':
return get_the_time('i', $the_post_id);
break;
// Get the day of the post
case 'second':
return get_the_time('s', $the_post_id);
break;
// Get the day of the post
case 'post_id':
return $the_post_id;
break;
// Get the day of the post
case 'category':
$categorie_info = get_the_category($the_post_id);
if (!empty($categorie_info)) {
$categorie_info = current($categorie_info);
//return print_r($categorie_info,1);
return (isset($categorie_info->name) ? $categorie_info->name : '');
}
return '';
break;
// default value
default:
if (isset($matches[0])) {
return $matches[0];
}
return false;
break;
}
}
return false;
}
/***************************************************************
* Tabs
***************************************************************/
/**
* Get the current tab
*
* @return Ambigous |string
*/
function wsp_get_current_tab() {
if (isset($_GET['tab'])) {
return esc_html($_GET['tab']);
} else {
return 'main';
}
}
/**
* Display the tabs
*/
function wsp_show_tabs() {
global $wp_db_version;
// Get the current tab
$current_tab = wsp_get_current_tab();
// All tabs
$tabs = array();
$tabs['main'] = esc_html__('Settings', 'wp-sitemap-page');
$tabs['about'] = esc_html__('How to use', 'wp-sitemap-page');
// Generate the tab links
$tab_links = array();
foreach ($tabs as $tab_k => $tab_name) {
$tab_curent = ($tab_k === $current_tab ? ' nav-tab-active' : '' );
$tab_url = '?page=' . WSP_PLUGIN_NAME .'&tab='.$tab_k;
$tab_links[] = ''.$tab_name.'';
}
// Since the 25 oct. 2010 WordPress include the tabs (in CSS)
// The 25 oct. 2010 = WordPress version was "3.1-alpha"
if ( $wp_db_version >= 15477 ) {
// Tabs in CSS
?>
'.wsp_esc_some_html_tags(wsp_wp_sitemap_page_func( $atts, $content )).'
'.esc_html__('Powered by "WP Sitemap Page"', 'wp-sitemap-page').'
'; } // Exclude pages, posts and CTPs protected by password if ($wsp_is_exclude_password_protected==1) { global $wpdb; // Obtain the password protected content $sql = 'SELECT ID FROM '.$wpdb->posts.' WHERE post_status = \'publish\' AND post_password <> \'\' '; $password_pages = $wpdb->get_col($sql); // add to the other if not empty if (!empty($password_pages)) { // convert array to string $exclude_pages = implode(',', $password_pages); // Add the excluded page to the other protected page if (!empty($wsp_exclude_pages)) { $wsp_exclude_pages .= ','.$exclude_pages; } else { $wsp_exclude_pages = $exclude_pages; } } } // check if the attribute "only" is used switch ($only_cpt) { // display only PAGE case 'page': return wsp_return_content_type_page($is_title_displayed, $is_get_only_private, $display_nofollow, $wsp_exclude_pages, $sort).$copyright_link; break; // display only POST case 'post': return wsp_return_content_type_post($is_title_displayed, $display_nofollow, $display_post_only_once, $is_category_title_wording_displayed, $wsp_exclude_pages, $sort, $sort, $order, $exclude_categories).$copyright_link; break; //le tableau $exclude_categories est passé en paramètre // display only ARCHIVE case 'archive': return wsp_return_content_type_archive($is_title_displayed, $display_nofollow).$copyright_link; break; // display only AUTHOR case 'author': return wsp_return_content_type_author($is_title_displayed, $display_nofollow, $sort).$copyright_link; break; // display only CATEGORY case 'category': return wsp_return_content_type_categories($is_title_displayed, $display_nofollow, $sort, $exclude_categories).$copyright_link; //le tableau $exclude_categories en paramètre break; // display only TAGS case 'tag': return wsp_return_content_type_tag($is_title_displayed, $display_nofollow).$copyright_link; break; // empty case '': // nothing but do break; default: // check if it's the name of a CPT // extract CPT object $cpt = get_post_type_object( $only_cpt ); if ( !empty($cpt) ) { return wsp_return_content_type_cpt_items( $is_title_displayed, $display_nofollow, $cpt, $only_cpt, $wsp_exclude_pages, $sort ); } // check if it's a taxonomy $taxonomy_obj = get_taxonomy( $only_cpt ); if ( !empty($taxonomy_obj) ) { return wsp_return_content_type_taxonomy_items($is_title_displayed, $display_nofollow, $taxonomy_obj, $wsp_exclude_pages); } // end } //=============================================== // Otherwise, display traditionnal sitemap //=============================================== // List the PAGES (check if it should be exclude) if ( empty(get_option('wsp_exclude_cpt_page')) ) { $return .= wsp_return_content_type_page($is_title_displayed, $is_get_only_private, $display_nofollow, $wsp_exclude_pages, $sort); } // List the POSTS by CATEGORY (check if it should be exclude) if ( empty(get_option('wsp_exclude_cpt_post')) ) { $return .= wsp_return_content_type_post($is_title_displayed, $display_nofollow, $display_post_only_once, $is_category_title_wording_displayed, $wsp_exclude_pages, $sort, $sort, $order, $exclude_categories); } //le tableau $exclude_categories est passé en paramètre // List the CPT $return .= wsp_return_content_type_cpt_lists($is_title_displayed, $display_nofollow, $wsp_exclude_pages); // List the Taxonomies $return .= wsp_return_content_type_taxonomies_lists($is_title_displayed, $display_nofollow, $wsp_exclude_pages); // List the ARCHIVES (check if it should be exclude) if ( empty(get_option('wsp_exclude_cpt_archive')) ) { $return .= wsp_return_content_type_archive($is_title_displayed, $display_nofollow); } // List the AUTHORS (check if it should be exclude) if ( empty(get_option('wsp_exclude_cpt_author')) ) { $return .= wsp_return_content_type_author($is_title_displayed, $display_nofollow, $sort); } // return the content return $return.$copyright_link; } /** * Return list of pages * * @param bool $is_title_displayed * @param bool $is_get_only_private * @param bool $display_nofollow * @param array $wsp_exclude_pages * @param str $sort * @return str $return */ function wsp_return_content_type_page($is_title_displayed = true, $is_get_only_private = false, $display_nofollow = false, $wsp_exclude_pages = array(), $sort = null) { // init $return = ''; if ($display_nofollow==true) { add_filter('wp_list_pages', 'wsp_add_no_follow_to_links'); } // define the way the pages should be displayed $args = array(); $args['title_li'] = ''; $args['echo'] = '0'; // change the sort if ($sort!==null) { $args['sort_column'] = $sort; } // exclude some pages ? if (!empty($wsp_exclude_pages)) { $args['exclude'] = $wsp_exclude_pages; } // get only the private content if ($is_get_only_private==true) { $args['post_status'] = 'private'; } // get data $list_pages = wp_list_pages($args); // check it's not empty if (empty($list_pages)) { return ''; } // add content if ($is_title_displayed==true) { $return .= ''.esc_html__('Pages', 'wp-sitemap-page').'
'."\n"; } $return .= '- '."\n";
$return .= $list_pages;
$return .= '
'.esc_html__('Posts by category', 'wp-sitemap-page').'
'."\n"; } $return .= wsp_html_from_multi_array($cats, true, $display_post_only_once, $is_category_title_wording_displayed, $display_nofollow, $wsp_exclude_pages, $sort, $order); // return content return apply_filters( 'wsp_posts_return', $return ); } /** * Return list of posts in the categories * * @param bool $is_title_displayed * @return str $return */ function wsp_return_content_type_categories( $is_title_displayed=true, $display_nofollow=false, $sort=null, $exclude_categories = array() ) { // init $return = ''; // args avec l'exclusion des catégories $args = array( 'exclude' => $exclude_categories // Ajout de l'exclusion ici ); // change the sort order if ($sort!==null) { $args['orderby'] = $sort; } // Get the categories $cats = get_categories( $args ); // check it's not empty if (empty($cats)) { return ''; } // display a nofollow attribute ? $attr_nofollow = ($display_nofollow==true ? ' rel="nofollow"' : ''); // add content if ($is_title_displayed==true) { $return .= ''.esc_html__('Categories', 'wp-sitemap-page').'
'."\n"; } $return .= '- '."\n";
foreach ($cats as $cat) {
$return .= "\t".'
- '.$cat->name.' '."\n"; } $return .= '
'.esc_html__('Tags', 'wp-sitemap-page').'
'."\n"; } $return .= '- '."\n";
foreach($posttags as $tag) {
$return .= "\t".'
- '.$tag->name.' '."\n"; } $return .= '
'.esc_html__('Archives', 'wp-sitemap-page').'
'."\n"; } $return .= '- '."\n";
$return .= $list_archives;
$return .= '
'.esc_html__('Authors', 'wp-sitemap-page').'
'."\n"; } $return .= '- '."\n";
$return .= $list_authors;
$return .= '
' . $cpt->label . '
'."\n"; } $return .= '- '."\n";
$return .= $list_pages;
$return .= '
' . $taxonomy_label . '
'."\n"; } $return .= '- '."\n";
$return .= $list_pages;
$return .= '
- '."\n";
}
// display a nofollow attribute ?
$attr_nofollow = ($display_nofollow==true ? ' rel="nofollow"' : '');
// List all the categories
foreach ($nav as $page) {
//si la boucle parcourt une catégories exclue, on saute
if (in_array($page->cat_ID, $exclude_categories)) {
continue;
}
// define category link
$category_link = ''.esc_html($page->name).'';
// define the text to display for the title of the category
if ($is_category_title_wording_displayed) {
$category_link_display = esc_html__('Category: ', 'wp-sitemap-page').$category_link;
} else {
$category_link_display = $category_link;
}
$html .= "\t".'
- '.$category_link_display.''."\n";
$post_by_cat = wsp_display_post_by_cat($page->cat_ID, $display_post_only_once, $display_nofollow, $wsp_exclude_pages, $sort, $order);
// List of posts for this category
$category_recursive = '';
if (!empty($page->sub)) {
// Use recursive function to get the childs categories
$category_recursive = wsp_html_from_multi_array( $page->sub, false, $display_post_only_once, $is_category_title_wording_displayed,
$display_nofollow, $wsp_exclude_pages, $sort, $order );
}
// display if it exist
if ( !empty($post_by_cat) || !empty($category_recursive) ) {
$html .= '
- ';
}
if ( !empty($post_by_cat) ) {
$html .= $post_by_cat;
}
if ( !empty($category_recursive) ) {
$html .= $category_recursive;
}
if ( !empty($post_by_cat) || !empty($category_recursive) ) {
$html .= '
'."\n";
}
if ($useUL === true) {
$html .= '