2

I'm using the code below in my functions.php to pre-populate new posts.

It works fine, except if I use entities (example: »), which are converted (example: ») when I open the edit page for a new post.

I prefer to keep them unconverted.

add_filter( 'default_content', 'pu_default_editor_content' );

function pu_default_editor_content( $content ) {
  global $post_type;
  switch( $post_type ) {
    case 'post':
      $content = 'Default content for blog posts.';
    break;
  }
  return $content;
}
  • Any reason why you can't use the text tab in the editor? – Andy Macaulay-Brook Oct 2 '16 at 16:42
  • @Andy Macaulay-Brook... not sure what you mean. – glvr Oct 3 '16 at 6:57
  • I read your question as saying that you don't like seeing the conversion in the visual editor so I though maybe switching to the text tab there would be easier than coding something, or have I misunderstood? – Andy Macaulay-Brook Oct 3 '16 at 10:09
  • @Andy Macaulay-Brook... ah, understood. For years, I've disabled the visual editor. – glvr Oct 4 '16 at 7:19
1

Default Editor Content

Here is an updated version of the pu_default_editor_content() function which will take into account for when a user has disabled the Visual Editor (per your comment).

function pu_default_editor_content( $content ) {
    global $post_type;

    switch( $post_type ) {
        case 'post' :
            $content = 'Default content for blog posts. » » »';
            $rich_editing = get_user_option( 'rich_editing', get_current_user_id() );

            if ( 'false' === $rich_editing ) {
                $content = htmlspecialchars( $content );
            }   
        break;
    }

    return $content;
}
add_filter( 'default_content', 'pu_default_editor_content' );

Rich Text Editor Disabled view

editor-default-content-rich-text-disabled User has Visual Editor disabled under User > Profile.

TinyMCE/QuickTags

If the Visual Editor is enabled, which is the default setup, TinyMCE will need to be configured to show named entities. Encoding is being applied by TinyMCE, but this behavior can be customized via the tiny_mce_before_init hook. Thanks to this answer for the tip.

Add additional entities that you may need to the $settings['entities'] string. Documentation on formatting is here:

This option contains a comma separated list of entity names that is used instead of characters. Odd items are the character code and even items are the name of the character code.

// Custom configuration for TinyMCE
function wpse241282_tiny_mce_before_init( $settings, $editor_id ) {

    // Used named encodings (the default setting in WordPress is raw).
    // https://www.tinymce.com/docs/configure/content-filtering/#encodingtypes
    $settings['entity_encoding'] = 'named';

    // Default entities for TinyMCE: 38,amp,60,lt,62,gt1
    // Additional entities: copy, reg, trade, service mark, euro, right angle quote, left angle quote
    // The odd entires are the entity *number*, the even entries are the entity *name*. If the entity has no name,
    // use the number, prefixed with a hash (for example, the service mark is "8480,#8480").

    $entities = '169,copy,174,reg,8482,trade,8480,#8480,8364,euro,187,raquo,171,laquo';

    if ( isset( $settings['entities'] ) && ! empty( $settings['entities'] ) ) {
        $settings['entities'] .= ',' . $entities;
    } else {
        $settings['entities'] = $entities;
    }
    return $settings;
}
add_filter( 'tiny_mce_before_init', 'wpse241282_tiny_mce_before_init', 10, 2 );
add_filter( 'quicktags_settings', 'wpse241282_tiny_mce_before_init', 10, 2 );

Visual Editor Enabled

enter image description here User has Visual Editor enabled under User > Profile.

  • thanks. But I have the visual editor disabled, so does TinyMCE still become involved? The suggested code doesn't change things. – glvr Oct 3 '16 at 6:56
  • Okay, that's an important detail it turns out. I've updated the answer and have tested the code for when user's have disabled the visual editor. – Dave Romsey Oct 3 '16 at 8:48
  • thanks, appreciated. But it seems to be of no effect, the new post is populated with » rather than ». – glvr Oct 4 '16 at 7:17
  • All good. I tried, and it's working for me, for whatever that's worth :-) Added some images to illustrate what I'm seeing. – Dave Romsey Oct 4 '16 at 8:30
  • Thanks, again. ;-) I'll investigate my code to sort this. – glvr Oct 5 '16 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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