WordPress wp_editor 短代码问题

3

我使用了wp_editor创建了一个前端编辑器,并且它的功能正常。

在测试中,我注意到它会去除已插入内容中的任何短代码。我调查了这个问题,并发现是'ob_'(输出缓冲)导致它们被删除。如果我删除这个输出缓冲,则短代码会正常显示,但这将破坏我为编辑器创建的功能。

我应该如何保留我正在使用的代码,但修改它以确保所有短代码都会被显示?非常感谢您的任何帮助和想法。

if(!is_feed() && current_user_can( 'manage_network' ) ) :

function ps_add_post_editor($content) {
global $post;
$settings = array(
    'wpautop' => true,
    'media_buttons' => false
);
    $content .= '<div id="content-edit-area" style="display:none;"><form action="" id="page-content-editor-panel" method="post"><span id="ajax_my_post_edit_nonce" class="hidden">' . wp_create_nonce( 'ajax_my_post_edit_nonce' ) . '</span>' . ps_get_wp_editor($post->post_content, 'textarea-' . $post->ID , $settings) . '<input type="submit" id="feesavebtn" value="Save" /></form><a href="#" id="cancelbtn">Cancel</a></div><br><br><div id="loadingmessage"><img src="'.get_template_directory_uri().'/images/loading.gif" /> saving...</div>

    <style type="text/css">
        #textarea-'.$post->ID.'_ifr, #textarea-'.$post->ID.' { min-height:700px !important; }
    </style>

    <script type="text/javascript">
    jQuery(\'#page-content-editor-panel\').submit(function(){       
    var pageid = '.$post->ID.'; 
    var content;
    var editor = tinyMCE.get(\'textarea-'.$post->ID.'\');
    if (editor) {
        content = editor.getContent();
    } else {
        content = jQuery(\'#textarea-'.$post->ID.'\').val();
    }       
    jQuery(\'#content-edit-area\').hide();  
    jQuery(\'#loadingmessage\').show(); 
    jQuery.post(
       ajaxurl, 
       {
          \'action\':\'add_foobar\',
          \'nonce\':jQuery(\'#ajax_my_post_edit_nonce\').text(),
          \'post_id\':pageid,
          \'post_data\':content
       }, 
       function(response){            
          window.location.reload(true); 
       }
    ); 
    return false;        
    });
    </script>';

return $content;
}

function ps_get_wp_editor($content,$textarea,$settings) {
ob_start();
wp_editor($content, $textarea, $settings);
$edior_html_code = ob_get_contents();
ob_end_clean();
return $edior_html_code;
} 
add_filter('the_content', 'ps_add_post_editor');

add_action('wp_ajax_add_foobar', 'prefix_ajax_add_foobar');
add_action('wp_ajax_nopriv_add_foobar', 'prefix_ajax_add_foobar');

function prefix_ajax_add_foobar() {
if( wp_verify_nonce( $_REQUEST['nonce'], 'ajax_my_post_edit_nonce' ) )           {      
    $myfee_post = array();
    $myfee_post['ID'] = $_POST['post_id'];
    $myfee_post['post_content'] = $_POST['post_data'];
    wp_update_post( $myfee_post );      
    die("This page has now been updated."); 
} else {        
    die("Unable to process your request at this time.");
}
}

endif;

我不知道你是否已经遇到过这个问题,但它可能会有一些模糊的用处 https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress/issues/129。我在使用Ajax和`wp_editor`函数时遇到了很多问题,而且这些问题一直没有得到解决,相关信息也非常稀少。 - Joe Buckle
我使用 wp_editor() 而不是 ps_get_wp_editor() 进行了代码测试(没有 ob_* 的东西),它可以正常工作,短代码没有被剥离。你尝试过禁用所有其他插件并使用 TwentyTwelve 主题吗? - brasofilo
1个回答

1
我必须承认,我不完全确定你的目标是什么,但这对我来说似乎有效:
remove_filter('the_content', 'do_shortcode', 11);

function ps_get_wp_editor($content,$textarea,$settings = array()) {
   ob_start();
   wp_editor($content, $textarea, $settings);
   $editor_html_code = ob_get_contents();
   ob_end_clean();
   return $editor_html_code;
}

function ps_add_post_editor($content) {
    global $post;

    $post_content = $post->post_content;
    $content = ps_get_wp_editor($post_content, 'ps-editor-' . $post->ID );
    return $content;
}
add_filter('the_content', 'ps_add_post_editor');

function ps_eat_pants() {
    return "eat pants has happened";
}
add_shortcode('eat_pants', 'ps_eat_pants');

事实上,如果您想要能够像在后端编辑器中一样编辑短代码,则需要从“the_content”中删除“do_shortcode”过滤器,以便在前端显示时不运行。
我必须承认,我不知道您发布的代码实际上是如何工作的,我假设它可能被错误地键入或上下文不正确。=)
我的意思是,在任何时候,您都没有实际提供所有必要参数给您的函数...
如果您能更详细地解释您正在做的事情,那可能会有所帮助,但我希望这可以帮到您。
[编辑:哦,那个“eat_pants”只是一个测试短代码...]

好的,谢谢回复 - 移除短代码过滤器确实解决了我的问题,但是在保存或取消编辑时,短代码被留作代码而不是运行。 (我已经添加了我的完整代码)。 - ss888
@ss888 哦,好吧,我认为你需要找到一些条件,在这个条件下你可以移除和重新添加过滤器。当你显示帖子时,你必须以某种方式重新添加过滤器,并且只有在添加编辑器时才能移除它...嗯... 这可能有点棘手。我在想,你是否可以在某种情况下仅在添加编辑器时从 the_content 过滤器中移除 do_shortcode,但是目前我有点困惑。这很棘手,因为“the_content”将始终在前端进行过滤,而这正是您想要显示内联编辑器的地方... - ZroonStack
啊啊啊啊啊啊啊啊!谢谢提供信息 - 似乎找不到任何方法使它工作!!! - ss888

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接