WordPress / WordPress/WordPress-Coding-Standards
switch_to_blog() - flag for missing subsequent restore_current_blog()
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 2.8k
- Forks
- 521
- Avg merge
- 5d 20h
- Merged PRs (30d)
- 1
Description
Describe the solution you'd like
Sometimes difficult to determine logic bugs occur when switch_to_blog() is called without a subsequent restoration. It would be worth flagging to the user that restore_current_blog() should be called after (if not already) after switch_to_blog().
This should be reported:
function switch_blog() {
$id = get_current_blog_id();
if ( $id === 2 ) {
return;
}
switch_to_blog( 2 );
}
This should not be reported:
function switch_blog() {
$id = get_current_blog_id();
if ( $id === 2 ) {
return;
}
switch_to_blog( 2 );
restore_current_blog();
}
Additional context (optional)
https://github.com/Automattic/VIP-Coding-Standards/issues/651
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by locating the PHP_CodeSniffer rule or sniff that analyzes WordPress function calls, then inspect how it handles call sequences and function boundaries. Use the two switch_to_blog() examples as behavioral cases: an unpaired call should be reported, while a following restore_current_blog() should not be.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php
- Domain
- tooling
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100