Как в админку сайта добавить столбец с количеством символов в заголовке и добавить сортировку по количеству символов?
/**
* Подсчёт длины заголовка + сортировка
*/
function fenv_count_title_length( $post_id = null ) {
if ( ! $post_id ) $post_id = get_the_ID();
$post = get_post( $post_id );
if ( ! $post ) return 0;
return mb_strlen( trim( wp_strip_all_tags( $post->post_title ) ) );
}
add_action( 'save_post', 'fenv_save_title_length', 10, 3 );
function fenv_save_title_length( $post_id, $post, $update ) {
if ( wp_is_post_revision( $post_id ) ) return;
if ( wp_is_post_autosave( $post_id ) ) return;
if ( $post->post_status === 'auto-draft' ) return;
if ( ! in_array( $post->post_type, array( 'post', 'page' ) ) ) return;
$length = mb_strlen( trim( wp_strip_all_tags( $post->post_title ) ) );
update_post_meta( $post_id, '_title_char_count', intval( $length ) );
}
// Колонка ВСТАВЛЯЕТСЯ ПОСЛЕ ЗАГОЛОВКА
add_filter( 'manage_posts_columns', 'fenv_add_title_length_column' );
add_filter( 'manage_pages_columns', 'fenv_add_title_length_column' );
function fenv_add_title_length_column( $columns ) {
$new_columns = array();
foreach ( $columns as $key => $label ) {
$new_columns[ $key ] = $label;
if ( 'title' === $key ) {
$new_columns['title_length'] = 'Длина заголовка';
}
}
return $new_columns;
}
add_action( 'manage_posts_custom_column', 'fenv_fill_title_length_column', 10, 2 );
add_action( 'manage_pages_custom_column', 'fenv_fill_title_length_column', 10, 2 );
function fenv_fill_title_length_column( $column, $post_id ) {
if ( $column !== 'title_length' ) return;
$length = get_post_meta( $post_id, '_title_char_count', true );
if ( $length === '' ) {
$p = get_post( $post_id );
if ( $p ) {
$length = mb_strlen( trim( wp_strip_all_tags( $p->post_title ) ) );
update_post_meta( $post_id, '_title_char_count', intval( $length ) );
} else {
$length = 0;
}
}
$length = intval( $length );
$color = $length < 30 ? '#c00' : ( $length < 60 ? '#ff8c00' : '#1f7a1f' );
echo '<strong style="color:' . esc_attr( $color ) . ';">' . $length . '</strong>';
}
add_filter( 'manage_edit-post_sortable_columns', 'fenv_sortable_title_length_column' );
function fenv_sortable_title_length_column( $columns ) {
$columns['title_length'] = 'title_length';
return $columns;
}
add_action( 'pre_get_posts', 'fenv_sort_title_length_query' );
function fenv_sort_title_length_query( $query ) {
if ( ! is_admin() || ! $query->is_main_query() ) return;
if ( $query->get( 'orderby' ) === 'title_length' ) {
$query->set( 'meta_key', '_title_char_count' );
$query->set( 'orderby', 'meta_value_num' );
}
}
add_action( 'admin_init', 'fenv_bulk_count_title_length_once' );
function fenv_bulk_count_title_length_once() {
if ( ! current_user_can( 'manage_options' ) ) return;
if ( get_option( 'fenv_title_length_count_done' ) ) return;
$posts = get_posts( array(
'post_type' => array( 'post', 'page' ),
'posts_per_page' => -1,
'post_status' => 'any',
) );
foreach ( $posts as $p ) {
$length = mb_strlen( trim( wp_strip_all_tags( $p->post_title ) ) );
update_post_meta( $p->ID, '_title_char_count', intval( $length ) );
}
update_option( 'fenv_title_length_count_done', 1 );
}