WordPress.org

Make WordPress Core

Opened 5 months ago

Closed 5 months ago

#53025 closed defect (bug) (invalid)

Change to get_permalink in 5.7 breaks existing code

Reported by: turbodb Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.7
Component: Permalinks Keywords:
Focuses: Cc:

Description

This is a follow-up to #5272.

I have the following code, which I’ve registered as a custom column on the admin pages, to show me the permalink (or what will be the permalink) for posts that are not yet published.

For example, if the post title is “My First Post” then the column will show

http://site/my-first-post/

as the permalink, even when the post is in draft state.

This worked just fine for many versions. I recently updated to 5.7. Now, this code is no longer working; it is simply returning a permalink of the form

http://site/?p=postID

I’ve check the code and all lines work as expected, except for

$url = get_permalink($my_post);
<?php
function atp_post_permalink_column() {
    add_filter('manage_posts_columns', 'atp_permalink_column_head');
    add_action('manage_posts_custom_column', 'atp_permalink_content', 9, 2);
}

function atp_permalink_column_head($default) {
    $default['permalink'] = 'Permalink';
    return $default;
}

function atp_permalink_content($column, $post_id) {
        if ($column == 'permalink') {
        
                $post = get_post( $post_id );
                $url = '';
                if (in_array($post->post_status, array('draft', 'pending', 'auto-draft', 'future'))) {
                        $my_post = clone $post;
                        $my_post->post_status = 'published';
                        $my_post->post_name = sanitize_title($my_post->post_name ? $my_post->post_name : $my_post->post_title, $my_post->ID);

                        $url = get_permalink($my_post);
                } else {
                        $url = get_permalink($post);
                }

                echo $url;        
        }
}

This appears to be due to change 5272.

This is my first ticket, apologies if I've got any of the fields set incorrectly. Please update as appropriate.

Change History (2)

#1 @turbodb
5 months ago

I've played around with this some more, and I believe the bug was actually on my side, but didn't manifest until 5.7.

Specifically, the line of code:

<?php
$my_post->post_status = 'published';

is incorrect. The status there should be 'publish' rather than the past tense.

In pre-5.7 incarnations of get_permalink(), the function may have checked for status <> 'draft' or that contained 'publish' and as such, my code worked. However, with the new more stringent checks, my code no longer passed the check for a pretty permalink.

I've updated my code, and verified that even with the fix for #5272, everything works as expected.

This ticket can be closed.

#2 @peterwilsoncc
5 months ago

  • Component changed from Security to Permalinks
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Hi and welcome to trac and thanks for following up.

You're correct, previous versions of WordPress checked hard-coded status names rather than using the properties of the status object. While working on #5272, I discovered this was causing problems in some circumstances.

If it's any consolation, I recently found a similar typo to published in WordPress's own unit tests. It's an easy mistake to make.

Again, thank you for following up and I will close this ticket off as requested.

Note: See TracTickets for help on using tickets.