Best Practices 10up Engineering
10up Engineering Best Practices
目录:
CSS
JS
MarkUp
Tools
PHP
Structure
Migrations
Version Control
1. CSS
CSS Syntax
Write one selector per line
Write one declaration per line
Closing braces should be on a new line
.class-1, .class-2, .class-3 { width: 10px; height: 20px; color: red; background-color: blue;
Declaration ordering:
Positioning
Box model
Typography
Visual
Other
Performance
Limit the number of requests by concatenating CSS files and encoding sprites and font files to the CSS file.
Minify stylesheets
Use GZIP compression when possible Automate these tasks with a PHP or/and JavaScript build process.
2. JS
Try to Pass an HTMLElement or HTMLCollection to jQuery Instead of a Selection String
jQuery( document.getElementById( 'menu' ) );
Cache DOM Selections
var $menu = jQuery( '#menu' ); var $hideButton = jQuery( '.hide-button' ); $hideButton.on( 'click', function() { $menu.hide(); });
Event Delegation
jQuery( '#menu' ).on( 'click', 'li', function() { // Do stuff! });
3. MarkUp
Semantic HTML
Elements like
<header>
,<nav>
,<footer>
, or<article>
do a much better job of explaining the content that is contained within the element than<span>
or<div>
Optimize Readability
<ul> <?php foreach( $things as $thing ) : ?> <li><?php echo esc_html( $thing ); ?></li> <?php endforeach; ?> </ul>
5. PHP
Efficient Database Queries
When querying the database in WordPress, you should generally use a
WP_Query
object.Only run the queries that you need.
'no_found_rows' => true:
useful when pagination is not needed.'update_post_meta_cache'
=> false: useful when post meta will not be utilized.'update_post_term_cache'
=> false: useful when taxonomy terms will not be utilized.'fields' => 'ids'
: useful when only the post IDs are needed (less typical).
Do not use
posts_per_page => -1
Do not use
$wpdb
orget_posts()
unless you have good reason.get_posts()
actually callsWP_Query
, but callingget_posts()
directly bypasses a number of filters by default. Not
Avoid using
post__not_in
<?php
$foo_query = new WP_Query( array(
'post_type' => 'post',
'posts_per_page' => 30 + count( $posts_to_exclude )
) );
if ( $foo_query->have_posts() ) :
while ( $foo_query->have_posts() ) :
$foo_query->the_post();
if ( in_array( get_the_ID(), $posts_to_exclude ) ) {
continue;
}
the_title();
endwhile;
endif;
?>
Escape or Validate Output