humanmade / humanmade/coding-standards

Do not allow the usage of magic properties on WP_Post objects

Open
#229 4 comments 2 reactions 0 assignees View on GitHub
phpcs
Dominant language
PHP
Stars
160
Forks
17
PR merge metrics
No merged PRs in 30d

Description

The `WP_Post` class offers access to various properties through magic getters:
- `$page_template`
- `$ancestors`
- `$post_category`
- `$tag_input`

In addition, post meta can be retrieved by using `$post->meta_key`.

We should not allow the usage of these coding approaches:
1. Magic properties are discouraged in the PHP community, because IDEs and other code quality tools cannot understand them.
2. Since there are reserved keywords, using `$post->meta_key` does not always return the meta data.
3. Depending on how the post is instantiated, the meta value will be passed through `sanitize_post_field()` before being returned.
4. Meta will always be pulled with `$single` set to `true`.

All in all there are no benefits here, and only downsides.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.