Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions WordPress/Docs/WP/GlobalVariablesOverrideStandard.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0"?>
<documentation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd"
title="Global Variables Override"
>
<standard>
<![CDATA[
WordPress global variables should not be overridden.

Two WordPress global variables, `$content_width` and `$wp_cockneyreplace`, are
specifically intended to be overridden by themes/plugins and are exempt from this rule.
]]>
</standard>
<code_comparison>
<code title="Valid: Different variable name.">
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm not sure about this title. Maybe Valid: Using custom variable/key names instead of WordPress globals. or Valid: Using custom variable/key names. is a bit better and more descriptive? Happy to hear other suggestions you might have.

The concern that I have with Valid: Different variable name. is that it reads a bit vaguely and doesn't clearly communicate what makes the examples below valid.

<![CDATA[
<em>$my_query</em> = new WP_Query( $args );


<em>$GLOBALS['my_data']</em> = "some data";
]]>
</code>
<code title="Invalid: Global variable overridden.">
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
<code title="Invalid: Global variable overridden.">
<code title="Invalid: WordPress global variable overridden.">

I think it is important to mention that this is about WordPress globals and not any globals.

<![CDATA[
<em>global $wp_query;</em>
<em>$wp_query</em> = new WP_Query( $args );

<em>$GLOBALS['wp_query']</em> = new WP_Query( $args );
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
<em>$GLOBALS['wp_query']</em> = new WP_Query( $args );
<em>$GLOBALS['post']</em> = get_post( 1 );

Nitpick: maybe use a different global here to illustrate one more WP global that is flagged by this sniff?

]]>
</code>
</code_comparison>
</documentation>
Loading