Your 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',
)
)
);