Professional WordPress Plugin Development. Brad Williams

Чтение книги онлайн.

Читать онлайн книгу Professional WordPress Plugin Development - Brad Williams страница 19

Professional WordPress Plugin Development - Brad Williams

Скачать книгу

also prefixes class filenames with class‐. The PDEVSetup or PDEV_Setup class from the previous section in this chapter would be named class‐pdev‐setup.php following the WordPress standard.

      Again, this is where WordPress deviates from the standards within the PHP community. At some point, you'll likely want to use an autoloader, such as the one provided by Composer (https://getcomposer.org). Once you do that, you'll quickly realize that the best method of naming class files is for the filename to match the class name exactly because it simplifies the autoloader code and keeps you from having to write a custom implementation. In this case, you should name the file for the PDEVSetup class as PDEVSetup.php.

      Single and Double Quotes

      PHP allows you to use single or double quotes when defining strings. With WordPress, it's recommended to always use single quotes when possible. This makes it easy to use double quotes within the string without escaping them. The following is an example of echoing a link that has double quotes within the single‐quoted string:

      <?php echo '<a href="https://example.com">Visit Example.com</a>';

      You can also use double quotes when you need to insert a variable within the string. The following example grabs the current site's name and echoes a message with the variable:

      <?php $pdev_site_name = get_bloginfo( 'name', 'display' ); echo "You are viewing {$pdev_site_name}.";

      Indentation

      Put 10 programmers in a room and pose the question, “Spaces or tabs?” This may be the closest you'll ever see to a programming discussion resulting in fisticuffs. Many a developer has lived and died on their personal space/tab hill. The one thing that most can agree on is that your code should follow some sort of standard indentation.

      The WordPress standard is to indent all code with tabs. Consider the following example of an if/else statement:

      It's hard to follow the logical structure of the code with no indentation. Instead, you should indent each line within the brackets of the conditions, as shown in the following example:

      <?php if ( $condition ) { echo 'Yes'; } else { echo 'No'; }

      The one exception that WordPress makes to its tab preference is when aligning multiple lines of similar items. Take note of the spacing usage in the following snippet:

      <?php $some_var = 'ABC'; $another_var = 'JKL'; $yet_another_var = 'XYZ';

      As you can see, the operators and values assigned to the variables are clearly lined up and easy to read.

      Brace Style

      Any multiline statement in PHP should use brackets to contain the entire code block. Following this brace style will keep your code clear and less prone to errors. See the next code snippet for the appropriate usage of braces:

      <?php if ( $condition_a ) { action_a(); } elseif ( $condition_b || $condition_c ) { action_b(); action_c(); }

      Even if there's only a single line within each block, make sure to enclose it too. Forgoing the braces for single‐line statements is usually a bad idea and may result in bugs when you add extra code in the future.

      Space Usage

      When building your plugin, you should always add spaces after commas and on each side of logical, comparison, and other operators. The following code snippet shows several basic PHP examples and how they should be spaced:

      Using this spacing technique keeps your code clean and makes it easy to read. The rule of thumb is to always be judicious with spacing. Or, when in doubt, use a space.

      Shorthand PHP

      You should never use shorthand PHP tags ( <? and ?>) when developing plugins. Shorthand tags must be enabled on the server to work. As a plugin developer, you typically won't have access to the server, so there's no guarantee they'll work for your users. Always use the full PHP opening and closing tags ( <?php and ?>).

      The one exception to this rule is for the short echo tag, as shown in the following example:

      <?= 'Hello, World!'; ?>

      The short echo tag is always enabled as of PHP 5.4. This is uncommon usage and not standard for WordPress. It's typically used in templates, which is not the primary use case for plugins.

      SQL Statements

      When writing SQL statements in WordPress to make a direct database call, always capitalize the SQL part of the statement. Most statements should be written on a single line, but it's OK to break the statement into multiple lines if the statement is complex.

      SELECT ID FROM wp_users WHERE user_login = 'example'

      Chapter 4, “Security and Performance,” covers how to create proper SQL statements in more detail.

      This chapter covered how to properly set up your WordPress plugin. Following the methods and techniques outlined in this chapter will set you on the path to building plugins professionally. Starting from a solid foundation by following coding standards and documenting your code will make it easier to maintain your plugin in the long term. It will also help other developers understand your code. It's important to get the foundation right before moving on to more advanced topics.

      WHAT'S IN THIS CHAPTER?

       Creating menus and submenus

       Saving and retrieving plugin settings

       Saving global and per‐user options

       Designing and styling your plugin

      CODE DOWNLOADS FOR THIS CHAPTER

      The code downloads for this chapter are found at www.wiley.com/go/prowordpressdev2e on the Downloads tab.

      Integrating your plugin in WordPress is a critical step in building a professional plugin. WordPress features many different ways to integrate your plugin including adding top‐level and submenu items, creating custom setting pages, and styling to match the existing Dashboard user interface (UI).

      In this chapter,

Скачать книгу