post_type; } /** * Adds Slug column to Posts/Pages list column * * @param array $defaults An array of column names. */ public function WPASC_posts( $defaults ) { $defaults['wpasc-slug'] = __( 'Slug', 'admin-slug-column' ); return $defaults; } /** * Gets the post info from get_post function and displays the slug * * @param string $column_name Name of the column * @param int $id post id * * @see https://developer.wordpress.org/reference/functions/get_post/ */ public function WPASC_posts_data( $column_name, $id ) { if ( $column_name == 'wpasc-slug' ) { $post_info = get_post( $id, 'string', 'display' ); $post_slug = $post_info->post_name; $post_type = $post_info->post_type; $post_parent = $post_info->post_parent; $post_status = $post_info->post_status; // if post-type is page type we're going to do some extra stuff if ( $post_type === 'page' ) { // add root slash but only for published pages; ignore drafts if ( $post_status === 'publish' ) { $post_slug = '/' . $post_slug; } // add the parent slug in there too if this is a child page if ( $post_parent > 0 ) { $pre_post_slug = get_post_field( 'post_name', $post_parent, 'raw' ); $post_slug = '/' . $pre_post_slug . $post_slug; } } // if this is the front page then just show root slash $frontpage_id = get_option( 'page_on_front' ); if ( $frontpage_id == $id ) { $post_slug = '/'; } echo esc_attr( $post_slug ); } } /** * Adds Slug column to Posts/Pages sortable detection * * @param array $sortable_columns An array of sortable column names. */ public function WPASC_sort_posts( $sortable_columns ) { $sortable_columns[ 'wpasc-slug' ] = 'wpasc-slug'; return $sortable_columns; } /** * Function to handle the sort ordering conditions * * @param array $query An array of admin url params */ public function WPASC_sort_posts_orderby( $query ) { if ( ! $query->is_main_query() ) { return; } if ( 'wpasc-slug' === $query->get( 'orderby') ) { $query->set( 'orderby', 'post_name' ); } } } $WPAdminSlugColumn = new WPAdminSlugColumn();