admin管理员组文章数量:1124174
i would like to add placeholder when using wp_editor()
function.
I tried to pass placeholder attribute in textarea, but didn't work. I tried to pass it in settings
wp_editor(
$project_description ,
'project_description',
array(
'wpautop' => true,
'media_buttons' => false,
'textarea_name' => 'project_description',
'editor_class' => 'is-project-description-textarea',
'textarea_rows' => 5,
//'textarea_placeholder' => 'Project Description WP Editor Default',
'placeholder' => 'Project Description WP Editor Default 2'
)
);
I checked then the _WP_Editors::parse_settings( string $editor_id, array $settings ): array
Also tried to replace <textarea
with <textarea placeholder="
$wpEditorPlaceholder = 'My Placeholder';
$wpEditor = wp_editor(
$project_description ,
'project_description',
array(
'wpautop' => true,
'media_buttons' => false,
'textarea_name' => 'project_description',
'editor_class' => 'is-project-description-textarea',
'textarea_rows' => 5,
)
);
$wpEditorNew = str_replace('<textarea', '<textarea placeholder="' . $wpEditorPlaceholder . '" ', $wpEditor);
echo $wpEditorNew;
but didn't work and then i added it via add_action
add_action('the_editor', 'add_placeholder_to_editor');
function add_placeholder_to_editor($editorMarkup)
{
if (str_contains($editorMarkup, 'is-project-description-textarea')) {
$editorMarkup = str_replace('<textarea', '<textarea placeholder="Hello"', $editorMarkup);
}
return $editorMarkup;
}
Placeholder added successfully but it didn't show it when js loaded
Then i thought to add tinymce in settings
wp_editor(
$project_description ,
'project_description',
array(
'wpautop' => true,
'media_buttons' => false,
'textarea_name' => 'project_description',
'editor_class' => 'is-project-description-textarea',
'textarea_rows' => 5,
'tinymce' => array(
'paste_as_text' => true,
'placeholder' => 'Project Description',
)
)
);
Key paste_as_text
worked but key placeholder
didn't work.
What i miss? How can i add placeholder attribute?
i would like to add placeholder when using wp_editor()
function.
I tried to pass placeholder attribute in textarea, but didn't work. I tried to pass it in settings
wp_editor(
$project_description ,
'project_description',
array(
'wpautop' => true,
'media_buttons' => false,
'textarea_name' => 'project_description',
'editor_class' => 'is-project-description-textarea',
'textarea_rows' => 5,
//'textarea_placeholder' => 'Project Description WP Editor Default',
'placeholder' => 'Project Description WP Editor Default 2'
)
);
I checked then the _WP_Editors::parse_settings( string $editor_id, array $settings ): array
Also tried to replace <textarea
with <textarea placeholder="
$wpEditorPlaceholder = 'My Placeholder';
$wpEditor = wp_editor(
$project_description ,
'project_description',
array(
'wpautop' => true,
'media_buttons' => false,
'textarea_name' => 'project_description',
'editor_class' => 'is-project-description-textarea',
'textarea_rows' => 5,
)
);
$wpEditorNew = str_replace('<textarea', '<textarea placeholder="' . $wpEditorPlaceholder . '" ', $wpEditor);
echo $wpEditorNew;
but didn't work and then i added it via add_action
add_action('the_editor', 'add_placeholder_to_editor');
function add_placeholder_to_editor($editorMarkup)
{
if (str_contains($editorMarkup, 'is-project-description-textarea')) {
$editorMarkup = str_replace('<textarea', '<textarea placeholder="Hello"', $editorMarkup);
}
return $editorMarkup;
}
Placeholder added successfully but it didn't show it when js loaded
Then i thought to add tinymce in settings
wp_editor(
$project_description ,
'project_description',
array(
'wpautop' => true,
'media_buttons' => false,
'textarea_name' => 'project_description',
'editor_class' => 'is-project-description-textarea',
'textarea_rows' => 5,
'tinymce' => array(
'paste_as_text' => true,
'placeholder' => 'Project Description',
)
)
);
Key paste_as_text
worked but key placeholder
didn't work.
What i miss? How can i add placeholder attribute?
Share Improve this question asked Mar 8, 2024 at 8:57 SakSak 234 bronze badges1 Answer
Reset to default 0Your approach to replace <textarea
with <textarea placeholder="
is correct. Just wp_editor
function does not returns html code, which you can change. Function just echoes output.
You can capture this output with ob_start
and ob_get_clean
, as described here:
$wpEditorPlaceholder = 'My Placeholder';
ob_start();
wp_editor(
$project_description ,
'project_description',
array(
'wpautop' => true,
'media_buttons' => false,
'textarea_name' => 'project_description',
'editor_class' => 'is-project-description-textarea',
'textarea_rows' => 5,
)
);
$wpEditor = ob_get_clean();
$wpEditorNew = str_replace('<textarea', '<textarea placeholder="' .
$wpEditorPlaceholder . '" ', $wpEditor);
echo $wpEditorNew;
Or, even, you can add placeholder with the action. Unfortunately, there's no TinyMCE setting for adding placeholder, but you can use two filters to add placeholder: wp_editor_settings
- to remember placeholder value and the_editor
- to alter <textarea>
tag.
Example code for your functions.php
, or for your plugin:
class AddPlaceholderTinyMCE {
private $placeholder = '';
public function __construct()
{
add_filter( 'wp_editor_settings', array( $this, 'wp_editor_settings_callback' ), 10, 2 );
}
public function wp_editor_settings_callback( $settings, $editor_id ) {
if ( ! empty( $settings['tinymce']['placeholder'] ) ) {
$this->placeholder = $settings['tinymce']['placeholder'];
add_filter( 'the_editor', array( $this, 'the_editor_callback' ) );
}
}
public function the_editor_callback( $html ) {
remove_filter( 'the_editor', array( $this, 'the_editor_callback' ) );
return str_replace( '<textarea', '<textarea placeholder="' . esc_attr( $this->placeholder ) . '"', $html );
}
}
new AddPlaceholderTinyMCE();
When you call wp_editor
with placeholder
setting for tinymce, it will show you specified placeholder value:
wp_editor(
$project_description ,
'project_description',
array(
...
'tinymce' => array(
'placeholder' => 'Project Description',
)
)
);
本文标签: tinymceHow to add placeholder in wpeditor
版权声明:本文标题:tinymce - How to add placeholder in wp_editor? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736621080a1945576.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论