【问题标题】:Wordpress Pre Fill content in PostsWordpress 在帖子中预填充内容
【发布时间】:2015-03-14 07:41:55
【问题描述】:

我有一个管理插件,里面有一个文章列表,旁边有一个“添加到帖子”按钮。单击该按钮后,我想重定向到“/wp-admin/post-new.php”并预先填写表格。

我可以在网址中设置标题,例如wp-admin/post-new.php?post_type=post&post_title=My Titlle。但是,如何预填充内容?

我读过一些类似this 的文章,但不是我要找的。​​p>

另外,内容每次都会不同,所以我不想设置为默认。

我现在在做什么:

我的 jQuery 按钮点击:

function add_to_post(id) {

    var data = {
        'action'    : 'add_to_post',
        'id'    : id
    };

    $.ajax({
        type    : 'POST',
        url     : ajaxurl,
        data    : data
    })
    .done(function(){

        var title   = $(document.getElementById('title_'+id)).text(); 
        var link    = host+"/wp-admin/post-new.php?post_type=post&post_title="+title;
        window.open(link,"_blank");
    })
    ;
}

我的动作插件代码

add_action('wp_ajax_add_to_post','add_to_post_callback');

function add_to_post_callback() {

    add_filter( 'default_content', 'my_editor_content', 10 , 2 );

    wp_die();
}

function my_editor_content( $content ) {

    $content = "This is some custom content I'm adding to the post editor because I hate re-typing it.";

    return $content;
}

但是当我点击“添加到帖子”按钮时,内容仍然是空的 我将不胜感激。

阿曼。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    WordPress 有一个默认内容过滤器:

    add_filter( 'default_content', 'set_default_content', 10, 2 );
    function set_default_content( $content, $post ) {
        $content = ...your content...;
        return $content;
    }
    

    您可以只为内容使用一个变量,并随时更改它。

    【讨论】:

    • 嘿,bobdye,我试着按照你说的做 - 但内容仍然是空的。我对我的问题进行了编辑以添加我所做的事情。如果你能指出我的问题:) .. 谢谢
    • 我不知道您是否可以在 Ajax 处理程序中添加过滤器。通常插件内的过滤器被添加到主插件文件或“init”操作处理程序中。由于您从未对返回的 data 执行任何操作,因此您究竟想对 Ajax 调用做什么?
    【解决方案2】:

    这就是我最终做的:

    在 jQuery 中:

    function add_to_post(feed_id) {
    
        var title   = $(document.getElementById('title_'+feed_id)).text(); 
        var content = $(document.getElementById('summary_'+feed_id)).text(); 
        var link    = host+"/wp-admin/post-new.php";
    
        var data = {
            'post_title'    : title,
            'pre_content'   : content,
            'post_type'     : 'post'
        };
    
        $.extend({
            redirectPost: function(location, args) {
                var form = '';
                $.each( args, function( key, value ) {
                    form += '<input type="hidden" name="'+key+'" value="'+value+'">';
                });
                $('<form target = "_blank" action="'+location+'" method="POST">'+form+'</form>').appendTo('body').submit();
            }
        });
        // sending content as post because Get request has character limit of 2048. Just taking no chances.
        $.redirectPost(link,data);
    }
    

    在 PHP 中

    //To add content to your post.
    add_filter( 'default_content', 'my_editor_content', 10 , 2 );
    
    function my_editor_content( $content , $post ) {
    
        if(isset($_POST['pre_content'])) {
            $content = $_POST['pre_content'];
        }
    
        return $content;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      • 2017-12-04
      相关资源
      最近更新 更多