1

Is there a way to "freeze" a shortcode in place so non-Administrators can't edit it?

Let's say I have a page like this:

[shortcode]

Text of the page

[other shortcode]


Is there a way that non-Administrators would see the page as the following:

[shortcode]

Text of the page

[other shortcode]

Where the italicized shortcodes are untouchable/uneditable?

0

You can check for user capability (aka non-admins) and remove shortcode you don't want to be formatted:

...in functions.php

$cuser = wp_get_current_user();
if (!isset($cuser->allcaps['administrator']) || !$cuser->allcaps['administrator']) {
    remove_shortcode('your_shortcode_tag_name');
}

You can also remove all shortcodes using remove_all_shortcodes function.

  • that's amazing! i wouldn't need to remove shortcodes, just don't want other users to touch them. i'll need to try this out! – Jamie Oct 21 '15 at 16:32
  • can i make this a general "hide all shortcodes" thing, or do i have to specify by specific shortcode? – Jamie Oct 21 '15 at 16:43
  • this doesn't hide shortcodes in the editor, it removes the shortcode handler that renders the shortcode when content is viewed on the front end. – Milo Oct 21 '15 at 17:47
  • If I understand correctly OP wanted to still have actual string "[shortcode]" visible inside the content. – mjakic Oct 22 '15 at 14:51
  • @Jamie you can wrap the whole thing in a function and/or add all shortcode names to some array and loop the remove_shortcode part. – mjakic Oct 22 '15 at 14:52
0

AFAIK, there is no way to do it like that, but you can hook into the filter the_content and prepend the output of do_shortcode( 'before_content_shortcode' ) to the actual content and append the the output of do_shortcode( 'after_content_shortcode' ).

function so_modify_the_content( $content ) {
    $prependContent = do_shortcode( 'pre_content_shortcode' );
    $appendContent  = do_shortcode( 'post_content_shortcode' );

    $content        = $prependContent . $content . $appendContent;

    return $content; 
}

add_filter( 'the_content', 'so_modify_the_content' );
  • can you explain in further detail? i'm a bit new to hooks and filters :) – Jamie Oct 21 '15 at 16:31
  • Please, see my edit. The function runs shortcodes, stores their results and then wraps content in those results, so to say. And it's triggered when the content is being showed on the page to the visitor. – iSS Oct 21 '15 at 16:50

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.