Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm in the process of migrating my blog from Drupal to Wordpress. I have made liberal use of Markdown in the original posts (and Geshi syntax highlighting).

In see that the WP-Markdown plugin converts to (and from HTML) on save (and edit), so I now have a bunch of posts which appear with the Markdown 'on show'.

Is there an API call which I can use to effectively do an 'edit and save' operation on each post to force the Markdown on Save plugin to operate on them all?

Or alternatively, a different Markdown plugin I should try?

share|improve this question

1 Answer 1

up vote 6 down vote accepted
+100

Note: The following is largely untested (It worked after testing on one post).

As you've pointed out when you edit a post, the plug-in takes the content in the database (HTML) and converts that to MarkDown for editing. Since in your case the content is actually not in HTML, but already in MarkDown, we want to stop this happening. Then by updating the post, the plug-in should convert the MarkDown to HTML.

This should be fairly easy to do since this parsing is just hooked onto some filters, you simply remove the appropriate callbacks to prevent the plug-in from doing anything. (Then again, given the class structure of the plug-in, maybe its not quite so easy). Given that, you may just wish to manually edit the plug-in files to remove the filters.

The filters in question are added here.

add_filter( 'edit_post_content', array( $this, 'wpautop' ), 10, 2 );
add_filter( 'edit_post_content', array( $this, 'edit_post_content' ), 10, 2 );

Removing those (manually or otherwise) and then updating each of the posts should work. But that could take a while, so lets go for an automated solution...

   function wpse65948_correct_markdown(){

        //Only run script if ?markdown=correct is set
        if( empty($_GET['markdown']) || 'correct' !== $_GET['markdown'] )
             return;

        if( !current_user_can('publish_posts') )
             return;

        //Do a query to get all posts that are expected to contain markdown
        //Typically will be decided by post type. Set post_status to 'any'.
        $markdowns = get_posts( array(
             'fields'=>'ids',
             'post_type'=>'post',
             'post_status'=>'any',
         ) );

         //If no posts found abort.
         if( !$markdowns )
            return;

         /** !Important 
          *  At this point the filters should be removed. Either remove them now
          *  or ensure they have been manually removed prior to triggering this script.
          */
         foreach ($markdowns as $pid ){

             // Get the content for editing
             $markdown = get_post_to_edit( $pid );

             //$markdown->post_content should contain the correct MarkDown
             $update_post = array();
             $update_post ['ID'] = $pid;
             $update_post ['post_content'] = $markdown->post_content;

             //Update the post into the database
             wp_update_post( $update_post);
          }
   }

   add_action('admin_init','wpse65948_correct_markdown',20);

You should test this script with one of your posts first to check that it works before running for the rest.

The script can be added to the very bottom of the wp-markdown.php file or functions.php. Remove again after use. To trigger the script just append ?markdown=correct to an admin url.

If you manually remove the filters before running this script, make sure you replace them again.

share|improve this answer
    
In the end, just commenting out the filter lines was sufficient - going through the posts didn't take long and allowed me to fix some tagging errors in the migration –  Martin Thompson Oct 1 '12 at 19:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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