Custom Post Type Custom Template ?

Been researching how to create custom template for custom post type in a plugin. This is a note about what i found:

1. can add filter for ‘template_include’ hook, and add a meta box for switching your own custom template (create your own dropdown menu)

For example:

add_filter('template_include', 'template_include_function', 10, 1);

function template_include_function($template) {
	global $post;
	if ($post->post_type == 'YOUR_CUSTOM_POST_TYPE') {
                $postmeta = get_post_meta(...);  
		...
		$template =  plugin_dir_path(__FILE__) . YOUR_TEMPLATE_FILE;
                ...
	}
	return $template;
}

function template_box() {
	add_meta_box(
		'template_box',
		__('...','...'),
		'template_content',
		'YOUR TYPE',
		'side',
		'core'
	);	
	
}

function template_content($post) {
	$template = get_post_meta(.. 
?>
<label ...>_e(...);</label>
<select ...">
<option ...>Option 1</option>
<option ...>Option 2</option>
</select>

<?php
}

add_action('add_meta_boxes', 'template_box');
&#91;/php&#93;

then add your own meta box to display this template list.  Use action hook 'save_post' to update post meta data for your template selection

2.  I have tried to add Page Attributes capability, but it does not show the custom template in the theme.  I looked at the code for function page_attributes_meta_box, and found the a condition that will check if post type is 'page', then will show the template.  Therefore, it is not gonna call the page_template_dropdown(), and no page template shows up in the admin page.

3. a filter 'page_attributes_dropdown_pages_args' is apply if the custom post type supports hierarchical, so one thing can try is add filter for   'page_attributes_dropdown_pages_args' and add drop down menu that point to your template directory.  In your filter function, you can try

&#91;php&#93;
function YOURFUNCTION($dropdown_args, $post) {
if ($post->post_type == 'YOUR CUSTOM POST TYPE') {
...
<select ...>
<?php page_template_dropdown(false); ?>
</select>
...
}

return $dropdown_args; // just return the same dropdown args. don't need to modify
}

This basically will create a drop down menu for all template in your Theme directory (not your plugin). Note that this drop down menu will appear above the Parent drop down menu(for hierarchical setting). This is useful if you have custom post type in your theme and template file within your theme directory.

4. So instead of calling page_template_dropdown(), it is better to add your own template here if you do not want to add extra meta box.

function YOURFUNCTION($dropdown_args, $post) {
if ($post->post_type == 'YOUR CUSTOM POST TYPE') {
...
<select ...>
<option ...> Option 1 </option>
<option ...> Option 2 </option>
</select>
...
}

return $dropdown_args; // just return the same dropdown args. don't need to modify
}

add_filter('page_attributes_dropdown_pages_args', YOURFUNCTION, 10, 2);

Ok, it is not done yet. You add options here, then you also need to add action to ‘save_post’ hook to update the the post meta. the post meta will be used in the filter hook ‘template_include’. (look at step 1)

5. Right now I am studying WP_Theme class get_page_templates() to see how it parses template in the theme directory.

Note: I think create your own meta box for templates for your own custom post type is more appropriated…

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Ping.fm Post to Reddit Post to StumbleUpon

Read More

jQuery and WP_Ajax_Response

I tried to use WP_Ajax_Response class to respond Ajax request, but then some data wrap with [CDATA], and found that I just can’t use jQuery .text() to grab the data inside that… here is how i resolved this problem

<?xml ....?>
<wp_ajax>
...
<supplemental>
<url><!&#91;CDATA&#91;http://.....&#93;&#93;></url>
</supplemental>
</wp_ajax>
var xml = $.parseXML(response),
    imgURL = $(xml).find('supplemental url').text()

first use jQuery function to parse the response, then use jQuery .text() method to retrieve the data within CDATA.

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Ping.fm Post to Reddit Post to StumbleUpon

Read More

Using Plupload

My note on how to use plupload:

1. Create instance with configuration

2. Bind ‘Init’ Event for anything that want to do be done after plupload initialed.

3. call init()

4. Bind ‘FilesAdded’ Event to start action after select files for uploading (Example: add file to your upload list, or start uploading right away by call start())

5. Can call start() to start uploading files (Optional(I guess):call refresh() if need Reposition Flash/Silverlight)

6. Bind ‘UploadProgress’ Event when uploading (example: add some animation showing progress bar)

7. Bind ‘Error’ for handling failing uploading

8. Bind ‘FileUploaded’ for handling after the files have been uploaded. (example: show the uploaded picture url on the screen)

Just my note for steps how to use plupload (so I can use it on WordPress for uploading media files)

Post to Twitter Post to Plurk Post to Yahoo Buzz Post to Delicious Post to Digg Post to Facebook Post to MySpace Post to Ping.fm Post to Reddit Post to StumbleUpon

Read More