Недавно я удалил функцию извлечения из Wordpress и сделал свою собственную, чтобы я мог разрешить показ HTML в отрывке. Я внес изменения в файл функций дочерней темы.
В клиентской части он работал нормально, но после этого у меня возникли проблемы в серверной части Wordpress. В /wp-admin/edit.php
в отрывке постов я получаю отрывок поста со всеми тегами HTML, так что было действительно трудно прочитать фактическое содержание поста.
Чтобы исправить это, я внес изменения в основной файл Wordpress/wp-admin/includes/class-wp-posts-list-table.php
. Я удалил содержимое из строки 1037.
echo esc_html( get_the_excerpt() );
функция esc_html
Как я могу сделать это изменение постоянным, чтобы после возможного обновления Wordpress изменение не было потеряно?
Это изменение безопасно? У меня будет много пользователей в моем бэкэнде Wordpress.
ОБНОВЛЕНИЕ МОЕГО ВОПРОСА
add_filter( 'get_the_excerpt', 'my_clean_excerpt' );
function wpse_allowedtags() {
// Add custom tags to this string
return '<style>,<br>,<em>,<i>,<ul>,<ol>,<li>,<a>,<p>,<img>,<video>,<audio>,<div>,<wbr>';
}
if ( ! function_exists( 'wpse_custom_wp_trim_excerpt' ) ) :
function wpse_custom_wp_trim_excerpt($wpse_excerpt) {
$raw_excerpt = $wpse_excerpt;
if ( '' == $wpse_excerpt ) {
$wpse_excerpt = get_the_content('');
$wpse_excerpt = strip_shortcodes( $wpse_excerpt );
$wpse_excerpt = apply_filters('the_content', $wpse_excerpt);
$wpse_excerpt = str_replace(']]>', ']]>', $wpse_excerpt);
//$wpse_excerpt = strip_tags($wpse_excerpt, wpse_allowedtags()); /*IF you need to allow just certain tags. Delete if all tags are allowed */
//Set the excerpt word count and only break after sentence is complete.
$excerpt_word_count = 75;
$excerpt_length = apply_filters('excerpt_length', $excerpt_word_count);
$tokens = array();
$excerptOutput = '';
$count = 0;
// Divide the string into tokens; HTML tags, or words, followed by any whitespace
preg_match_all('/(<[^>]+>|[^<>\s]+)\s*/u', $wpse_excerpt, $tokens);
foreach ($tokens[0] as $token) {
if ($count >= $excerpt_length && preg_match('/[\,\;\?\.\!]\s*$/uS', $token)) {
// Limit reached, continue until , ; ? . or ! occur at the end
$excerptOutput .= trim($token);
break;
}
// Add words to complete sentence
$count++;
// Append what's left of the token
$excerptOutput .= $token;
}
$wpse_excerpt = trim(force_balance_tags($excerptOutput));
$excerpt_end = ' <a href="'. esc_url( get_permalink() ) . '">' . ' » ' . sprintf(__( 'Read more about: %s »', 'wpse' ), get_the_title()) . '</a>';
$excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
//$pos = strrpos($wpse_excerpt, '</');
//if ($pos !== false)
// Inside last HTML tag
//$wpse_excerpt = substr_replace($wpse_excerpt, $excerpt_end, $pos, 0); /* Add read more next to last word */
//else
// After the content
//$wpse_excerpt .= $excerpt_more; /*Add read more in new paragraph */
// Extra filter to remove the above text from excerpt
$badwords = array(
'< !DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">',
'< ?xml encoding="utf-8" ?>',
);
foreach ( $badwords as $badword ) {
$wpse_excerpt = str_replace( $badword, '', $wpse_excerpt);
}
//End extra filrer
return $wpse_excerpt;
}
return apply_filters('wpse_custom_wp_trim_excerpt', $wpse_excerpt, $raw_excerpt);
}
endif;
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wpse_custom_wp_trim_excerpt');
Я использовал эту функцию для добавления тегов html в выдержку.
Я после обновления выдержки из базы данных. И я получаю желаемый результат.
Но я получаю это в бэкэнде. проблемный бэкэнд
Таким образом, быстрый способ избавиться от проблемы - перейти к /wp-admin/includes/class-wp-posts-list-table.php
и удалить функцию esc_html
из этой строки echo esc_html( get_the_excerpt() );
, где генерируется выдержка дляback-end.
Так что мой вопрос не в том, как разрешить использование html-тегов в отрывке, я уже это сделал, а в том, как сделать это изменение постоянным.
Мой back-end после этого изменения, Back-end после Это то, что я хочу сделать.