SlideShare una empresa de Scribd logo
1 de 45
Descargar para leer sin conexión
#wpewebinar
February 28, 2018
#wpewebinar
#wpewebinar
CROP IMAGE
TO GRAY BOX
We’ll answer as many questions as we can after
the presentation
ASK QUESTIONS AS WE GO
Slides and recording will be
made available shortly after
the webinar
Use the “Questions” pane
throughout the webinar
#wpewebinar
WHAT YOU’LL LEARN:
● Why coding standards are important
● Common mistakes even good developers make
● Tips for becoming an even better developer
● Resources available to learn more
● What Tide is and what’s coming to WordPress.org
● Q&A
#wpewebinar
Director of Product & Innovation
XWP
Luke Carbis
● Film photography hobbyist
● Bullet Journal devotee
● Has lived in Rome and Jerusalem
(now in Brisbane)
Innovation Program Manager
WP Engine
Steven Word
● Makes Instrumental Hip-Hop
● Loves Hiking
● Craft Beer Enthusiast
Luke Carbis
From Good to Great
Coding Standards and WordPress
Scripts & Styles
Prefixing
Sanitising & Escaping
Formatting Standards
But… why?
If it ‘aint broke, why fix it?
— Terrible advice of unknown origin.
Wordpress
What’s wrong here?
WordPress
Capital P, dangit!
Why are these conventions so important, anyway?
● Prevent conflicts
Why are these conventions so important, anyway?
● Prevent conflicts
● Prevent loading unnecessary files
Why are these conventions so important, anyway?
● Prevent conflicts
● Prevent loading unnecessary files
● Readability
Why are these conventions so important, anyway?
● Prevent conflicts
● Prevent loading unnecessary files
● Readability
● Maintainability
Why are these conventions so important, anyway?
● Prevent conflicts
● Prevent loading unnecessary files
● Readability
● Maintainability
● Prevent conflicts with other plugins
● Automatic minification
● Dependencies loaded
● Include all WordPress functionality
● Handling security nonces correctly
● Prevent SQL injection attacks
● Encourage community support
Better, Faster, and Stronger.
Best practices exist to make your site
Learn to write code you can be proud of.
Scripts & Styles
Do not…
add html directly into your theme:
or
or even
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
var foo = 'bar';
</script>
<script src="//code.jquery.com/jquery-latest.min.js"></script>
This is better:
In your plugin, or in your functions.php file, use the wp_enqueue_script and wp_enqueue_style
functions:
wp_register_script( 'select2', get_template_directory_uri() . 'ui/select2/select2.m
wp_register_style( 'select2', get_template_directory_uri() . 'ui/select2/select2.cs
wp_enqueue_script( 'select2' );
wp_enqueue_style( 'select2' );
wp_enqueue_script( 'my-script', 'ui/main.js', array( 'jquery', 'select2', 'heartbea
or
This is even better:
Wrap your wp_enqueue_script and _style calls in a function, which fires at the right time.
add_action( 'wp_enqueue_scripts', 'my_plugin_enqueue_scripts' ) );
my_plugin_enqueue_scripts() {
wp_register_script( 'select2', get_template_directory_uri() . 'ui/select2/selec
wp_register_style( 'select2', get_template_directory_uri() . 'ui/select2/select
wp_enqueue_script( 'select2' );
wp_enqueue_style( 'select2' );
}
Prefixing
Do not name your functions
custom_meta_box()
or
save_options()
This is better:
my_plugin_custom_meta_box()
or
my_plugin_save_options()
This is even better
class Akismet {
function init() {
// …
}
function save() {
// …
}
}
$GLOBALS['akismet'] = new Akismet;
Sanitising & Escaping
Don’t do this:
echo $permalink;
Do this:
echo esc_url( $permalink );
Escape Outputs
Don’t do this:
Do this:
$foo = $_GET[ 'foo' ];
$bar = $_GET[ 'bar' ];
echo '<p id="' . $foo . '">' . $bar . '</p>';
echo wp_kses_post(
sprintf(
'<p id="%s">%s</p>',
esc_attr( $_GET[ 'foo' ] ),
esc_html( $_GET[ 'bar' ] )
)
);
Escape Outputs
Don’t do this:
Do this:
Sanitise Inputs
$title = $_POST['title'];
update_post_meta( $post->ID, 'title', $title );
$title = sanitize_text_field( $_POST['title'] );
update_post_meta( $post->ID, 'title', $title );
Don’t do this:
Do this:
Sanitise Inputs
$secondary_email = $_POST[ 'email' ];
update_user_meta( $user_id, 'secondary_email', $secondary_email );
$secondary_email = sanitize_email( $_POST[ 'email' ] );
update_user_meta( $user_id, 'secondary_email', $secondary_email );
Sanitise with $wpdb
This is one way of getting all posts attributed to a particular author:
global $wpdb;
$post_author = $_POST['author_id'];
$results = $wpdb->get_results(
"SELECT ID, post_title
FROM $wpdb->posts
WHERE post_status = 'publish' AND post_author = $post_author"
);
Sanitise with $wpdb
This is a safer way of doing the same thing:
global $wpdb;
$post_author = intval( $_POST['author_id'] );
$results = $wpdb->get_results(
$wpdb->prepare(
"SELECT ID, post_title
FROM $wpdb->posts
WHERE post_status = %s AND post_author = %d",
'publish',
$post_author
)
);
Formatting Standards
Indentation
Use real tabs and not spaces.
Yoda Conditions
if ( true === $the_force ) {
$victorious = you_will( $be );
}
This is clever:
Clever Code
isset( $var ) || $var = some_function();
But this is readable:
if ( ! isset( $var ) ) {
$var = some_function();
}
Always use braces, even where not required.
Not this:
Braces
if ( condition() )
return true;
if ( condition() ) {
return true;
}
This:
Scripts & Styles
Prefixing
Sanitising & Escaping
Formatting Standards
Tide
Tide is a series of automated tests run against every plugin and
theme in the WordPress directory.
Tide currently tests for Coding Standards and PHP Compatibility.
https://make.wordpress.org/tide
PHP CodeSniffer
with WordPress Coding Standards rules
https://github.com/WordPress-Coding-Standards/
Thank You
xwp.co
#wpewebinar
Slides and recording will be made available
shortly after the webinar
QUESTIONS AND ANSWERS
#wpewebinar
RESOURCES
Tide: A Path to Better Code Across the WordPress Ecosystem
Webinar Links, Snippets and Resources
WordPress Coding Standards Handbook - WordPress.org
Tide
#wpewebinar
CROP IMAGE
TO GRAY BOX
NEXT UP...
Register Now:
http://wpeng.in/
ml-wp
Wednesday, Mar 7
11:00 a.m. CST,
12:00 p.m. EST,
9:00 a.m. PST,
17:00 UTC/GMT
#wpewebinar
HELP US IMPROVE
#wpewebinar
THANK YOU
#wpewebinar
@wpengine

Más contenido relacionado

Más de WP Engine

Más de WP Engine (20)

Best Practices for Site Deployment With Local.pdf
Best Practices for Site Deployment With Local.pdfBest Practices for Site Deployment With Local.pdf
Best Practices for Site Deployment With Local.pdf
 
Site Monitoring: The Intersection of Product, UX Design & Research .pdf
Site Monitoring: The Intersection of Product, UX Design & Research .pdfSite Monitoring: The Intersection of Product, UX Design & Research .pdf
Site Monitoring: The Intersection of Product, UX Design & Research .pdf
 
Front End: Building Future-Proof eCommerce Sites.pdf
Front End: Building Future-Proof eCommerce Sites.pdfFront End: Building Future-Proof eCommerce Sites.pdf
Front End: Building Future-Proof eCommerce Sites.pdf
 
Gutenberg and Headless WordPress.pdf
Gutenberg and Headless WordPress.pdfGutenberg and Headless WordPress.pdf
Gutenberg and Headless WordPress.pdf
 
Blueprints and Other Local Features for Agencies.pdf
Blueprints and Other Local Features for Agencies.pdfBlueprints and Other Local Features for Agencies.pdf
Blueprints and Other Local Features for Agencies.pdf
 
Modern Theming & The Future of WordPress- Working with Full Site Editing and ...
Modern Theming & The Future of WordPress- Working with Full Site Editing and ...Modern Theming & The Future of WordPress- Working with Full Site Editing and ...
Modern Theming & The Future of WordPress- Working with Full Site Editing and ...
 
6 WooCommerce Dev Tricks for Building Fast eCommerce Websites.pdf
6 WooCommerce Dev Tricks for Building Fast eCommerce Websites.pdf6 WooCommerce Dev Tricks for Building Fast eCommerce Websites.pdf
6 WooCommerce Dev Tricks for Building Fast eCommerce Websites.pdf
 
Headless 101 for WordPress Developers.pdf
Headless 101 for WordPress Developers.pdfHeadless 101 for WordPress Developers.pdf
Headless 101 for WordPress Developers.pdf
 
Be the Change: The Future of WordPress with WP Engine's Developer Relations Team
Be the Change: The Future of WordPress with WP Engine's Developer Relations TeamBe the Change: The Future of WordPress with WP Engine's Developer Relations Team
Be the Change: The Future of WordPress with WP Engine's Developer Relations Team
 
An Atlas of Atlas.pdf
An Atlas of Atlas.pdfAn Atlas of Atlas.pdf
An Atlas of Atlas.pdf
 
2022 – Year of the WordPress Developer.pdf
2022 – Year of the WordPress Developer.pdf2022 – Year of the WordPress Developer.pdf
2022 – Year of the WordPress Developer.pdf
 
Using WooCommerce to Scale Your Store
Using WooCommerce to Scale Your StoreUsing WooCommerce to Scale Your Store
Using WooCommerce to Scale Your Store
 
Growing Your WooCommerce Store Without Knowing Code
Growing Your WooCommerce Store Without Knowing CodeGrowing Your WooCommerce Store Without Knowing Code
Growing Your WooCommerce Store Without Knowing Code
 
Between a Block & a Hard Place
Between a Block & a Hard PlaceBetween a Block & a Hard Place
Between a Block & a Hard Place
 
Under the Hood with Headless WordPress and the Google Cloud Platform
Under the Hood with Headless WordPress and the Google Cloud PlatformUnder the Hood with Headless WordPress and the Google Cloud Platform
Under the Hood with Headless WordPress and the Google Cloud Platform
 
Going From Project To Project To Monthly Recurring Revenue with Growth Suite
Going From Project To Project To Monthly Recurring Revenue with Growth Suite Going From Project To Project To Monthly Recurring Revenue with Growth Suite
Going From Project To Project To Monthly Recurring Revenue with Growth Suite
 
Why Protection From DDoS Attacks is Critical For Your Business
Why Protection From DDoS Attacks is Critical For Your BusinessWhy Protection From DDoS Attacks is Critical For Your Business
Why Protection From DDoS Attacks is Critical For Your Business
 
Top Insights for Your WordPress Site
Top Insights for Your WordPress SiteTop Insights for Your WordPress Site
Top Insights for Your WordPress Site
 
Growing Your WooCommerce Store Without Knowing Code
Growing Your WooCommerce Store Without Knowing CodeGrowing Your WooCommerce Store Without Knowing Code
Growing Your WooCommerce Store Without Knowing Code
 
How To Work Faster & More Profitably With Client Site Starter Templates
How To Work Faster & More Profitably With Client Site Starter TemplatesHow To Work Faster & More Profitably With Client Site Starter Templates
How To Work Faster & More Profitably With Client Site Starter Templates
 

Último

Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girlsRussian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Monica Sydney
 
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
ydyuyu
 
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
pxcywzqs
 
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
ydyuyu
 
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
ydyuyu
 
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu DhabiAbu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Monica Sydney
 
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
gajnagarg
 
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsIndian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Monica Sydney
 

Último (20)

Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girlsRussian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
Russian Call girls in Abu Dhabi 0508644382 Abu Dhabi Call girls
 
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
best call girls in Hyderabad Finest Escorts Service 📞 9352988975 📞 Available ...
 
APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53APNIC Updates presented by Paul Wilson at ARIN 53
APNIC Updates presented by Paul Wilson at ARIN 53
 
Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...
Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...
Tadepalligudem Escorts Service Girl ^ 9332606886, WhatsApp Anytime Tadepallig...
 
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
20240510 QFM016 Irresponsible AI Reading List April 2024.pdf
 
Local Call Girls in Seoni 9332606886 HOT & SEXY Models beautiful and charmin...
Local Call Girls in Seoni  9332606886 HOT & SEXY Models beautiful and charmin...Local Call Girls in Seoni  9332606886 HOT & SEXY Models beautiful and charmin...
Local Call Girls in Seoni 9332606886 HOT & SEXY Models beautiful and charmin...
 
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
哪里办理美国迈阿密大学毕业证(本硕)umiami在读证明存档可查
 
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
一比一原版(Offer)康考迪亚大学毕业证学位证靠谱定制
 
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
20240507 QFM013 Machine Intelligence Reading List April 2024.pdf
 
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrStory Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
Story Board.pptxrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
 
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查在线制作约克大学毕业证(yu毕业证)在读证明认证可查
在线制作约克大学毕业证(yu毕业证)在读证明认证可查
 
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
原版制作美国爱荷华大学毕业证(iowa毕业证书)学位证网上存档可查
 
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
2nd Solid Symposium: Solid Pods vs Personal Knowledge Graphs
 
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency""Boost Your Digital Presence: Partner with a Leading SEO Agency"
"Boost Your Digital Presence: Partner with a Leading SEO Agency"
 
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
APNIC Policy Roundup, presented by Sunny Chendi at the 5th ICANN APAC-TWNIC E...
 
Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.Meaning of On page SEO & its process in detail.
Meaning of On page SEO & its process in detail.
 
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu DhabiAbu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
Abu Dhabi Escorts Service 0508644382 Escorts in Abu Dhabi
 
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Dindigul [ 7014168258 ] Call Me For Genuine Models ...
 
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac RoomVip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
Vip Firozabad Phone 8250092165 Escorts Service At 6k To 30k Along With Ac Room
 
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi EscortsIndian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
Indian Escort in Abu DHabi 0508644382 Abu Dhabi Escorts
 

Webinar - From Good to Great: Coding Standards and WordPress

  • 2. #wpewebinar CROP IMAGE TO GRAY BOX We’ll answer as many questions as we can after the presentation ASK QUESTIONS AS WE GO Slides and recording will be made available shortly after the webinar Use the “Questions” pane throughout the webinar
  • 3. #wpewebinar WHAT YOU’LL LEARN: ● Why coding standards are important ● Common mistakes even good developers make ● Tips for becoming an even better developer ● Resources available to learn more ● What Tide is and what’s coming to WordPress.org ● Q&A
  • 4. #wpewebinar Director of Product & Innovation XWP Luke Carbis ● Film photography hobbyist ● Bullet Journal devotee ● Has lived in Rome and Jerusalem (now in Brisbane) Innovation Program Manager WP Engine Steven Word ● Makes Instrumental Hip-Hop ● Loves Hiking ● Craft Beer Enthusiast
  • 5. Luke Carbis From Good to Great Coding Standards and WordPress
  • 6. Scripts & Styles Prefixing Sanitising & Escaping Formatting Standards
  • 7. But… why? If it ‘aint broke, why fix it? — Terrible advice of unknown origin.
  • 10. Why are these conventions so important, anyway? ● Prevent conflicts
  • 11. Why are these conventions so important, anyway? ● Prevent conflicts ● Prevent loading unnecessary files
  • 12. Why are these conventions so important, anyway? ● Prevent conflicts ● Prevent loading unnecessary files ● Readability
  • 13. Why are these conventions so important, anyway? ● Prevent conflicts ● Prevent loading unnecessary files ● Readability ● Maintainability
  • 14. Why are these conventions so important, anyway? ● Prevent conflicts ● Prevent loading unnecessary files ● Readability ● Maintainability ● Prevent conflicts with other plugins ● Automatic minification ● Dependencies loaded ● Include all WordPress functionality ● Handling security nonces correctly ● Prevent SQL injection attacks ● Encourage community support
  • 15. Better, Faster, and Stronger. Best practices exist to make your site
  • 16. Learn to write code you can be proud of.
  • 18. Do not… add html directly into your theme: or or even <link href="style.css" rel="stylesheet" type="text/css" /> <script type="text/javascript"> var foo = 'bar'; </script> <script src="//code.jquery.com/jquery-latest.min.js"></script>
  • 19. This is better: In your plugin, or in your functions.php file, use the wp_enqueue_script and wp_enqueue_style functions: wp_register_script( 'select2', get_template_directory_uri() . 'ui/select2/select2.m wp_register_style( 'select2', get_template_directory_uri() . 'ui/select2/select2.cs wp_enqueue_script( 'select2' ); wp_enqueue_style( 'select2' ); wp_enqueue_script( 'my-script', 'ui/main.js', array( 'jquery', 'select2', 'heartbea or
  • 20. This is even better: Wrap your wp_enqueue_script and _style calls in a function, which fires at the right time. add_action( 'wp_enqueue_scripts', 'my_plugin_enqueue_scripts' ) ); my_plugin_enqueue_scripts() { wp_register_script( 'select2', get_template_directory_uri() . 'ui/select2/selec wp_register_style( 'select2', get_template_directory_uri() . 'ui/select2/select wp_enqueue_script( 'select2' ); wp_enqueue_style( 'select2' ); }
  • 22. Do not name your functions custom_meta_box() or save_options()
  • 24. This is even better class Akismet { function init() { // … } function save() { // … } } $GLOBALS['akismet'] = new Akismet;
  • 26. Don’t do this: echo $permalink; Do this: echo esc_url( $permalink ); Escape Outputs
  • 27. Don’t do this: Do this: $foo = $_GET[ 'foo' ]; $bar = $_GET[ 'bar' ]; echo '<p id="' . $foo . '">' . $bar . '</p>'; echo wp_kses_post( sprintf( '<p id="%s">%s</p>', esc_attr( $_GET[ 'foo' ] ), esc_html( $_GET[ 'bar' ] ) ) ); Escape Outputs
  • 28. Don’t do this: Do this: Sanitise Inputs $title = $_POST['title']; update_post_meta( $post->ID, 'title', $title ); $title = sanitize_text_field( $_POST['title'] ); update_post_meta( $post->ID, 'title', $title );
  • 29. Don’t do this: Do this: Sanitise Inputs $secondary_email = $_POST[ 'email' ]; update_user_meta( $user_id, 'secondary_email', $secondary_email ); $secondary_email = sanitize_email( $_POST[ 'email' ] ); update_user_meta( $user_id, 'secondary_email', $secondary_email );
  • 30. Sanitise with $wpdb This is one way of getting all posts attributed to a particular author: global $wpdb; $post_author = $_POST['author_id']; $results = $wpdb->get_results( "SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_author = $post_author" );
  • 31. Sanitise with $wpdb This is a safer way of doing the same thing: global $wpdb; $post_author = intval( $_POST['author_id'] ); $results = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title FROM $wpdb->posts WHERE post_status = %s AND post_author = %d", 'publish', $post_author ) );
  • 33. Indentation Use real tabs and not spaces.
  • 34. Yoda Conditions if ( true === $the_force ) { $victorious = you_will( $be ); }
  • 35. This is clever: Clever Code isset( $var ) || $var = some_function(); But this is readable: if ( ! isset( $var ) ) { $var = some_function(); }
  • 36. Always use braces, even where not required. Not this: Braces if ( condition() ) return true; if ( condition() ) { return true; } This:
  • 37. Scripts & Styles Prefixing Sanitising & Escaping Formatting Standards
  • 38. Tide Tide is a series of automated tests run against every plugin and theme in the WordPress directory. Tide currently tests for Coding Standards and PHP Compatibility. https://make.wordpress.org/tide
  • 39. PHP CodeSniffer with WordPress Coding Standards rules https://github.com/WordPress-Coding-Standards/
  • 41. #wpewebinar Slides and recording will be made available shortly after the webinar QUESTIONS AND ANSWERS
  • 42. #wpewebinar RESOURCES Tide: A Path to Better Code Across the WordPress Ecosystem Webinar Links, Snippets and Resources WordPress Coding Standards Handbook - WordPress.org Tide
  • 43. #wpewebinar CROP IMAGE TO GRAY BOX NEXT UP... Register Now: http://wpeng.in/ ml-wp Wednesday, Mar 7 11:00 a.m. CST, 12:00 p.m. EST, 9:00 a.m. PST, 17:00 UTC/GMT