Wordpress Site Admin can't publish <iframe&

2019-08-20 10:38发布

I have a Wordpress multisite network. I've found that if you're not a Network Admin, Site Admins cannot publish <iframe>s or <script> tags.

What I've tried:

  • Added the unfiltered_html capability to the Admin role
  • Added iframe to the global $allowedtags
  • Added iframe to the extended_valid_elements using the tiny_mce_before_init filter

I've read around a lot, and I know there's an Iframe plugin, but It would really be best to allow my users to copy and paste Iframes from YouTube, etc.

Any insight is much appreciated!

EDIT: full code below

function add_theme_caps() {
    $role = get_role('administrator');
    $role->add_cap('unfiltered_html');
}
add_action('admin_init', 'add_theme_caps');



public function add_tags()
{
    global $allowedtags;
    $allowedtags['iframe'] = [
        'src'               => [],
        'width'             => [],
        'height'            => [],
        'frameborder'       => [],
        'style'             => [],
        'allowfullscreen'   => []
    ];
}
add_action('init', 'add_tags');



public function add_mce_tags($options) {
    // Comma separated string of extended tags
    $ext = 'iframe[align|longdesc|name|width|height|frameborder|scrolling|marginheight|marginwidth|src]';

    if (isset($options['extended_valid_elements'])) {
        $options['extended_valid_elements'] .= ',' . $ext;
    } else {
        $options['extended_valid_elements'] = $ext;
    }
    // maybe; set tiny paramter verify_html
    // $options['verify_html'] = false;
    return $options;
}
add_filter('tiny_mce_before_init', 'add_mce_tags');

2条回答
等我变得足够好
2楼-- · 2019-08-20 11:17

You can solve this issue using shortcode. If you not found needed solution.

Something like that

add_shortcode('book', array('iframe_shortcode', 'shortcode')); class iframe_shortcode {
    function shortcode($atts, $content=null) {
          extract(shortcode_atts(array(
               'url'      => '',
               'scrolling'      => 'yes',
               'width'      => '680',
               'height'      => '850',
               'frameborder'      => '0',
               'marginheight'      => '0',
          ), $atts));
          if (empty($url)) return '<!-- Iframe: You did not enter a valid URL -->';
     return '<iframe src="'.$url.'" title="" width="'.$width.'" height="'.$height.'" scrolling="'.$scrolling.'" frameborder="'.$frameborder.'" marginheight="'.$marginheight.'"><a href="'.$url.'" target="_blank">'.$url.'</a></iframe>';
    } }
查看更多
▲ chillily
3楼-- · 2019-08-20 11:27

admin isn't a valid default role, I believe you're looking for administrator. It's also considered a good practice to make sure your $role object is actually defined (though in this case it shouldn't be an issue), this should get you up and running:

function add_theme_caps() {
    if( $role = get_role( 'administrator' ) ){
        $role->add_cap('unfiltered_html');
    }
}
add_action( 'admin_init', 'add_theme_caps' );
查看更多
登录 后发表回答