Top Banner
Best Practices for Plugin Development Core Developer, WordPress.org Tech Ninja, Audrey Capital @nacin
47

Best Practices in Plugin Development (WordCamp Seattle)

May 08, 2015

Download

Technology

andrewnacin

My talk -- officially named "Y U NO CODE WELL" -- at WordCamp Seattle 2011 on best practices during plugin development. Find the video, as it provides some good context and conversation.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Best Practices in Plugin Development (WordCamp Seattle)

Best Practices for Plugin Development

Core Developer, WordPress.org Tech Ninja, Audrey Capital

@nacin

Page 2: Best Practices in Plugin Development (WordCamp Seattle)

how not to write a plugin

Page 3: Best Practices in Plugin Development (WordCamp Seattle)

ur doin it wrong

Page 4: Best Practices in Plugin Development (WordCamp Seattle)

DEVELOPERS: Y U NO CODE WELL

Page 5: Best Practices in Plugin Development (WordCamp Seattle)

One of these things is not like the others

lsp_add_scripts()  lsp_add_custom_meta()  lsp_create_html()  lsp_save_meta()  install()  

Page 6: Best Practices in Plugin Development (WordCamp Seattle)

function  nacin_project_func_name()  {}  

Page 7: Best Practices in Plugin Development (WordCamp Seattle)

class  Nacin_Project  {              function  __construct()  {}              function  init()  {}              function  activate()  {}  }  

Page 8: Best Practices in Plugin Development (WordCamp Seattle)

class  Nacin_Project  {            static  $instance;              function  __construct()  {                      self::$instance  =  $this;            }  }  new  Nacin_Project;  

Page 9: Best Practices in Plugin Development (WordCamp Seattle)

class  Nacin_Project  {            function  __construct()  {                      add_action(  'init',                          array(  $this,  'init'  )  );            }            function  init()  {                      //  Add  hooks  here            }  }  

Page 10: Best Practices in Plugin Development (WordCamp Seattle)

HTTP Make HTTP requests

Page 11: Best Practices in Plugin Development (WordCamp Seattle)

cURL example

Let’s fetch a URL: $ch  =  curl_init();  curl_setopt($ch,  CURLOPT_URL,  $url);  curl_setopt($ch,  CURLOPT_RETURNTRANSFER,  

true);  curl_setopt($ch,  CURLOPT_HEADER,  false);  Curl_setopt($ch,  CURLOPT_FOLLOWLOCATION,  

true);    $result  =  curl_exec($ch);  curl_close($ch);  

Page 12: Best Practices in Plugin Development (WordCamp Seattle)

var_dump(  function_exists(      'curl_init'  )  );  

bool(false)  

Page 13: Best Practices in Plugin Development (WordCamp Seattle)

And what about:

§ Cookies § Headers § Proxies

§ HTTP Auth § POST § Timeouts

Page 14: Best Practices in Plugin Development (WordCamp Seattle)

wp_remote_request(      $url,      $args  =  array()  );  

Page 15: Best Practices in Plugin Development (WordCamp Seattle)

Pick a card, any card

cURL streams

fopen fsockopen

HTTP extension

Page 16: Best Practices in Plugin Development (WordCamp Seattle)

2000 lines of work already done for you

Page 17: Best Practices in Plugin Development (WordCamp Seattle)

Helpers We make it so easy.

Page 18: Best Practices in Plugin Development (WordCamp Seattle)

Functions designed for filters

__return_true();  __return_false();  __return_zero();  __return_empty_array();  

Page 19: Best Practices in Plugin Development (WordCamp Seattle)

Serialization maybe_serialize();  maybe_unserialize();  is_serialized();  is_serialized_string();  

Page 20: Best Practices in Plugin Development (WordCamp Seattle)

Server helpers apache_mod_loaded($module);  got_mod_rewrite();  insert_with_markers();  extract_with_markers();  

Page 21: Best Practices in Plugin Development (WordCamp Seattle)

URLs

Bad: Better: Yes:  

get_option('home');  get_bloginfo('url');  home_url();  

Page 22: Best Practices in Plugin Development (WordCamp Seattle)

URLs

Bad: Better: Yes:  

get_option('siteurl');  

get_bloginfo('wpurl');  

site_url();  

Page 23: Best Practices in Plugin Development (WordCamp Seattle)

Bad: ABSPATH.  'wp-­‐content/plugins/nacin'  

Still bad: WP_CONTENT_DIR  .  'plugins/nacin'  

Better: WP_PLUGIN_DIR  .  '/nacin'  Yes: dirname(  __FILE__  )    

Page 24: Best Practices in Plugin Development (WordCamp Seattle)

Bad: get_bloginfo('url')        .  'wp-­‐content/plugins/nacin/a.php'  

Better: WP_CONTENT_URL  .  '/plugins/nacin/a.php'  

Yes: plugins_url(  'a.php',  __FILE__  )  

 

Page 25: Best Practices in Plugin Development (WordCamp Seattle)

Default Function Arguments

function  myfunc(  $args  =  array()  )  {    $defaults  =  array(        'first_arg'    =>  true,          'second_arg'  =>  'foo'  );      $args  =  wp_parse_args($args,  $defaults);  

 Even allows for query string calls:  nacin_my_func(  'first_arg=false'  );  

Page 26: Best Practices in Plugin Development (WordCamp Seattle)

Post Types Taxonomies

Widgets Shortcodes

Options Transients

Cache Cron

Formatting HTTP

Embeds

Settings Capabilities Templates

Query i18n/L10n

Admin Menus Meta Boxes

Multisite Updates

Filesystem Admin Bar

Page 27: Best Practices in Plugin Development (WordCamp Seattle)

Other nifty helpers

download_url(  $target  );  unzip_file(  $file,  $to  );  

wp_handle_sideload(  $file  );  

Page 28: Best Practices in Plugin Development (WordCamp Seattle)

register_activation_hook(  __FILE__,  'my_activation_hook');  

 Ready your plugin (add_option) Set and flush rewrite rules Modify roles/capabilities

Page 29: Best Practices in Plugin Development (WordCamp Seattle)

register_deactivation_hook(  __FILE__,  'my_deactivation_hook');  

 Restore and flush rewrite rules Restore roles/capabilities But don’t remove your options

Page 30: Best Practices in Plugin Development (WordCamp Seattle)

Uninstall hook

Clean  up  after  yourself.

Page 31: Best Practices in Plugin Development (WordCamp Seattle)

uninstall.php

if  (!defined('WP_UNINSTALL_PLUGIN'))    die();    delete_option('my_plugin_option');  

 There’s also a hook, like activation and

deactivation, but it has its caveats.

Page 32: Best Practices in Plugin Development (WordCamp Seattle)

But be considerate of my data.

Page 33: Best Practices in Plugin Development (WordCamp Seattle)

Here’s a secret…

I hate activation hooks.

Page 34: Best Practices in Plugin Development (WordCamp Seattle)

Use the right level of the API.

Page 35: Best Practices in Plugin Development (WordCamp Seattle)

Don’t make any assumptions. Ever.

Page 36: Best Practices in Plugin Development (WordCamp Seattle)

Friends don’t let friends use direct database queries.

Page 37: Best Practices in Plugin Development (WordCamp Seattle)

Security. Please. Please. Please.

Page 38: Best Practices in Plugin Development (WordCamp Seattle)

Authentication vs. Intention

Nonces for CSRF protection. But check

capabilities too.

Page 39: Best Practices in Plugin Development (WordCamp Seattle)

Plugins can do anything.

Page 40: Best Practices in Plugin Development (WordCamp Seattle)

Plugins can do anything.

Page 41: Best Practices in Plugin Development (WordCamp Seattle)

Be considerate of other plugins.

Page 42: Best Practices in Plugin Development (WordCamp Seattle)

Consult the code.

Documentation is nice, but…

Page 43: Best Practices in Plugin Development (WordCamp Seattle)

Please…

Read and follow.

the coding standards.

Page 44: Best Practices in Plugin Development (WordCamp Seattle)

“I love the feeling

I get from my work being used by 30 million .

Page 45: Best Practices in Plugin Development (WordCamp Seattle)

Benefit from lessons in development and user experience.

(Consultants, hint hint.)

Page 46: Best Practices in Plugin Development (WordCamp Seattle)

”I learned more in 3 months than I had learned in 3 years .

Page 47: Best Practices in Plugin Development (WordCamp Seattle)

Thanks!

@nacin