Doing Things the WordPress Way

4,073 views

Published on

Be a better, stronger, more productive WP web developer by taking advantage of the WordPress way of doing things.

Published in: Technology
0 Comments
7 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,073
On SlideShare
0
From Embeds
0
Number of Embeds
216
Actions
Shares
0
Downloads
29
Comments
0
Likes
7
Embeds 0
No embeds

No notes for slide

Doing Things the WordPress Way

  1. 1. DOING THINGS THE WordPress WAY By Matt Wiebe Theme Designer, Plugin Developer Soma Design – http://somadesign.ca/ Twitter – @mattwiebe
  2. 2. BRIEFLY ABOUT ME
  3. 3. THE ERUDITE
  4. 4. EVENTS CALENDAR PRO
  5. 5. EVENTS CALENDAR PRO
  6. 6. THE WP WAY
  7. 7. THE WP WAY ★ Loading JS/CSS
  8. 8. THE WP WAY ★ Loading JS/CSS ★ HTTP Requests
  9. 9. THE WP WAY ★ Loading JS/CSS ★ HTTP Requests ★ JSON
  10. 10. THE WP WAY ★ Loading JS/CSS ★ HTTP Requests ★ JSON ★ Email
  11. 11. THE WP WAY ★ Loading JS/CSS ★ HTTP Requests ★ JSON ★ Email ★ Query Arguments
  12. 12. LOADING JS/CSS <script
type="text/javascript"
src="path/to/script.js"
/> <link
rel="stylesheet"
href="path/to/style.css"
 type="text/css"
media="all"
/>
  13. 13. LOADING JS/CSS <script
type="text/javascript"
src="path/to/script.js"
/> <link
rel="stylesheet"
href="path/to/style.css"
 type="text/css"
media="all"
/> NEVER AGAIN
  14. 14. LOADING JS/CSS wp_enqueue_script(
'script‐name',
 'http://path/to/script.js',
 array('jquery'),
'1.0',
true
); Script ID – tells WP when to load, helps track dependencies
  15. 15. LOADING JS/CSS wp_enqueue_script(
'script‐name',
 'http://path/to/script.js',
 array('jquery'),
'1.0',
true
); Script URL. Can be external.
  16. 16. LOADING JS/CSS wp_enqueue_script(
'script‐name',
 'http://path/to/script.js',
 array('jquery'),
'1.0',
true
); (Optional) Dependencies. Reference the Script IDs scripts that should be loaded before this one. (WP has many built-in)
  17. 17. LOADING JS/CSS wp_enqueue_script(
'script‐name',
 'http://path/to/script.js',
 array('jquery'),
'1.0',
true
); (Optional) Version #.
  18. 18. LOADING JS/CSS wp_enqueue_script(
'script‐name',
 'http://path/to/script.js',
 array('jquery'),
'1.0',
true
); (Optional) Load in Footer. Defaults to false, which loads in <head
/>
  19. 19. LOADING JS/CSS //
Or,
register
first,
enqueue
later wp_register_script(
'script‐name',
 'http://path/to/script.js',
 array('jquery'),
'1.0',
true
); wp_enqueue_script(
'script‐name'
);
  20. 20. LOADING JS/CSS //
Similar
for
styles wp_enqueue_style(
'style‐name',
'http:// path/to/style.css',
array('deps'),
'1.0',
 'media
type');
  21. 21. HTTP REQUESTS //
How
does
this
curl
$h|†
work
again?
  22. 22. HTTP REQUESTS //
How
does
this
curl
$h|†
work
again? $c
=
curl_init(); curl_setop('ohcrapicantremember');
  23. 23. HTTP REQUESTS //
How
does
this
curl
$h|†
work
again? $c
=
curl_init(); curl_setop('ohcrapicantremember'); //
Screw
it
  24. 24. HTTP REQUESTS ★ Not all PHP environments have cURL installed
  25. 25. HTTP REQUESTS ★ Not all PHP environments have cURL installed ★ The WP_Http class Standardizes the HTTP requests for WordPress. Handles cookies, gzip encoding and decoding, chunk decoding…
  26. 26. HTTP REQUESTS ★ Not all PHP environments have cURL installed ★ The WP_Http class Standardizes the HTTP requests for WordPress. Handles cookies, gzip encoding and decoding, chunk decoding… ★ wp_remote_* functions provided
  27. 27. HTTP REQUESTS //
HTTP
GET
request $get
=
wp_remote_get($url);
  28. 28. HTTP REQUESTS //
HTTP
GET
request $get
=
wp_remote_get($url); //
HTTP
POST
request $post
=
wp_remote_post($url,
$args);
  29. 29. HTTP REQUESTS //
HTTP
GET
request $get
=
wp_remote_get($url); //
HTTP
POST
request $post
=
wp_remote_post($url,
$args); //
Just
the
response
body $body
=
wp_remote_retrieve_body(
$get
);
  30. 30. HTTP REQUESTS //
HTTP
GET
request $get
=
wp_remote_get($url); //
HTTP
POST
request $post
=
wp_remote_post($url,
$args); //
Just
the
response
body $body
=
wp_remote_retrieve_body(
$get
); //
Just
the
response
header $h
=
wp_remote_retrieve_header(
$get
);
  31. 31. JSON
  32. 32. JSON ★ JSON (JavaScript Object Notation) is the preferred way to exchange data
  33. 33. JSON ★ JSON (JavaScript Object Notation) is the preferred way to exchange data ★ PHP 5.2 has json_encode() and json_decode() functions
  34. 34. JSON ★ JSON (JavaScript Object Notation) is the preferred way to exchange data ★ PHP 5.2 has json_encode() and json_decode() functions ★ Many WP users are on earlier versions of PHP
  35. 35. JSON ★ JSON (JavaScript Object Notation) is the preferred way to exchange data ★ PHP 5.2 has json_encode() and json_decode() functions ★ Many WP users are on earlier versions of PHP ★ WP has a backwards compatibility layer for PHP < 5.2 (since 2.9)
  36. 36. EMAIL
  37. 37. EMAIL ★ Sending email sucks
  38. 38. EMAIL ★ Sending email sucks ★ WP's mail sending functionality makes it suck less
  39. 39. EMAIL ★ Sending email sucks ★ WP's mail sending functionality makes it suck less ★ Centralizes all email so that it can be overridden/manipulated by plugins
  40. 40. EMAIL /** 
*
$to
email
address(es)
to
send
to. 
*
$subject
Email
subject 
*
$message
Message
contents 
*
$headers
Optional.
Additional
headers. 
*
$attachments
Optional.
Files
to 
*
attach. 
*/ $success
=
wp_mail(
$to,
$subject,
 $message,
$headers,
$attachments
);
  41. 41. QUERY ARGUMENTS
  42. 42. QUERY ARGUMENTS //
already
have
$some_url
defined //
want
to
add
a
query
string
  43. 43. QUERY ARGUMENTS //
already
have
$some_url
defined //
want
to
add
a
query
string $some_url
.=
"?get=something+awesome";
  44. 44. QUERY ARGUMENTS //
already
have
$some_url
defined //
want
to
add
a
query
string $some_url
.=
"?get=something+awesome"; //
what
if
$some_url
already
had
a
"?"
?
  45. 45. QUERY ARGUMENTS //
already
have
$some_url
defined //
want
to
add
a
query
string $some_url
.=
"?get=something+awesome"; //
what
if
$some_url
already
had
a
"?"
? //
add_query_arg()
to
the
rescue $some_url
=
add_query_arg(
"get",
 "something
awesome",
$some_url
);
  46. 46. QUERY ARGUMENTS //
awesomer
syntax
  47. 47. QUERY ARGUMENTS //
awesomer
syntax $params
=
array( "get"
=>
"something
awesome", "for"
=>
"Matt", "because"
=>
"He's
awesome" );
  48. 48. QUERY ARGUMENTS //
awesomer
syntax $params
=
array( "get"
=>
"something
awesome", "for"
=>
"Matt", "because"
=>
"He's
awesome" ); $url
=
add_query_arg(
$params,
$url
);
  49. 49. QUERY ARGUMENTS //
awesomer
syntax $params
=
array( "get"
=>
"something
awesome", "for"
=>
"Matt", "because"
=>
"He's
awesome" ); $url
=
add_query_arg(
$params,
$url
); //
http://amazon.com/?get=something +awesome&for=Matt&because=He's+awesome
  50. 50. BONUS ROUND //
can't
remember
if
your
URL
has //
a
slash
at
the
end? $slashed_url
=
trailingslashit($url); //
No
slashes $unslashed_url
=
untrailingslashit($url);
  51. 51. BONUS ROUND /**
 
*
merging
arguments
and
defaults? 
*
$defaults
array
of
defaults 
*
$args
accepts
an
array
or
query 
*
string.
eg
cat_name=moo&tag=cow 
*
@return
an
array */ $args
=
wp_parse_args(
$defaults,
$args);
  52. 52. BONUS ROUND //
Override
WP's
jQuery
with
Google
CDN
 add_action(
'wp_enqueue_scripts',
 'sd_google_cdn'); function
sd_google_cdn()
{ wp_deregister_script(
'jquery'
); wp_enqueue_script(
'jquery',
'http:// ajax.googleapis.com/ajax/libs/jquery/ 1.4.4/jquery.min.js',
array(),
 '1.4.4',
true
); }
  53. 53. NOW YOU’LL DO THINGS THE WordPress WAY
  54. 54. THANKS! By Matt Wiebe Theme Designer, Plugin Developer Soma Design – http://somadesign.ca/ Twitter – @mattwiebe

×