Tag Info

Hot answers tagged

11

Yes, WordPress's maintains backward compatibility (spaghetti), it would not be feasible to adhere to PSR-0 and rename everything, which is somewhat unfortunate for the long-term, with regards to FIG. That being said it's possible to adopt it for plugins and themes, there is a trac about it here: https://core.trac.wordpress.org/ticket/21300


10

This coding style is known as a Yoda Condition, and it's nothing specific to WordPress. I've used the same style in C++, C#, and even JavaScript code. The main benefit of a Yoda Condition is that it prevents accidental value assignment from a typo. Consider the following (often-seen-in-the-wild) typo: if ( $some_variable = 1 ) { // ... } This will ...


7

WordPress coding standard for PHP does not state anything about this, also there is no other standard, so it's up to developer choose one way or another. I have to say that the 2 methods have a different approach, while the first contain a conditional logic, the second is conditional function declaration, it means that if you try to call the function you ...


6

Do not create functions on the fly. This is hard to read and to debug. Implement separation of concerns instead, and separate the registration of callbacks from their execution (business logic). Setting the conditional checks logically in front of the callback registration is dead simple now. Wait for the action template_redirect to instantiate that ...


5

First of all lets clarify what paradigm word means in the programming. It means that we come to an agreement that we will handle certain cases/issues/situations in a certain way. For instance, we come to agreements that foot-passenger should cross a road on a green light in our country. This is our green light paradigm. In another country could be an ...


4

<!-- file shouldn't be named wp-lunch.php as it's not part of WP core --> <?php if ( current_user_can( 'eat_sandwich' ) ): // more specific verb makes more sense to me ?> <?php get_header( 'sandwich' ); // native function accepts type argument ?> <?php while ( have_fillings() ): the_filling(); // maybe native API, but feels ...


4

What happen if the user don't have capability to eat sandwich? WSOF? If I'd want to follow average default themes templates, I'd go for // eat-sandwich.php (as @Rarst said avoid wp-lunch.php as it's not part of WP core) get_header( 'sandwich' ); if ( current_user_can( 'eat_sandwich' ) ) { get_template_part( 'eat-sandwich', 'content' ); } else { // ...


3

You can use the Theme-Check plugin. The theme check plugin is an easy way to test your theme and make sure it's up to spec with the latest theme review standards. With it, you can run all the same automated testing tools on your theme that WordPress.org uses for theme submissions. The tests are run through a simple admin menu and all results are ...


3

I just want to add that in general, one should be careful using conditional tags like: if( is_*() ) { // stuff } in the global scope of functions.php, because it will run before any filter or action is fired with do_action() or apply_filters().


3

There is no difference when you are using just echo. What works different is unset(): function test_unset_1() { global $post; unset( $post ); } function test_unset_2() { unset( $GLOBALS['post'] ); } test_unset_1(); echo $GLOBALS['post']->ID; // will work test_unset_2(); echo $GLOBALS['post']->ID; // will fail The reason is that unset() ...


3

This is mostly just for readability. The semantics change before and after a parenthesis, and we are used to read spaces as word separators in western languages, so we can read such code faster. Another point is search: If you want to search for all changes on variables beginning with $post it is easier to write a regex when you know there is always a space ...


2

WordPress coding standards are loosely based on the Pear Coding Standards The Drupal open source project also follows similar standards and has published a guide to configuring various editors and IDEs which includes code for a Emacs Drupal mode which can be modified slightly for WordPress.


2

UNIX line endings (\n) and UTF-8 are just common code standards. As far as I can see, they are not even mentioned in the Coding Standards. Most (all?) core PHP files are just plain US-ASCII. Try to follow that path to keep your files as compatible as possible. If you use UTF-8, add a line at the start of a PHP file with an Encoding Cookie or file variable: ...


1

PHP itself does not care whether you use DOS or UNIX style line endings in the PHP code sections, because it simply ignores whitespace anyway, so both of those get ignored regardless. PHP interpreters do not know about the existence of Byte Order Marks in files, and so having a BOM at the start of the PHP file will cause that BOM to be output when the PHP ...


1

I highly recommand you to see this link. This will help you to respect coding standards in WP. EDIT : add link I give in comments


1

Memory is not the issue here. There are no complex operations, and the difference will be almost unmeasurable. What matters is readability: If your stuff function is rather short (less than eight functional steps) and you use the check just once – keep the check in it. But if you need the support check in different functions separate it. I'd rather ...


1

This is a very subjective question — though a very good and interesting one, and I'll try to provide my personal view on this. In general, you should always try to adhere to the WordPress Coding Standards. The only exception to this is when you're changing existing code. Basically, when you're editing an existing "product" (e.g. a plugin or theme), you ...


1

Roots adheres to its own conventions with a bit less spacing and 2 space indentation compared to WP's 1 tab indentation Here is what the codex say about indentations Your indentation should always reflect logical structure. Use real tabs and not spaces, as this allows the most flexibility across clients. Exception: if you have a block of code ...


1

No need for all of the opening and closing delimiters or clear linespaces when already indented: <?php if ( current_user_can( 'has_sandwich' ) ) { get_sandwich_header(); while ( has_filling() ) { the_filling(); get_sandwich_part( 'thick_layer', get_filling() ); } get_sandwich_footer(); } Should probably have a reset ...



Only top voted, non community-wiki answers of a minimum length are eligible