Make WordPress Core

Opened 9 years ago

Last modified 5 years ago

#29972 new enhancement

Split pagination calculations and HTML in paginate_links

Reported by: dylanj's profile dylanj Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.0
Component: General Keywords: has-patch
Focuses: template Cc:

Description

The current implementation of paginate_links has a number of issues. It has been improved significantly in the latest version, however there is still room for more improvements.

Currently, the function forces you to use the HTML as it has been returned. If you want to add a class onto something, there is no easy way to do it - you're stuck with the defaults as they are hard-coded.

The first step towards fixing this is to split the pagination calculations from the HTML. Sort out what's going to go where, and then go and build the HTML and return it.

My reasoning is below, take a look and see what you think. I'm not necessarily advocating my particular fix, but I believe at the very least it needs some discussion.

This change will make it easier to create features such as custom classes for elements, and will make the code easier to read and maintain.

Also this is my first time creating a ticket on WordPress Trac, so go easy :)

Return raw array data

If you want to write your own completely custom HTML, you currently have to either copy+paste the function and modify it to suit your needs, or write a whole new one from scratch. The function should be able to return an array containing these values for each pagination item:

  • type (next/prev/dots/page)
  • link (where the item links to)
  • text (the text to be displayed within the link)
  • active (so you can apply your own active class)

This allows for more flexible use of the function within the template.

Customize the default output

Sometimes, you might just want to add an extra class to an item or two. Now, you could add these features to the current code, but the section for the active element is split from the normal elements which means you'd have to duplicate code to do it.

Change the currently selected page element

I've added a new arg 'link_current' so you can change the default element of the selected page to an A tag instead of the SPAN. I needed this for our specific design, but thought other people might find it useful as well.

Attachments (4)

29972.diff (4.8 KB) - added by dylanj 9 years ago.
paging-new.php (5.4 KB) - added by dylanj 9 years ago.
Full proposed version of paginate_links
29972.2.diff (4.3 KB) - added by dylanj 9 years ago.
Updated diff
29972.3.diff (5.7 KB) - added by dylanj 9 years ago.
Latest diff

Download all attachments as: .zip

Change History (7)

@dylanj
9 years ago

@dylanj
9 years ago

Full proposed version of paginate_links

#1 @DrewAPicture
9 years ago

  • Keywords has-patch added

Hi @dylanj, thanks for the patch and welcome to Trac! Unfortunately, it looks like your patch might need to be regenerated from the WordPress root directory. Also, attaching the php file is unnecessary in the future, the patch file should be enough :-)

@dylanj
9 years ago

Updated diff

@dylanj
9 years ago

Latest diff

#2 @SergeyBiryukov
9 years ago

  • Focuses template added

#3 @th23
9 years ago

Interesting suggestion, makes a lot of sense to me - came just now across some styling difficulties due to the hard coded (and thus limited) HTML classes...

Note: See TracTickets for help on using tickets.