Setting the head of a post using a delimiter in your doc

I’ve gotten a lot of requests from people using our Docs to WordPress plugin on how to set a headline that’s different from the title of your Doc, such as we do at the BDN using a pipe.

This isn’t a standard feature of the plugin, but the plugin does include a few filters to modify how posts are formatted. One of these filters is pre_docs_to_wp_insert, and it can be leveraged as such:

<?php
/*
Plugin Name: Extend Docs to WP like so
*/

add_filter( 'pre_docs_to_wp_insert', 'bdn_split_post' );
function bdn_split_post( $post_array = array() ) {

	$exploded_fields = explode( '|', $post_array[ 'post_content' ] );
	
	//Sometimes people forget a pipe, and we don't want to put the entire post in the headline
	if( is_array( $exploded_fields ) && count( $exploded_fields ) >= 2 ) {

		//Save the old title in case you want to do something with it
		$old_title = $post_array[ 'post_title' ];

		//Set the title to the first occurance.
		$post_array[ 'post_title' ] = strip_tags( $exploded_fields[ 0 ] );
		
		//Unset the title
		unset( $exploded_fields[ 0 ] );
		
		//Now restore the post content and save it
		$post_array[ 'post_content' ] = implode( '|', $exploded_fields );
		
	}

	return $post_array;

}

I haven’t tested it but the above code should do the trick.

Coming soon: Details on moving stories from WordPress to InDesign!

This entry was posted in Plugins. Bookmark the permalink.

11 Responses to Setting the head of a post using a delimiter in your doc

  1. Tested it; works fine — added a semicolon to that last return $post_array; line — in WP 3.2.1. Many thanks to you for making all of this open-source and available.

    • William P. Davis says:

      Thanks Roger, you’re right. It’s always the small things…

      • Yeah. I have wreaked much havoc with a single misplaced bracket. But seriously, thank you for doing this — meaning making putting it out there. Every time you post an update or improvement, I think it shaves a man-hour or two off the web workload at our little rural weekly paper.

        And when you get around to the WordPress-to-InDesign thing, I plan to use it to help justify a long-overdue print production overhaul (and a switch from Quark, though maybe you should keep that part of my evil plan quiet).

        When you figure out which award you would like to win for all this, please post a link to the nomination form.

  2. Scott says:

    Would love to see your WordPress to InDesign post as mentioned at the end — as would others!.

  3. Steve Barton says:

    Could you tell me how much a site like this would cost to set up? Can I do it myself? I like what you did here – very leading edge!

  4. Jeffrey Harrington says:

    I feel like such a total idiot right now. I’m not sure where I should be editing or if this is a new script. I’d like to use the pipe to establish the article title but I’m totally lost.

    Could someone expand on this?

  5. Garret Ruh says:

    Similar to Jeffrey, I’m a little lost as to whether this is a new script or part of the plugin.php itself. How and where do I insert it if it’s part of plugin.php?

    • William P. Davis says:

      Garret,
      You can put this code (minus the plugin name declaration) in your functions.php or you can add it as a new file to the docs-to-wordpress directory and it will show up as a separate plugin that you can then activate.

      • Billy Suratt says:

        I put the code into a file which I named extend-delimiter.php and placed it in the Docs to WP directory. It showed up on the plugin menu like you said and worked when I activated it, but with two problems:

        1) The pipe at the end of my test story imported into the post body. I placed one pipe at the end of the title and one pipe at the end of the story like you showed in your examples.

        2) WP’s plugin manager says there’s an update available for the delimiter plugin, which, of course, isn’t right.

        I like the plugin option a lot better than adding the code to functions.php (less theme customization to remember tracking and backing up), so any help getting this to work would be appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>