简体   繁体   English

如何在 Wordpress 编辑器中放入脚本标签?

[英]How to put in script tags inside Wordpress Editor?

Been struggling with this problem for hours!几个小时以来一直在为这个问题而苦苦挣扎!

Websites like CSSDeck gives an embed code that consists of a pre and script tag.CSDeck这样的网站提供了一个由prescript标签组成的嵌入代码。 You can embed that code inside your blog and that stuff will show up.您可以将该代码嵌入到您的博客中,这些内容就会显示出来。 Here is an example of what I mean .这是我的意思的一个例子

But when I use that code in my Wordpress blog, it strips out the script tag.但是当我在我的 Wordpress 博客中使用该代码时,它会删除脚本标签。 How can I solve this problem?我怎么解决这个问题? Also can I solve it in a way so that I can add script tags from a whitelist domains that I defined somewhere?我还可以通过某种方式解决它,以便我可以从我在某处定义的白名单域中添加脚本标签吗?

Some Help would be really appreciated.一些帮助将不胜感激。

Cheers干杯

您可以使用tinymce设置valid_elementsvalid_children来定义将删除哪些tinymce和保留哪些tinymce。

If you wish to add javascript to your page you can try the Function Reference/wp enqueue script 如果您希望将javascript添加到页面中,可以尝试使用Function Reference / wp enqueue脚本。

Also, you should have a look at less filter plugin and Advanced Excerpt plugin 另外,您应该看看较少的过滤器插件Advanced Excerpt插件

You can paste (at least simple) scripts into the HTML version of the editor, but not into the Visual version. 您可以(至少简单)将脚本粘贴到编辑器的HTML版本中,但不能粘贴到Visual版本中。 The Visual version will encode the script, thus breaking it. Visual版本将对脚本进行编码,从而将其破坏。 I tested this a day or two ago on an almost up-to-date WordPress. 我在一两天前在几乎最新的WordPress上进行了测试。 If your script has dependencies like JQuery, it may not work though. 如果您的脚本具有JQuery之类的依赖项,则可能无法正常运行。 It depends on when the dependencies load and how the script is written. 这取决于何时加载依赖项以及如何编写脚本。

Add TinyMCE array:添加 TinyMCE 数组:

$editor_settings['tinymce '] = array(
        'extended_valid_elements' => 'script[src|async|defer|type|charset]',
         
      );

For wp_kses allow to add script:对于 wp_kses 允许添加脚本:

function allow_script_tags( $allowedposttags ){
  $allowedposttags['script'] = array(
      'src' => true,
      'height' => true,
      'width' => true,
      'charset' => true,
      'async' =>true
    );
  return $allowedposttags;
}

add_filter('wp_kses_allowed_html','allow_script_tags', 50);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM