Questions tagged [coding-standards]
The coding-standards tag has no usage guidance.
56 questions
1
vote
1
answer
45
views
plugin development code standard not matching for SQL query
In WordPress before submitting plugin need to check code with the PCP
It keeps showing warning of Use of a direct database call is discouraged
I tried to follow all the coding standard given by ...
0
votes
1
answer
68
views
phpcs, phpcbf and loose comparisons
phpcs found many warnings about "Loose comparisons" and means, that phpcbf can these fix automatically. But phpcbf does not fixed them:
93 | WARNING | [x] Loose comparisons are not allowed....
0
votes
1
answer
15
views
Problem assigning by reference [closed]
This code was working now it gives:
Strict Standards: Only variables should be assigned by reference
Code:
$starredgroup = $myPage->getGroup($starredgroup);
$starredgroup_id = $starredgroup['...
0
votes
1
answer
758
views
How can one use variables in a template or template part without polluting the global scope?
I'm a developer who is relatively new to developing in PHP for WordPress, and I am working on a WordPress site that has a considerable amount of custom code in the site's theme. I have been enjoying ...
1
vote
1
answer
865
views
$wpdb->prepare referencing a const without a coding guideline warning
I have defined my database table names as constants in a base class and use them throughout my code.
protected const MAP_KEY_TABLE = 'eds_map_keys';
If I use them like this I get a warning to use ...
4
votes
0
answers
3k
views
Correct processing of `$_POST`, following WordPress Coding Standards
Background
I'm building a project that according to the specifications need to follow the WordPress Coding Standards. I am using phpcs to scan my code.
If I try to pass $_POST-data into a function ...
0
votes
0
answers
28
views
Check if almost 10 year old - working code is up to date
I found the following code and tested it on my site. Works fine. But im not sure if the code is up to date because the last few years so much has changed on wordpress. Thx for your feedback and ...
8
votes
2
answers
4k
views
How to set up phpcs with WordPress coding standard with PHP8?
Trying to set up PHP_CodeSniffer with the WordPress Codings Standards. I'm running PHP v8.0.3 and phpcs v3.5.8. (in March 2021).
WPCS is installed via
git clone -b master https://github.com/WordPress/...
0
votes
1
answer
365
views
Exit or die in main plugin file breaking php standards recommendation
I have a few plugins that start something like this:
namespace MyPlugin;
// If this file is called directly, abort.
if (! defined('WPINC')) {
die;
}
/**
* Define Constants
*
*/
define(...
0
votes
1
answer
228
views
Understanding how the class family `inner-container` works
After changing the theme to Twenty Twenty-One, I have a bug with custom containers from various block plugins. In particular, custom blocks should have a class ending with inner-container (which I ...
1
vote
0
answers
106
views
Prefixing plugin hooks (actions/filters) with a wrapper class or functions
I'm developing a rather extensive plugin which has a lot of hooks that are prefixed with the plugin namespace. I have created a wrapper class like the one below but because this plugin is geared ...
3
votes
2
answers
2k
views
WP Coding standards - escaping the inescapable?
How do you escape these two examples?
wc_price() wraps the already escaped $product_price in p and span tags with currency symbol.
$product_price = $product->get_price();
<p><?php echo ...
0
votes
1
answer
1k
views
Should I use wp_cache in my plugin to make it faster?
As a plugin developer, I strive for coding plugins that take as less server resources as possible. That's why I thought of avoiding to repeat queries to the database that are run on the same request (...
-1
votes
2
answers
195
views
How to automatically convert "normal" conditions to yoda conditions
I am working on a large WordPress Plugin. It has already more than 10'000 lines of code (PHP + Javascript) and must be changed to yoda conditions, for matching the coding standards of WordPress.
Is ...
2
votes
1
answer
2k
views
Hyphens instead of Underscores in Post-type Archive Template Filenames when Post-type handle contains underscore
I'm working on implementing WordPress coding standards on a theme which has several custom post-types, each of which have underscores in their handles.
The theme uses the custom post-type handle in ...
4
votes
2
answers
1k
views
Nonce in settings API with tabbed navigation
I'm rewording this because, as it's correctly been pointed out, the original post was "woefully inadequate." I made a plugin which has tabbed views for the various settings. It's based on this ...
1
vote
1
answer
79
views
Strange glyphs code error
Recently I've noticed that just above the web page logo, strange glyphs appeared. What might be the cause? How to get rid of these.
1
vote
2
answers
2k
views
Assignments must be the first block of code on a line Validation Error on Travis
Travis is giving me ~ Assignments must be the first block of code on a line for this specific line of code:
$validate_string = $pf_param_string = substr( $pf_param_string, 0, - 1 );
It seems fine ...
1
vote
1
answer
4k
views
WP nonce verification
I'm busy testing out the plugin through Travis and the only issues remaining are:
Processing form data without nonce verification
It seems to be relating to this action:
/**
* An action that handles ...
1
vote
0
answers
36
views
Should I check for privileges before hooking into `wp_ajax_$handle` or after?
Should I heck for privileges before even hooking the wp_ajax_hook_name or in the function that I've hooked to it?
The first way:
if( current_user_can( 'manage_options' ) ) {
add_action( '...
1
vote
3
answers
5k
views
Translate a Constant while appeasing WordPress PHPCS
The following works but isn't up to snuff with PHP Code Sniffer WordPress coding standards
<?php esc_html_e( ADDRESS, 'wprig' ); ?>
Linter yells at me with:
[WordPress.WP.I18n....
0
votes
1
answer
627
views
Standard technique for AJAX post endpoint: WP REST or WP API?
I need to create a post endpoint for a contact form that any user (not just logged in users) can use. The post is made via AJAX/REST.
Is the industry standard way to do this via a new WP REST ...
1
vote
2
answers
2k
views
PHPCS: Strings should have translatable content
I am validating plugin to make it compatible with WPCS. a function which contains gettext placeholder returning error when i run phpcs command. Source code and screenshot of error is attached for ...
0
votes
1
answer
23
views
How could I better initialize a method from my class?
I have this class that generates some share links:
<?php
class Post_Share {
/**
* Class to add post-sharing capabilities to each post. It has to be initialized within a post page.
*/
...
1
vote
0
answers
58
views
Add comments for template variables
I am writing a small plugin that uses a custom template to render content. The plugin uses extract() to add variables that can be used inside the template. I was wondering if there is a standard for ...
0
votes
1
answer
2k
views
Class or function wrapper for plugin code
When developing plugins, to avoid using a prefix for all functions and variables, it is common to use a wrapper for the plugin code. I have seen in many places the practice of using a class and I want ...
3
votes
1
answer
326
views
Are we allowed to use the Allman (BSD) indent style when coding WordPress plugins and themes?
I am a real fan of the Allman (BSD) coding indent style. However, when i review the top rated plugins on WordPress, i am finding them using the K&R coding indent style.
Is there a requirement ...
4
votes
2
answers
1k
views
PHP Coding Standards, Widgets and Sanitization
I have an issue with custom widgets and WordPress Coding Standards. When you create a custom widget, your class should have a "widget" method that will display the widget. Something like:
<?php
...
4
votes
1
answer
975
views
how to escape wp_oembed_get for phpcs
I am using phpcs to help with making sure I write nice, valid WP theme code but am coming across the following issue regarding escaping before output.
If I try to use echo wp_oembed_get( 'https://www....
1
vote
0
answers
56
views
What's the best practice way of handling custom fields in different post formats?
I am developing a theme that is compatible multiple post formats (video, audio, gallery, status, quote, etc.)
From the perspective of a theme developer, what is the best practice way of handling ...
2
votes
2
answers
1k
views
How Flexible are the WordPress Coding Standards for PHPCS?
In my WordPress workflow I use Gulp and have a task that runs my PHP files through PHPCS using the WordPress coding standards tests (https://github.com/WordPress-Coding-Standards/WordPress-Coding-...
8
votes
2
answers
2k
views
When to use Exceptions vs Error Objects vs just plain false/null
I'm in the process of writing a plugin and I'm trying to gauge when to use different approaches of handling errors.
There are three methods I'm considering:
Throwing an Exception (custom class)
...
0
votes
1
answer
1k
views
PHP Code Sniffer - WordPress VIP Coding Standards
I'm trying to fix up my code to meet the WordPress VIP Coding Standards. I'm getting a couple of issues that I'd like to see go away, but i'm not sure what the best strategy is.
The first issue is ...
1
vote
3
answers
460
views
Can I use namespaces in my plugin?
I am building a plugin that I hope to eventually put on Envato CodeCanyon.
Currently I am creating functions of the form mypluginname_action. This is quickly becoming silly as I have to do that a lot....
0
votes
1
answer
35
views
Do define() statements need phpDocumentor-style docblocks?
There's no word on this in the WordPress Documentation Standards, but do define() statements need phpDocumentor-style docblocks?
/**
* Should this docblock be here?
*
* @since 1.0.0
*/
define( '...
7
votes
1
answer
5k
views
Avoiding "Usage of a direct database call is discouraged"
WordPress-Coding-Standards reports that "Usage of a direct database call is discouraged."
It's complaining about my usage of $wpdb->insert() in a plugin. Is this not recommended? What is the ...
0
votes
1
answer
631
views
Sanitization html output itself
I've just started code according to VIP WordPress Coding Standards and I stuck with sanitization. I know that all outputs need sanitization without any exception. But how can I do sanitization of a ...
0
votes
1
answer
80
views
Why the WP Core team does not allow filter_* functions? [closed]
This question is somehow related to this other question.
Provided that is seems we have a solution for the magic quotes issues, why the WP Core team does not allow them?
This is unclear to me and I ...
5
votes
2
answers
774
views
How should I document function calls?
Reading the WordPress PHP Documentation Standards, I can't see anywhere that mentions how to document a function call such as add_action( 'foo', 'bar' );
Should I be documenting function calls and, ...
25
votes
2
answers
3k
views
Why is Wordpress code so "space-happy"?
The WP core, many WP plugins, and the WP coding standards themselves use a very "generous application" of the Space character (not for indentation, but "inside" of parens and brackets). This seems to ...
9
votes
4
answers
3k
views
How to name files of namespaced classes?
WP Core Handbook > PHP Coding Standards > Naming Conventions suggests the following for naming files with classes:
Class file names should be based on the class name with class-
prepended and the ...
1
vote
0
answers
88
views
What make possible unit test plugins?
This is more a theory related question.
I'm not looking at the practical ways to do this, as I can find plenty of example (official and not), which however does not explain how it is possible to ...
3
votes
2
answers
260
views
What to do when theme and WordPress coding standards conflict?
I am considering different starter themes such as _s and roots. Roots looks very good, especially its sass fork. Anyway, there's one thing that concerns me. It seems like Roots doesn't follow the ...
7
votes
5
answers
981
views
Actions, functions and conditionals
When developing themes and plugins it is sometimes neccesary to add some functionality to some hook using conditional statements.
Example:
function my_custom_function() {
if( is_home()) {
...
8
votes
3
answers
807
views
Sandwich Coding Standards
I recently spotted the code for a wp-lunch.php file intended for a WordPress template, what would this look like if following the proper WordPress coding standards?
<?php if ( current_user_can('...
7
votes
4
answers
2k
views
Wordpress and event-driven programming - what is it about?
I want to state very clearly that this does not intend to revive the discussion about functional/procedural programming versus object-oriented programming. There is plenty said about that, on WPSE and ...
0
votes
2
answers
569
views
Wordpress Theme Validator?
I'm looking for a Wordpress theme Validator. Not necessarily something that validates the HTML but more along the lines of adhering to the Wordpress Coding Standards for Theme Development.
2
votes
2
answers
3k
views
What is WordPress file encoding?
I noticed that WordPress files use UNIX end-of-line and UTF-8 without BOM. Is this a standard that should be used? Is there anything else to pay attention to?
I noticed that Filezilla fails to ...
0
votes
1
answer
57
views
Is there a common structure for a wordpress formular?
I am working on a plugin for wordpress and was wondering if there is any common standard for the html and css structure and naming which I should use if I create a html form?
so is there a definition ...
2
votes
1
answer
1k
views
spaces inside parenthesis
The Wordpress coding standards states to put spaces both inside and outside opening and closing parenthesis.
What is the rationale behind spaces on the inside of parenthesis? I've hardly ever seen ...