$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to make shortcode to hide selection of text from post or page?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to make shortcode to hide selection of text from post or page?

matteradmin8PV0评论

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

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

Share Improve this question asked Dec 26, 2012 at 19:17 user25236user25236 411 silver badge2 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 6

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:

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()
{
    add_meta_box(
        't5_extra',
        'Extra',
        't5_extra_metabox_callback',
        NULL, // screen
        'normal',
        'default'
    );
}
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 );
    wp_editor(
        $content,
        '_t5_extra_box',
        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__ )
    )
    {
        return;
    }

    if ( isset ( $_POST['_t5_extra_box'] ) )
        update_post_meta( $post_id, '_t5_extra_box', $_POST['_t5_extra_box'] );
    else
        delete_post_meta( $post_id, '_t5_extra_box' );
}

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

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.

Post a comment

comment list (0)

  1. No comments so far