【问题标题】:Embed and render Twitter/Instagram post inside TinyMCE using jQuery/PHP使用 jQuery/PHP 在 TinyMCE 中嵌入和渲染 Twitter/Instagram 帖子
【发布时间】:2018-07-01 03:44:27
【问题描述】:

我正在使用 PHP/CodeIgniter 创建一个网站。我在视图中有一个 TinyMCE 文本编辑器,想将 Twitter 或 Instagram URL 粘贴到编辑器中,并使用该 URL,将其嵌入并在文本编辑器中呈现。例如:

Instagram 网址:https://www.instagram.com/p/BeA2XEHjQJ5/?hl=en&taken-by=iamsrk

推特网址:https://twitter.com/iamsrk/status/947915219648487424

在 WordPress 中,如果我只是将这些 URL 粘贴到文本编辑器中,它将转换并呈现​​它。我想要完全相同的东西。

【问题讨论】:

    标签: php tinymce embed


    【解决方案1】:

    TinyMCE 有一个高级插件,可让您在内容中轻松嵌入 Twitter 和 Instagram 帖子。

    https://www.tinymce.com/docs/plugins/mediaembed/

    【讨论】:

    • 感谢您的回答,我投票支持您,但说真的,我不能只为用户/管理员支付那么多钱,还有其他方法吗?
    【解决方案2】:

    这是我使用的解决方案

    为 twitter 定制插件

    $(document).ready(function()
    {
    
        tinymce.PluginManager.add('twitter_url', function(editor, url) {
            var icon_url='img/social/twitter.png';
    
            editor.on('init', function (args) {
                editor_id = args.target.id;
    
            });
            editor.addButton('twitter_url',
                {
                    text:false,
                    icon: true,
                    image:icon_url,
    
                    onclick: function () {
    
                        editor.windowManager.open({
                            title: 'Twitter Embed',
    
                            body: [
                                {   type: 'textbox',
                                    size: 40,
                                    height: '100px',
                                    name: 'twitter',
                                    label: 'twitter'
                                }
                            ],
                            onsubmit: function(e) {
    
                                var tweetEmbedCode = e.data.twitter;
    
                                $.ajax({
                                    url: "https://publish.twitter.com/oembed?url="+tweetEmbedCode,
                                    dataType: "jsonp",
                                    async: false,
                                    success: function(data){
                                        // $("#embedCode").val(data.html);
                                        // $("#preview").html(data.html)
                                        tinyMCE.activeEditor.insertContent(
                                            '<div class="div_border" contenteditable="false">' +
                                                '<img class="twitter-embed-image" src="'+icon_url+'" alt="image" />'
                                                +data.html+
                                            '</div>');
    
                                    },
                                    error: function (jqXHR, exception) {
                                        var msg = '';
                                        if (jqXHR.status === 0) {
                                            msg = 'Not connect.\n Verify Network.';
                                        } else if (jqXHR.status == 404) {
                                            msg = 'Requested page not found. [404]';
                                        } else if (jqXHR.status == 500) {
                                            msg = 'Internal Server Error [500].';
                                        } else if (exception === 'parsererror') {
                                            msg = 'Requested JSON parse failed.';
                                        } else if (exception === 'timeout') {
                                            msg = 'Time out error.';
                                        } else if (exception === 'abort') {
                                            msg = 'Ajax request aborted.';
                                        } else {
                                            msg = 'Uncaught Error.\n' + jqXHR.responseText;
                                        }
                                        alert(msg);
                                    },
                                });
                                setTimeout(function() {
                                    iframe.contentWindow.twttr.widgets.load();
    
                                }, 1000)
                            }
                        });
                    }
                });
        });
    
    
        tinymce.init({
            selector:"#content",
            height: 300,
            theme: 'modern',
            menubar:true,
            plugins:'preview code twitter_url',
            toolbar: 'code preview twitter_url',
    
            valid_elements : '+*[*]',
    
            extended_valid_elements: "+iframe[width|height|name|align|class|frameborder|allowfullscreen|allow|src|*]," +
            "script[language|type|async|src|charset]" +
            "img[*]" +
            "embed[width|height|name|flashvars|src|bgcolor|align|play|loop|quality|allowscriptaccess|type|pluginspage]" +
            "blockquote[dir|style|cite|class|id|lang|onclick|ondblclick"
            +"|onkeydown|onkeypress|onkeyup|onmousedown|onmousemove|onmouseout"
            +"|onmouseover|onmouseup|title]",
    
            content_css: ['css/main.css?' + new Date().getTime(),
                '//fonts.googleapis.com/css?family=Lato:300,300i,400,400i',
                '//www.tinymce.com/css/codepen.min.css'
            ],
            setup: function (editor) {
                console.log(editor);
                editor.on('init', function (args) {
                    editor_id = args.target.id;
                });
    
            },
        })
    })
    

    here is a youtube link of it

    【讨论】:

    • 对代码做了一些自己的修改,但这段代码效果很好。
    • 我在编辑器中收到错误消息。 Failed to load plugin: twitter_url from url https://cdnjs.cloudflare.com/ajax/libs/tinymce/4.9.6/plugins/twitter_url/plugin.min.js
    猜你喜欢
    • 1970-01-01
    • 2020-01-17
    • 1970-01-01
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多