Weitere ähnliche Inhalte Ähnlich wie Best Practices in Plugin Development (WordCamp Seattle) (20) Mehr von andrewnacin (18) Kürzlich hochgeladen (20) Best Practices in Plugin Development (WordCamp Seattle)8. class
Nacin_Project
{
static
$instance;
function
__construct()
{
self::$instance
=
$this;
}
}
new
Nacin_Project;
9. class
Nacin_Project
{
function
__construct()
{
add_action(
'init',
array(
$this,
'init'
)
);
}
function
init()
{
//
Add
hooks
here
}
}
11. 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);
13. And what
about:
§ Cookies § HTTP Auth
§ Headers § POST
§ Proxies § Timeouts
15. Pick a card, any card
cURL
streams
fopen
fsockopen
HTTP extension
18. Functions designed
for filters
__return_true();
__return_false();
__return_zero();
__return_empty_array();
21. URLs
Bad: get_option('home');
Better: get_bloginfo('url');
Yes:
home_url();
22. URLs
Bad: get_option
Better: ('siteurl');
Yes:
get_bloginfo
('wpurl');
site_url();
24. Bad:
get_bloginfo('url')
.
'wp-‐content/plugins/nacin/a.php'
Better:
WP_CONTENT_URL
.
'/plugins/nacin/a.php'
Yes:
plugins_url(
'a.php',
__FILE__
)
25. 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'
);
26. Post Types Settings
Taxonomies Capabilities
Widgets Templates
Shortcodes Query
Options i18n/L10n
Transients Admin Menus
Cache Meta Boxes
Cron Multisite
Formatting Updates
HTTP Filesystem
Embeds Admin Bar
27. Other nifty helpers
download_url(
$target
);
unzip_file(
$file,
$to
);
wp_handle_sideload(
$file
);
28. register_activation_hook(
__FILE__,
'my_activation_hook');
Ready your plugin (add_option)
Set and flush rewrite rules
Modify roles/capabilities
29. register_deactivation_hook(
__FILE__,
'my_deactivation_hook');
Restore and flush rewrite rules
Restore roles/capabilities
But don’t remove your options
31. 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.
38. Authentication
vs. Intention
Nonces for CSRF
protection. But check
capabilities too.
43. Please…
Read and
follow.
the coding
standards.
44. “
I love the
feeling
I get from my work
being used by
30 million .