• hello
    Is there anyway to moderate use of shortcodes in wordpress?
    I like to forbid my subscribs for use shortcodes in they tinymce!
    and only admins can type and use of shortcodes.
    thanks.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Best I can think of is to use add_filter with wp_insert_post_data hook and use a regular expression to prevent shortcodes (opening and closing square brackets and content in between) from begin saved. Probably no good way to stop user from actually typing them.

    Thread Starter mehdi-w

    (@mehdi-w)

    @scriptrunner (Doug Sparling)
    thank you
    but i am begginer in wordpress and don’t know php
    Can you explain this solution exactly?
    thanks.

    I’ll see if anyone has done this before via plugin or functions.php first. I’m just basing my answer on what I think could be done. I’d actually have to sit down and write some test code (which I do quite often for forum answers).

    Try this in your theme’s functions.php:

    function filter_handler( $data , $postarr ) {
        if ( !current_user_can( 'manage_options' ) ) {
            $data['post_content'] = preg_replace( "/\[([^\[\]]++|(?R))*+\]/", "", $data['post_content'] );
        }
        return $data;
    }
    add_filter( 'wp_insert_post_data', 'filter_handler', '99', 2 );

    This will prevent anyone other than admin/superadmin from inserting shortcodes (well, it will delete a pair of square brackets and anything in between when a non-admin inserts or updates a page/post – if there’s a better way I haven’t found it yet).

    Interesting problem!

    If you don’t want to mess with the input, you can always mess with the output ??

    One idea would be to check the capability of the current post author and
    remove the do_shortcode filter

    remove_filter( 'the_content', do_shortcode', 11 );

    if the post author is on your shortcodes black list.

    I think my plugin “Moody Shortcodes” can help you:
    https://www.ads-software.com/plugins/wp-moody-shortcodes/

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Is there anyway to moderate use of shortcodes in wordpress?’ is closed to new replies.