| 1 |
<?php |
| 2 |
/** |
| 3 |
* Admin Slug Column |
| 4 |
* |
| 5 |
* @package Admin_Slug_Column |
| 6 |
* @author Chuck Reynolds |
| 7 |
* @link https://chuckreynolds.com |
| 8 |
* @copyright 2013 Rynoweb LLC |
| 9 |
* @license GPL-2.0-or-later |
| 10 |
* |
| 11 |
* @wordpress-plugin |
| 12 |
* Plugin Name: Admin Slug Column |
| 13 |
* Plugin URI: https://github.com/chuckreynolds/Admin-Slug-Column |
| 14 |
* Description: Adds the URL path to the admin columns on all post type edit screens. |
| 15 |
* Version: 2.0.2 |
| 16 |
* Requires at least: 5.2 |
| 17 |
* Requires PHP: 7.4 |
| 18 |
* Author: Chuck Reynolds |
| 19 |
* Author URI: https://chuckreynolds.com |
| 20 |
* Text Domain: admin-slug-column |
| 21 |
* License: GPLv2 or later |
| 22 |
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt |
| 23 |
*/ |
| 24 |
|
| 25 |
// If this file is called directly, abort. |
| 26 |
if ( ! defined( 'WPINC' ) ) { |
| 27 |
die; |
| 28 |
} |
| 29 |
|
| 30 |
// Only run plugin in the admin |
| 31 |
if ( ! is_admin() ) { |
| 32 |
return; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Class WPAdminSlugColumn |
| 37 |
*/ |
| 38 |
class WPAdminSlugColumn { |
| 39 |
|
| 40 |
/** |
| 41 |
* Constructor for WPAdminSlugColumn Class |
| 42 |
*/ |
| 43 |
public function __construct() { |
| 44 |
add_action( 'current_screen', [ $this, 'init' ] ); |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Initialize the plugin |
| 49 |
* |
| 50 |
* @param WP_Screen $current_screen The current screen object. |
| 51 |
* @return void |
| 52 |
*/ |
| 53 |
public function init( WP_Screen $current_screen ): void { |
| 54 |
if ( 'edit' !== $current_screen->base ) { |
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
add_filter( "manage_{$current_screen->post_type}_posts_columns", [ $this, 'add_column' ], 10, 1 ); |
| 59 |
add_action( "manage_{$current_screen->post_type}_posts_custom_column", [ $this, 'display_column' ], 10, 2 ); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Adds Slug column to Posts list column |
| 64 |
* |
| 65 |
* @param array<string, string> $columns An array of column names. |
| 66 |
* @return array<string, string> Modified array of column names. |
| 67 |
*/ |
| 68 |
public function add_column( array $columns ): array { |
| 69 |
$new_columns = []; |
| 70 |
$insert_after = 'title'; |
| 71 |
|
| 72 |
foreach ( $columns as $key => $value ) { |
| 73 |
$new_columns[ $key ] = $value; |
| 74 |
if ( $key === $insert_after ) { |
| 75 |
$new_columns['wpasc-slug'] = __( 'URL Path', 'admin-slug-column' ); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
return $new_columns; |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Displays the slug and/or path in the custom column |
| 84 |
* |
| 85 |
* @param string $column_name Name of the column. |
| 86 |
* @param int $post_id Post ID. |
| 87 |
* @return void |
| 88 |
*/ |
| 89 |
public function display_column( string $column_name, int $post_id ): void { |
| 90 |
if ( 'wpasc-slug' !== $column_name ) { |
| 91 |
return; |
| 92 |
} |
| 93 |
|
| 94 |
$post = get_post( $post_id ); |
| 95 |
if ( ! $post instanceof WP_Post ) { |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
if ( in_array( $post->post_status, [ 'draft', 'pending', 'future' ], true ) ) { |
| 100 |
$this->display_draft_slug( $post ); |
| 101 |
} else { |
| 102 |
$this->display_published_slug( $post ); |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Displays the slug for draft, pending, or future posts |
| 108 |
* |
| 109 |
* @param WP_Post $post Post object. |
| 110 |
* @return void |
| 111 |
*/ |
| 112 |
private function display_draft_slug( WP_Post $post ): void { |
| 113 |
$post_draft_url_array = get_sample_permalink( $post ); |
| 114 |
if ( ! is_array( $post_draft_url_array ) || count( $post_draft_url_array ) !== 2 ) { |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
$post_draft_url_pre = str_replace( home_url(), '', $post_draft_url_array[0] ); |
| 119 |
// urldecode() not needed here; get_sample_permalink()[1] already handles multibyte decoding. |
| 120 |
// preg_replace handles any CPT rewrite tag placeholder, not just %postname%/%pagename%. |
| 121 |
$post_slug = preg_replace( '/%[^%]+%/', $post_draft_url_array[1], $post_draft_url_pre ); |
| 122 |
printf( |
| 123 |
'<span style="color: #999;">%s</span>', |
| 124 |
esc_html( $post_slug ) |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Displays the slug for published posts |
| 130 |
* |
| 131 |
* @param WP_Post $post Post object. |
| 132 |
* @return void |
| 133 |
*/ |
| 134 |
private function display_published_slug( WP_Post $post ): void { |
| 135 |
$permalink = get_permalink( $post ); |
| 136 |
if ( ! is_string( $permalink ) ) { |
| 137 |
return; |
| 138 |
} |
| 139 |
|
| 140 |
$post_slug = str_replace( home_url(), '', $permalink ); |
| 141 |
echo esc_html( urldecode( $post_slug ) ); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
// Initialize the plugin |
| 146 |
add_action( 'plugins_loaded', function() { |
| 147 |
new WPAdminSlugColumn(); |
| 148 |
} ); |
| 149 |
|
| 150 |
|