0

I have created a front-end editor using wp_editor that functions as it should.

Upon testing I have noticed that it is stripping out any shortcodes that have been inserted in the content. I have looked into this issue and found that it's the 'ob_' (output buffering) that is removing them. If I remove this output buffering then the shortcodes display fine, but it breaks the functionality I have created for the editor.

How would I keep the code I am using below, but amend it to make sure that all shortcodes are being displayed? Any help/ideas greatly appreciated, S.

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;
4
  • Don't you think that "how do I keep the code I am using" maybe is the wrong approach? X-Y Problem?
    – kaiser
    Sep 2, 2013 at 13:45
  • @kaiser Good point.... although I have spent a long time getting the above code working, only to be undone by this shortcode issue...
    – sta777
    Sep 2, 2013 at 15:52
  • @sta777 I have added an answer. Have you tried it? Sep 9, 2013 at 16:37
  • @sta777 Have you found any answer on this question. Sep 10, 2013 at 13:18

1 Answer 1

0

You can display shortcodes in WP editor by removing do_shortcode filter from the_content filter as shown in the following code but beware that it will also display shortcodes tags when you use the_content() function in the theme anywhere else in that case before the_content() function add do_shortcode filter again like this add_filter('the_content', 'do_shortcode',15 );

function ps_get_wp_editor($content,$textarea,$settings) {
    remove_filter('the_content', 'do_shortcode',11 );
    ob_start();
    wp_editor($content, $textarea, $settings);
    $edior_html_code = ob_get_contents();
    ob_end_clean();
    return $edior_html_code;
} 
7
  • 1
    Why don't add filter again just a line before return? (Tip: ob_get_contents immediately followed by ob_end_clean can be changed in ob_get_clean)
    – gmazzap
    Sep 6, 2013 at 7:46
  • I tried adding add filter before return but it didn't work. I also tried changing priority of filters but no it's not working. It works outside the_content filter function. Sep 6, 2013 at 7:58
  • Is this function hooked into the 'the_content' filter?
    – gmazzap
    Sep 6, 2013 at 8:09
  • I checked WP core for it and found it is hooked in wp-includes/shortcodes.php like this add_filter('the_content', 'do_shortcode', 11); Sep 6, 2013 at 8:12
  • I'm talking about ps_get_wp_editor. Where is this function hooked?
    – gmazzap
    Sep 6, 2013 at 8:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.