is there any shortcode or a plugin that will add new quicktag, like this example:

[hidemytext]some hidden text[/hidemytext]

and any text inside those tag is hidden from the post or from the page.

Thanks and regards


Simple answer:

add_shortcode( 'hidemytext', '__return_false' );

But shortcodes should never be used like that. Imagine what happens when the plugin or theme with that shortcode is turned off: everyone can see the content now. This is not very user friendly.

So switch the logic: show nothing or the bare shortcode tag by default, show the content only if the shortcode handler is active. Store the hidden content in a simple metabox:

enter image description here

To do that, add a metabox with an editor and show its content per shortcode:

add_shortcode( 'extra',  't5_extra_content' );
add_action( 'add_meta_boxes_post', 't5_register_extra_metabox' );
add_action( 'save_post', 't5_save_shortcode_box', 10, 2);

function t5_extra_content( $attributes, $content = '' )
    $args = shortcode_atts( array ( 'cap' => 'edit_posts' ), $attributes );

    if ( current_user_can( $args['cap'] ) )
        return wpautop(
            get_post_meta( get_the_ID(), '_t5_extra_box', TRUE )
            . $content

function t5_register_extra_metabox()
        NULL, // screen
function t5_extra_metabox_callback( $post )
    $nonce = wp_create_nonce( __FILE__ );
    echo "<input type='hidden' name='t5_extra_box_nonce' value='$nonce' />";
    $content = get_post_meta($post->ID, '_t5_extra_box', TRUE );
        array (
            'textarea_rows' => 10,
            'media_buttons' => FALSE,
            'teeny'         => TRUE,
            'tinymce'       => TRUE
function t5_save_shortcode_box( $post_id )
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE
        or ! isset ( $_POST['post_type'] )
        or 'post' !== $_POST['post_type']
        or ! current_user_can( 'edit_post', $post_id )
        or ! wp_verify_nonce( $_POST[ 't5_extra_box_nonce' ], __FILE__ )

    if ( isset ( $_POST['_t5_extra_box'] ) )
        update_post_meta( $post_id, '_t5_extra_box', $_POST['_t5_extra_box'] );
        delete_post_meta( $post_id, '_t5_extra_box' );
| improve this answer | |
  • Very true, the deactivation of plugins poses issues but this solution also poses some issues in some circumstances. Consider the fact that the post content isn't where other plugins would expect to find it. SEO plugins are the best example. I've yet to see an SEO plugin that allows us to tell it that a specific meta key holds content that should be parsed with the post content. – Ryan Bayne Feb 21 '19 at 14:25
  • If a budget exists. A custom plugin should always be considered. My opinion is that all commercial sites should have their own custom plugin that handles some of the simple but key functionality. The OP requires something that is very basic and would cost very little to implement. The advantage is not relying on a third-party plugin to hide content and maintain a members only system. – Ryan Bayne Feb 21 '19 at 14:31

You really should be able to work this out from the examples in the Codex.

function hide_text( $atts,$content) {
  return ''; // returns nothing
  // or
  return '<!-- '.$content.' -->'; // returns an html commented version of your text
add_shortcode( 'hidemytext', 'hide_text' );
| improve this answer | |
  • This would answer, "How to display content using a shortcode if the visitor is allowed to view the content" but this does not hide post content entered in the WYSIWYG editor or Gutenberg blocks which is what I believe the OP has requested. – Ryan Bayne Feb 21 '19 at 14:06

You could set up a filter hook and filter all post content. The hooked function would return a default "Not Permitted" type content or if the visitor has permission, the function would return the actual post content.

I'm about to implement something in TwitchPress that might use this approach but take it a step further. I might detect a specific block (basically detect the blocks tags/html) and hide all content after that block. This allows some flexibility when authoring posts.

The approach with a block isn't much different from your own idea. The block just makes it all drag and drop instead of typing [hide] or something similar. Whichever approach, the post content requires filtering. I'm concerned about the extra processing as opposed to restricting the entire post and redirecting none permitted users to a standard "Not Permitted" page which done when WordPress is initializing.

So there are many approaches. Performance and flexibility will be different with each. I think I'll implement them all in TwitchPress.

| improve this answer | |

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.