1

I want to use namespace in my plugin. And I have error after PHP_Code_Sniffer check.

Namespaces declared by a theme/plugin should start with the theme/plugin prefix. Found: "eustatos\test_plugin". (WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound)

My folder structure:

.
└── test-plugin
    ├── composer.json
    ├── phpcs.xml
    ├── test-plugin.php
    └── vendor
// test-plugin.php
<?php

namespace eustatos\test_plugin;

phpcs.xml content from here
How I must name namespace?

1 Answer 1

2

At first, we need to update phpcs.xml.

Let's replace "my_prefix" with your plugin name (then, assuming "eustatos"):

    <rule ref="WordPress.NamingConventions.PrefixAllGlobals">
        <properties>
            <property name="prefixes" type="array">
                <element value="eustatos"/>
            </property>
        </properties>
    </rule>

Then, you can use namespace start with eustatos in project PHP file.

ex:

namespace eustatos\test_plugin;

cf.

Customizable sniff properties · WordPress/WordPress-Coding-Standards Wiki

To execute this check one or more prefixes which should be considered valid must be provided via the $prefixes property in your custom ruleset or via the command-line.

If this property is not set, this sniff will not run.

<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
  <properties>
      <property name="prefixes" type="array">
          <element value="my_prefix"/>
          <element value="tgmpa"/>
      </property>

  </properties>
</rule>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.