Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The $domain parameter must be a single text string literal. Found: self::$text_domain #2398

Closed
abhij89 opened this issue Oct 23, 2023 · 4 comments

Comments

@abhij89
Copy link

abhij89 commented Oct 23, 2023

Bug Description

I am using phpcs with standard WordPress. I have defined the domain globally in my class as a variable. Then using the same variable to pass the domain to all escaping functions. But it is throwing error saying The $domain parameter must be a single text string literal. Found: self::$text_domain which is not correct

Minimal Code Snippet

// Defined variable
public static $text_domain = 'my-plugins-domain';

The issue happens when running this command:

phpcs --standard=WordPress my-plugin-file.php

... over a file containing this code:

<?php _e( 'My Plugin Title', self::$text_domain ); ?>

The file was not auto-fixed via phpcbf

Error Code

Environment

Question Answer
PHP version 8.1.0
PHP_CodeSniffer version 3.7.2
WordPressCS version 3.0.1
WordPressCS install type Composer global

Tested Against develop Branch?

  • [ x ] I have verified the issue still exists in the develop branch of WordPressCS.
@dingo-d
Copy link
Member

dingo-d commented Oct 23, 2023

Hi @abhij89!

You should check up on the internationalization chapter in the themes handbook: https://developer.wordpress.org/themes/functionality/internationalization/#add-text-domain-to-strings or in the plugins handbook:
https://developer.wordpress.org/plugins/internationalization/how-to-internationalize-your-plugin/#basic-strings

The text domain should never be passed as a variable. Because having it as a variable will not allow the parsing tools to differentiate between text domains.

@dingo-d dingo-d closed this as not planned Won't fix, can't repro, duplicate, stale Oct 23, 2023
@jrfnl
Copy link
Member

jrfnl commented Oct 23, 2023

@abhij89 I'm not sure what you are reporting, but the sniff is working exactly as it should.

You should NEVER put the text domain in a variable or constant and pass that. You should ALWAYS pass a literal text string as the text domain.

There have been numerous articles written about this. I suggest you read them.

Some suggestions:

@abhij89
Copy link
Author

abhij89 commented Oct 23, 2023

My bad, I missed this rule.

@abhij89
Copy link
Author

abhij89 commented Oct 23, 2023

Thank you for clarifying

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants