Skip to content

Users: Defer loading capability/role data for authordata lookups - #13825

Open
spacedmonkey wants to merge 6 commits into
WordPress:trunkfrom
spacedmonkey:fix/lazy-load-capablity
Open

spacedmonkey wants to merge 6 commits into
WordPress:trunkfrom
spacedmonkey:fix/lazy-load-capablity

Conversation

@spacedmonkey

@spacedmonkey spacedmonkey commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Summary

  • Adds get_authordata(), a lighter-weight alternative to get_userdata() for contexts that only need display data (name, avatar, bio, etc.) and never check capabilities or roles, such as author archives and the $authordata global set up in the Loop.
  • Capability and role data ($caps, $roles, $allcaps) on the returned WP_User is no longer queried eagerly; it's loaded transparently on first access via has_cap(), get_role_caps(), or any of the role/capability mutator methods, via a new private WP_User::$short_init flag.
  • Updates get_the_modified_author(), get_the_author_meta(), get_author_posts_url(), wp_list_authors(), WP_Query, WP::handle_404(), and get_permalink() to use get_authordata() instead of get_userdata() where only display data is needed.
  • Updates existing tests and adds new coverage for the lazy-loading behavior, including regression tests for recursion and serialization edge cases.

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

Test plan

  • phpunit tests in tests/phpunit/tests/user/getAuthordata.php, tests/phpunit/tests/query/generatePostdata.php, and tests/phpunit/tests/query/setupPostdata.php pass
  • Manual smoke test of author archives and the Loop's $authordata global

🤖 Generated with Claude Code

@github-actions

github-actions Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props spacedmonkey, westonruter.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Adds get_authordata(), a lighter-weight alternative to get_userdata() for
contexts (author archives, the Loop's $authordata global, permalinks) that
only need display data and never check capabilities or roles. Capability and
role data is now loaded lazily on WP_User via a new short_init flag, only
when has_cap(), get_role_caps(), or a role/capability mutator is called.

See #58001.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@jonnynews
jonnynews force-pushed the fix/lazy-load-capablity branch from a47fb2b to b74e3eb Compare September 29, 2026 22:11
@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@westonruter westonruter left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple drive-by suggestions to fix the PHPStan errors being raised.

Comment thread src/wp-includes/class-wp-query.php Outdated
$id = (int) $post->ID;

$authordata = get_userdata( $post->post_author );
$authordata = get_authordata( $post->post_author );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$authordata = get_authordata( $post->post_author );
$authordata = get_authordata( (int) $post->post_author );

Comment thread src/wp-includes/link-template.php Outdated
$author = '';
if ( str_contains( $permalink, '%author%' ) ) {
$authordata = get_userdata( $post->post_author );
$authordata = get_authordata( $post->post_author );

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$authordata = get_authordata( $post->post_author );
$authordata = get_authordata( (int) $post->post_author );

spacedmonkey and others added 3 commits October 9, 2026 16:43
Also casts post_author to int when calling get_authordata(), per review feedback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… data.

Extracts the capability loading logic shared by `for_site()` and the lazy
loader into a public `load_capabilities()` method, allowing callers to force
capability data to load immediately or to reload it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants