WordPress.org

Make WordPress Core

Opened 6 months ago

Last modified 6 months ago

#51715 new defect (bug)

get_permalink: stdClass treated as WP_Post causes bad behavour

Reported by: samiker Owned by:
Milestone: Awaiting Review Priority: normal
Severity: major Version:
Component: Permalinks Keywords: has-patch
Focuses: Cc:

Description

Normally get_permalink() expects post as int|WP_Post
But actually, we can call get_permalink() with a custom stdClass who has needed properties to work (retrieve posts with custom query), but the hooks used in the function get_permalink()

  • post_link_category
  • pre_post_link
  • post_link_category
  • post_link

expects WP_Post object as parameter not a stdClass

example:

<?php
                /**
         * @param string $permalink
         * @param WP_Post $post
         * @param bool $leavename
         */
        function custom_link( $permalink, $post, $leavename = false ) {
                my_first_function( $post );

                my_second_function( $post );
        }

        /**
         * @param WP_Post $post
         */
        function my_first_function( $post ) {
                if ( $post instanceof WP_Post ) {
                //FALSE
                }
        }

        /**
         * @param WP_Post $post
         */
        function my_second_function( WP_Post $post ) {
                //FATAL ERROR
        }


        add_filter( 'post_link', 'custom_link', 10, 3 );

        /**
         * @var stdClass $my_custom_post
         */
        get_permalink( $my_custom_post );

Change History (2)

This ticket was mentioned in PR #689 on WordPress/wordpress-develop by samikerb.


6 months ago

  • Keywords has-patch added

get_post if the passed post is not a WP_Post instance

Trac ticket: https://core.trac.wordpress.org/ticket/51715

#2 @prbot
6 months ago

github-actions[bot] commented on PR #689:

Hi @samikerb! 👋

Thank you for your contribution to WordPress! 💖

It looks like this is your first pull request, so here are a few things to be aware of that may help you out.

No one monitors this repository for new pull requests. Pull requests must be attached to a Trac ticket to be considered for inclusion in WordPress Core. To attach a pull request to a Trac ticket, please include the ticket's full URL in your pull request description.

Pull requests are never merged on GitHub. The WordPress codebase continues to be managed through the SVN repository that this one mirrors. But please feel free to use pull requests to work on any contribution you are making.

More information about how GitHub pull requests can be used to contribute to WordPress can be found in this blog post.

Including tests in your pull request is one way to help your patch be considered faster. To learn about WordPress' test suites, visit the Automated Testing page in the handbook.

If you have not had a chance, please review the Contribute with Code page in the WordPress Core Handbook.

The Developer Hub also documents the various coding standards that are followed:

Please remember that the WordPress project is largely maintained by volunteers

Thank you,
The WordPress Project

Note: See TracTickets for help on using tickets.