Make WordPress Core

Opened 18 months ago

Closed 18 months ago

Last modified 18 months ago

#22352 closed enhancement (invalid)

Update get_permalink() to allow for permalink structure argument

Reported by: doublesharp Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.5
Component: Permalinks Keywords: has-patch needs-testing dev-feedback
Focuses: Cc:


This patch allows for an optional custom permalink structure argument to the get_permalink() function, which would be used for the permalink generation in place of the structure saved in the "permalink_structure" option. Useful for showing what a permalink for a post/page would look like with a new structure, or for updating content when permalink structures change.

Attachments (1)

link-template.php.patch (978 bytes) - added by doublesharp 18 months ago.
optional permalink structure argument for get_permalink()

Download all attachments as: .zip

Change History (4)

doublesharp18 months ago

optional permalink structure argument for get_permalink()

comment:1 scribu18 months ago

Couldn't you use the 'pre_post_link' filter?

function my_change_permalink( $permalink, $post ) {
  return '/foo/bar/';

add_filter( 'pre_post_link', 'my_change_permalink', 10, 2 );
$link = get_permalink();
remove_filter( 'pre_post_link', 'my_change_permalink', 10, 2 );

comment:2 doublesharp18 months ago

  • Resolution set to invalid
  • Status changed from new to closed

Ack, you are 100% correct, that would be exactly the way to do it. Got a bit carried away dealing with the bug in ticket:22351 trying to create a permalink from the "old permalink_structure" which is (not correctly) being passed by the "permalink_structure_changed" action. In hindsight it's easy enough to keep this value in an instance variable and reference it from the hooked filter, so:

class MyClass {
    private $old_structure;
    public function get_old_structure(){
        return $this->old_structure;
    public function permalink_structure_changed($old_structure, $new_structure){
        $this->old_structure = $old_structure;
        add_filter( 'pre_post_link', array($this, 'get_old_structure') );
        $old_permalink = get_permalink($post_id, false);
        remove_filter( 'pre_post_link', array($this, 'get_old_structure') );
        // more code...
$myclass = new MyClass();
add_action( 'permalink_structure_changed', array( $myclass, 'permalink_structure_changed' ), 10, 2 );

comment:3 duck_18 months ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.