For as much as I talk about writing code that conforms to the WordPress Coding Standards and for using tools such as PHP CodeSniffer, there are times where I’ve found that you need to silence the errors for the sake of something you’re trying to do.

Case in point:

WordPress uses global variables to maintain certain data structures. As per the coding standards, you should not change the values of global variables.

As a rule of thumb, this is true. But what about in the case where you need to make a modification to the admin menu (which uses a global variable)?

How To Ignore Coding Standards

Before I actually show the small code snippet for how to do this, I want to be clear that this is not meant to show a way to completely circumvent writing standards-compliant code.

Ignore Coding Standards in WordPress

That is, if you opt to ignore all of the errors then you might as well do away with the Code Sniffer. But if everything else is set up properly like:

  • using the proper hook for what you’re trying to achieve,
  • making sure that your conditional logic is set up using Yoda conditions,
  • adding query string variables to a value using add_query_arg,
  • and so on

And you still need to modify the value of the global variable, then I think it’s an acceptable use case.

So, for example, here’s a short snippet for how to ignore coding standards when working with WordPress coding standard-compliant code:

Easy enough, isn’t it? You simply define a directive at the beginning of where the sniffer should ignore your code and then terminate the directive at the end of the code.

I don’t necessarily think it’s a good idea to ignore an entire block (that would call into question the standards of your block), but if you need to make a modification such that is using the WordPress API and is using all of the other standards properly, then I think it’s acceptable.

Category:
Articles
Tags:

Join the conversation! 9 Comments

  1. Hey Tom,

    I would probably prefer to code this into the project’s phpcs.xml file, so that the source code itself can be kept clean.

    So, you would add something along these lines to your phpcs.xml file:


    <rule ref="WordPress.Variables.GlobalVariables">
    <exclude-pattern>*/src/class-name-that-overrides-globals.php</exclude-pattern>
    </rule>

  2. Regarding the example snippet, a filter was recently added for modifying the submenu file, so I don’t think overriding the global is necessary anymore.

    It’s also worth being aware of whitelist comments to ignore specific sniffs. In the example, this does the same thing, but is a little more descriptive and prevents other sniffs from being ignored accidentally:

    $submenu_file = add_query_arg( $args, $url ); // WPCS: Override OK.

    • Regarding the example snippet, a filter was recently added for modifying the submenu file, so I don’t think overriding the global is necessary anymore.

      Oh, nice! I need to refactor this code then. If you don’t mind me asking, where did you catch that this filter was added?

      I try to follow the usual channels but it’s a lot of information sometimes (especially when you’re on vacation ;) and I don’t always catch all of this.

      In the example, this does the same thing, but is a little more descriptive and prevents other sniffs from being ignored accidentally:

      Yeah, I’ve also used // Input var okay for certain things with the PHP globals only when I’m sure I’ve done the proper inspection, though I like yours, too.

      I’m still curious about this new filter :).

      • I don’t recall where I first noticed that filter, but it was likely the @wpdevel Twitter feed. Aside from other developers, that feed is about all I’m able to keep up with. I’ve hacked that $submenu_file global a few times myself, so I was glad to see that filter pop up.

  3. You can also use this to ignore single lines only:

    // @codingStandardsIgnoreLine

Leave a Reply