Friday, March 20, 2009

WordPress Plugin: Change Admin Pagination

This plugin has been tested on WordPress Version 2.7.

Are you seeing enough posts in your WordPress admin screen? Or are you seeing too much?

Well, either way I've written a plugin to accomodate for the lack of choice for posts-per-page. First, please note this is unfortunately not a standard install for a WordPress plugin. Because WordPress hardcoded a static value (15) for posts-per-page, I needed to modify some of WordPress' core-code. So, continue at your own risk, copy/paste with caution, make backups often!

We will be editing the following files: wp-admin/edit.php and wp-admin/includes/post.php

Let's start with wp-admin/includes/post.php. Look around line 806, you should see this line: wp("post_type=post&what_to_show=posts$post_status_q&posts_per_page=15&order=$order&orderby=$orderby"); Aha! This is where they hardcode the posts_per_page to 15.

We're going to add a global variable line and replace the 15 with that global variable.

global $ppp;
wp("post_type=post&what_to_show=posts$post_status_q&posts_per_page=$ppp&order=$order&orderby=$orderby");

Now, let's go and edit the wp-admin/edit.php page. Look around line 250, you should see this code block:

__( 'Displaying %s–%s of %s' ) . '%s',
number_format_i18n( ( $_GET['paged'] - 1 ) * $wp_query->query_vars['posts_per_page'] + 1 ),
number_format_i18n( min( $_GET['paged'] * $wp_query->query_vars['posts_per_page'], $wp_query->found_posts ) ),
number_format_i18n( $wp_query->found_posts ),
$page_links
); echo $page_links_text; ?><\/div>
<\ ?php } ?>


We're going to add the following to make it look like this:
); echo $page_links_text;
if (function_exists(writePostsPerPage)) {
echo ' Posts per page';
echo writePostsPerPage(15);
echo writePostsPerPage(25);
echo writePostsPerPage(35);
}
?>
<\/div>
<\ ?php } ?>


That's all of the core code you'll be editing. Nothing too major! Now, all you need to do is add this following plugin code to a file and activate the plugin. Now, you should see a "Posts per page" option in your Edit posts screen.



/*
Plugin Name: Change Admin Pagination
Description: Change the pagination values in the Admin section for Posts and Pages
Version: 1.0
Plugin URI:
Author: Adrian J. Cruz
Author URI: http://drincruz.blogspot.com/
License:
Copyright 2009 Adrian J. Cruz (email : drincruz at gmail.com)

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

Changelog:
2009-03-20: didn't want as many core code changes, so moving as much as i can
to this plugin.

*/

global $ppp;
$ppp = $_GET['posts_per_page'];

/**
* This changes the posts_per_page option.
*/
function updatePostsPerPage($ppp) {
if (!$ppp)
return;
$old_ppp = get_option('posts_per_page');
if ($ppp == $old_ppp)
return;
else
update_option('posts_per_page', $ppp);
}

/**
* This will return the proper URL for the posts_per_page links.
*/
function writePostsPerPage ($n) {
if (!$n) { return; }
$ppp_url = "<\a href=\"?paged=" . $_GET['paged'] . "&posts_per_page=" . $n . "\">$n<\/a>";
return $ppp_url;
}

/**
* We want to update_option before the admin page is loaded so the page
* will take the changes immediately.
*/
add_filter('admin_head', updatePostsPerPage($ppp));


*Note: I know blogger doesn't handle source code very well within its 'code' tags, sorry about that! I think I'll try and find a free fileshare where I can keep source code and you can just download. I'll keep you guys posted.

Cheers!

No comments: