Wordpress Site Admin can't publish <iframe>s on multsite network

我们两清 提交于 2019-12-25 03:31:57

问题


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

回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/52172991/wordpress-site-admin-cant-publish-iframes-on-multsite-network

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!