2

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

enter image description here enter image description here

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?

1 Answer 1

0

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',
         )
     ) 
 );
2
  • Thank you for your answer. Both of your suggestions are working in text editor but visual editor shows empty. Everything is updated to latest version and there aren't any errors in console. Also i deactivated all plugins with same results. text editor with ob_get_clean (ibb.co/wwQZ459), text editor with filters (ibb.co/6NCB4sn) and visual editor (ibb.co/4jZ2gCs)
    – Sak
    Commented Mar 11 at 7:26
  • Seems that WordPress using older version of TinyMCE, that does not have built in feature to display placeholder over visual text editor. Newer version have it. I was thinking about updating TinyMCE to that new version, but It probably will be incompatible with WordPress. It will be easier to write a plugin for current TinyMCE version, that will just show that placeholder in visual editor. Easier, but not easy.
    – Roman
    Commented Mar 12 at 17:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.