Главная
›
Новости
The_editor_content — Устанавливает предварительный контент/текст для редактора WordPress. Т.е. текст по умолчанию. Хук-фильтр WordPress
Опубликовано: 01.09.2018
Устанавливает предварительный контент/текст для редактора WordPress. Т.е. текст по умолчанию.
C помощью этого фильтра можно, например, установить начальный контент для типа записи, при создании новой записи.
Есть еще аналогичный хук default_content, который устанавливает контент по умолчанию для создаваемой записи, а не для редактора. Он срабатывает при любой публикации записи: даже при press-this, xmlrpc и т.д. Оба хука взаимозаменяемы в 80% случаев.
Также, есть подобные хуки:
для заголовка - default_title
для цитаты: default_excerpt.
Использование
add_filter( 'the_editor_content', '____filter_function_name' );
function ____filter_function_name( $content ) {
// Фильтр...
return $content;
}
$content
(строка)
Контент который будет установлен в редактор WordPress.
Примеры
#1 Установим начальный контент для типа записи xxx
add_filter('the_editor_content', 'new_xxx_content');
function new_xxx_content( $content ){
global $post;
// Устанавливаем текст только если контента еще нет и это нужный тип записи
if( empty( $content ) && $post->post_type == 'xxx' ){
return 'Это начальный текст для записи.';
}
return $content;
}
#2 Установим контент по умолчанию через хук 'default_content'
add_filter( 'default_content', 'custom_post_type_content' );
function custom_post_type_content( $content ) {
if( function_exists('get_current_screen') && get_current_screen()->post_type == 'my_post') {
$content = 'Контент по умолчанию для постов типа: "my_post"';
return $content;
}
}
Где используется хук
...
// Prepare the content for the Visual or Text editor, only when TinyMCE is used (back-compat).
if ( self::$this_tinymce ) {
add_filter( 'the_editor_content', 'format_for_editor', 10, 2 );
}
/**
* Filters the default editor content.
*
* @since 2.1.0
*
* @param string $content Default editor content.
* @param string $default_editor The default editor for the current user.
* Either 'html' or 'tinymce'.
*/
$content = apply_filters( 'the_editor_content', $content, $default_editor );
// Remove the filter as the next editor on the same page may not need it.
if ( self::$this_tinymce ) {
remove_filter( 'the_editor_content', 'format_for_editor' );
}
// Back-compat for the `htmledit_pre` and `richedit_pre` filters
if ( 'html' === $default_editor && has_filter( 'htmledit_pre' ) ) {
// TODO: needs _deprecated_filter(), use _deprecated_function() as substitute for now
_deprecated_function( 'add_filter( htmledit_pre )', '4.3.0', 'add_filter( format_for_editor )' );
$content = apply_filters( 'htmledit_pre', $content );
} elseif ( 'tinymce' === $default_editor && has_filter( 'richedit_pre' ) ) {
_deprecated_function( 'add_filter( richedit_pre )', '4.3.0', 'add_filter( format_for_editor )' );
$content = apply_filters( 'richedit_pre', $content );
}
...
...
'text' => '',
)
);
?>
<?php if ( ! $this->is_legacy_instance( $instance ) ) : ?>
<?php
if ( user_can_richedit() ) {
add_filter( 'the_editor_content', 'format_for_editor', 10, 2 );
$default_editor = 'tinymce';
} else {
$default_editor = 'html';
}
/** This filter is documented in wp-includes/class-wp-editor.php */
$text = apply_filters( 'the_editor_content', $instance['text'], $default_editor );
// Reset filter addition.
if ( user_can_richedit() ) {
remove_filter( 'the_editor_content', 'format_for_editor' );
}
// Prevent premature closing of textarea in case format_for_editor() didn't apply or the_editor_content filter did a wrong thing.
$escaped_text = preg_replace( '#</textarea#i', '</textarea', $text );
?>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" class="title sync-input" type="hidden" value="<?php echo esc_attr( $instance['title'] ); ?>">
<textarea id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>" class="text sync-input" hidden><?php echo $escaped_text; ?></textarea>
<input id="<?php echo $this->get_field_id( 'filter' ); ?>" name="<?php echo $this->get_field_name( 'filter' ); ?>" class="filter sync-input" type="hidden" value="on">
<input id="<?php echo $this->get_field_id( 'visual' ); ?>" name="<?php echo $this->get_field_name( 'visual' ); ?>" class="visual sync-input" type="hidden" value="on">
<?php else : ?>
...