[wp-hackers] patch that adds columns hooks for plugins in the Manage Posts page

Michel Valdrighi michelv at gmail.com
Fri Feb 11 00:17:28 GMT 2005


Hello,

Attached is a patch that adds much flexibility to plugin writers and
core WP stuff if we feel like taking advantage of the hooks it provides.
I intend to commit this patch to CVS tomorrow if it hasn't caused
problems to testers.

Basically, with this patch, the table that displays posts on edit.php
can have columns added or removed at will.

Two hooks are added:

1) manage_posts_columns, that gets fed a $posts_columns array consisting
of key=>value pairs.
The keys are internal names for columns, while the  values are the
columns' names. For example, among the default key=>value pairs is
'date'=>__('When'), this allows to display 'When' in any language while
keeping a single name for it internally.
Functions that use this hook are expected to add rows to $posts_columns
or delete some. For example: function modify_columns($columns)
{ $columns['mycol'] = 'Mine mine mine'; return $columns; } would add a
column with header "Mine mine mine" and internal name 'mycol'.

2) manage_posts_custom_column, that gets fed the current column's name,
and the post's $id.
This hook is only called when we hit a column name that is not one of
the defaults ('id', 'date', 'title', 'category', 'comments', 'author',
'control_view', 'control_edit', 'control_delete').
It is expected that functions using this action check for the column
name and only do their thing if the column name is  one they added with
the manage_posts_columns hook. On the other hand, one could think of
multiple plugins sharing the same column and then display their things
when they see that shared column name.


How this is possible: this patch makes each row of the table consist of
a loop. The loop goes through $posts_columns and displays the accurate
cell content according to a switch statement. The switch's default case
calls the hook manage_posts_custom_column.

Functions to use the hooks are rather simple, here's an example:

<?php
function example_add_column($posts_columns) {
	// adding a column
	$posts_columns['example_column'] = 'Our New Shiny Example Column Header';
	// deleting a column
	unset($posts_columns['author']);
	return $posts_columns;
}
function example_do_column($column_name) {
	// check that it's our column that we are asked to display
	if ($column_name != 'example_column') {
		return false;
	}
	echo 'Test output plus random number! ' . rand(10);
}
add_filter('manage_posts_columns',       'example_add_column');
add_action('manage_posts_custom_column', 'example_do_column');
?>

In this example, the first function adds a column 'example_column' and
deletes the column 'author'.

As a visual example, here's a screenshot of what I'm going to do with my
blog once I'm done writing my tags plugin: (actually once I finish
tagging my old posts :p)
http://zengun.org/stuff/images/columnshooksinaction.jpg

I can imagine plugins like MiniPosts, Bunny's Technorati Tags and the
like could benefit from this plugin too.


So, can anyone test this and/or provide ideas for changes or
improvements to its functionnality?

Thanks in advance, and have fun!

-- 
Michel Valdrighi
http://zengun.org/weblog/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: editphp_withcolumnshooks.patch
Type: text/x-patch
Size: 4539 bytes
Desc: not available
Url : http://comox.textdrive.com/pipermail/hackers/attachments/20050211/01a585a0/editphp_withcolumnshooks.bin


More information about the hackers mailing list