为Padlet.com注册嵌入式处理程序

时间:2020-02-28 作者:cogdog

I\'m trying and failing to register an embed code for content from padlet.com (strangely it\'s supported in WP.com but not WP.org). A URL like

 https://padlet.com/cogdogblog/aos9fosbbwk4

should embed like

<div class="padlet-embed" style="border:1px solid rgba(0,0,0,0.1);border-radius:2px;box-sizing:border-box;overflow:hidden;position:relative;width:100%;background:#F4F4F4"><p style="padding:0;margin:0"><iframe src="https://padlet.com/embed/aos9fosbbwk4" frameborder="0" allow="camera;microphone;geolocation" style="width:100%;height:608px;display:block;padding:0;margin:0"></iframe></p>

My attempt that fails so far

add_action( \'init\', function(){
  $regex_url = \'#https?://padlet\\.com/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$#i\';

  wp_embed_register_handler(
    \'padlet\',
    $regex_url,
    \'wp_embed_handler_padlet\',
    true
  );
});


function wp_embed_handler_padlet( $matches, $attr, $url, $rawattr )
{
  $embed = sprintf(
    \'<div class="padlet-embed" style="border:1px solid rgba(0,0,0,0.1);border-radius:2px;box-sizing:border-box;overflow:hidden;position:relative;width:100%;background:#F4F4F4"><p style="padding:0;margin:0"><iframe src="https://padlet.com/embed/%1$s" frameborder="0" allow="camera;microphone;geolocation" style="width:100%;height:608px;display:block;padding:0;margin:0"></iframe></p>\',
    esc_attr($matches[2])
    );

  return apply_filters( \'embed_padlet\', $embed, $matches, $attr, $url, $rawattr );
}

I am sure it is my regex clumsiness. It would sure help newbies like me if the Codex docs for this function had a better explained (and actually working) example.

And what form of regex is used in wp_embed_register_handler? None of the online testers I use have this format and requires escaping of / - looking for a place to test patterns that WordPress uses.

UPDATE After failing to find Padlet listed as an oembed provider https://oembed.com/ and not finding any documentation on their developer site https://padlet.readme.io/ I decided to wild guess at the URL for an oembed request from the format

 https://padlet.com/oembed?url=https%3A//padlet.com/XXXXXX/yyyyyyyyyy&format=json

or a real example

https://padlet.com/oembed?url=https%3A//padlet.com/cogdogblog/aos9fosbbwk4&format=json

And I get a response (this makes sense as this is how WordPress.com is likely supporting it). So now I get full support via the much simpler:

wp_oembed_add_provider( "https://padlet.com/*", "https://padlet.com/oembed/", false );
2 个回复
SO网友:Tom J Nowell

您是否考虑过使用更简单的正则表达式,例如:

\'#^https?://padlet\\.com/(\\?.+)?$#\'
然后用explode 获取视频ID?

SO网友:Dave Romsey

我认为问题在于<iframe> 由于WP对HTML的清理,导致嵌入阻塞。一旦我删除了内联样式并平衡了HTML标记,我就能够让嵌入工作:

    add_action( \'init\', function(){
        $regex_url = \'#https?://padlet\\.com/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$#i\';

        wp_embed_register_handler(
                \'padlet\',
                $regex_url,
                \'wp_embed_handler_padlet\',
                true
        );
    });


    function wp_embed_handler_padlet( $matches, $attr, $url, $rawattr ) {
        $embed = sprintf(
            \'<div class="padlet-embed">
                <iframe src="//padlet.com/embed/%1$s" frameborder="0" allow="camera;microphone;geolocation"></iframe>
            </div>\',
            esc_attr($matches[2])
        );

        return apply_filters( \'embed_padlet\', $embed, $matches, $attr, $url, $rawattr );
    }

padlet embed example

可以通过目标设置嵌入样式.padlet-embed 而不是内联添加样式。

编辑:在块编辑器中测试,可在后端和前端工作,无需任何更改,但需要使用嵌入块。enter image description hereenter image description here

相关推荐

Use oEmbed for static html

我正在尝试添加jQuery插件(https://github.com/rmanivannan/speedometer-jquery-plugin) 到Wordpress页面(不是post),但是插件的行为不符合预期。我认为这可能与使用我的Wordpress主题生成页面时脚本引用、css引用和Javascript被破坏的方式有关。是否可以:-将包含控件的静态HTML页面上载到子域-使用oEmbed将静态HTML页面包括在Wordpress页面中i、 e.子域将是主域的oEmbed提供程序?