How can I control the HTML output of my post?

As you already know, WordPress manipulates posts, even if you write everything in Text mode. (replacing line breaks with rn ‘s , converting single quotes into double quotes etc.)

I know this is good for helping us not to mess things up, however, in my case, I want to put HTML codes inside my post -both <code> and </code> tags , and some HTML directly-.

Read More

so I want to make sure what I see in Text mode (or install an HTML mode somehow) , so that What I See is What I Get, in terms of HTML 🙂

Maybe a better explanation for what I want is this :

What I write in “add new post” window should be exactly equal to what is printed between <div class='entry-content'> and </div>.

Any tips for this?

Should I change my theme? Or a plugin for tinymce?

p.s.:

I know single quotes should be escaped for database, but I want to preserve line breaks, html tags etc. So please tell me there is a way other than writing directly into the database.

Related posts

3 comments

  1. I have decided that the best solution is to just disable the visual editor,
    from Users -> Your Profile -> Disable visual editor when writing.

  2. get_user_meta() is a wrapper for get_metadata(). In case this gets triggered to question if a user can use the Rich Editor, you can disable it with that call.

    <?php
    /**
     * Plugin Name: (#116210) Disable RichEdit by User or Role
     * Author:      Franz Josef Kaiser <wecodemore@gmail.com>
     */
    defined( 'ABSPATH' ) OR exit;
    
    add_filter( 'get_user_metadata', 'wpse116210VisualEditorDisabled', 20, 4 );
    function wpse116210VisualEditorDisabled( $enabled, $object_id, $meta_key, $single ) {
        if ( ! is_admin() )
            return $enabled;
    
        // Conditional switch for different post types
        if ( 'post' === get_current_screen()->post_type )
        {
            $user = wp_get_current_user();
            // Check here if you want *that* user/role to be enabled/disabled.
            $enabled = FALSE;
        }
    
        return $enabled;
    }
    

    Another option might be to use the WP_Role#has_cap check (in case it’s used there). It has a filter as well: 'role_has_cap'

    <?php
    /**
     * Plugin Name: (#116210) Disable RichEdit by Role
     * Author:      Franz Josef Kaiser <wecodemore@gmail.com>
     */
    defined( 'ABSPATH' ) OR exit;
    
    add_filter( 'role_has_cap', 'wpse116210RoleHasCap' );
    function wpse116210RoleHasCap( $allCaps, $checkedCap, $roleName )
    {
        $role = get_role( $roleName );
        // Do your check if you want to allow it. Return *boolean*.
    
        return $allCaps[ $checkedCap ];
    }
    
  3. IIRC this is the user meta/caps filter:

    <?php
    /**
     * Plugin Name: (#116210) Disable RichEdit for some post types
     * Author:      Franz Josef Kaiser <wecodemore@gmail.com>
     */
    defined( 'ABSPATH' ) OR exit;
    
    add_filter( 'user_can_richedit', function( $enabled ) {
        if ( ! is_admin() )
            return $enabled;
    
        // Conditional switch for different post types
        // If you want to check against multiple post types, use in_array()
        return 'post' === get_current_screen()->post_type;
    }
    

    If you want to globally disable it for all post types, you can use the following shortcut:

    <?php
    /**
     * Plugin Name: (#116210) Disable RichEdit for some post types
     * Author:      Franz Josef Kaiser <wecodemore@gmail.com>
     */
    defined( 'ABSPATH' ) OR exit;
    add_filter( 'user_can_richedit', '__return_false' );
    

Comments are closed.