SlideShare a Scribd company logo
DOING THINGS THE

WordPress
             WAY


     By Matt Wiebe
  Theme Designer, Plugin Developer
Soma Design – http://somadesign.ca/
       Twitter – @mattwiebe
BRIEFLY ABOUT ME
THE ERUDITE
EVENTS CALENDAR PRO
EVENTS CALENDAR PRO
THE WP WAY
THE WP WAY
★ Loading JS/CSS
THE WP WAY
★ Loading JS/CSS
★ HTTP Requests
THE WP WAY
★ Loading JS/CSS
★ HTTP Requests
★ JSON
THE WP WAY
★ Loading JS/CSS
★ HTTP Requests
★ JSON
★ Email
THE WP WAY
★ Loading JS/CSS
★ HTTP Requests
★ JSON
★ Email
★ Query Arguments
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"
/>
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
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
LOADING JS/CSS
wp_enqueue_script(
'script‐name',

'http://path/to/script.js',

array('jquery'),
'1.0',
true
);



Script URL. Can be external.
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)
LOADING JS/CSS
wp_enqueue_script(
'script‐name',

'http://path/to/script.js',

array('jquery'),
'1.0',
true
);



(Optional) Version #.
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
/>
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'
);
LOADING JS/CSS
//
Similar
for
styles

wp_enqueue_style(
'style‐name',
'http://
path/to/style.css',
array('deps'),
'1.0',

'media
type');
HTTP REQUESTS
//
How
does
this
curl
$h|†
work
again?
HTTP REQUESTS
//
How
does
this
curl
$h|†
work
again?

$c
=
curl_init();
curl_setop('ohcrapicantremember');
HTTP REQUESTS
//
How
does
this
curl
$h|†
work
again?

$c
=
curl_init();
curl_setop('ohcrapicantremember');

//
Screw
it
HTTP REQUESTS

★ Not all PHP environments have
cURL installed
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…
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
HTTP REQUESTS
//
HTTP
GET
request
$get
=
wp_remote_get($url);
HTTP REQUESTS
//
HTTP
GET
request
$get
=
wp_remote_get($url);

//
HTTP
POST
request
$post
=
wp_remote_post($url,
$args);
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
);
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
);
JSON
JSON
★ JSON (JavaScript Object Notation)
is the preferred way to exchange data
JSON
★ JSON (JavaScript Object Notation)
is the preferred way to exchange data
★ PHP 5.2 has json_encode() and
json_decode() functions
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
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)
EMAIL
EMAIL
★ Sending email sucks
EMAIL
★ Sending email sucks
★ WP's mail sending functionality
makes it suck less
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
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
);
QUERY ARGUMENTS
QUERY ARGUMENTS
//
already
have
$some_url
defined
//
want
to
add
a
query
string
QUERY ARGUMENTS
//
already
have
$some_url
defined
//
want
to
add
a
query
string

$some_url
.=
"?get=something+awesome";
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
"?"
?
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
);
QUERY ARGUMENTS
//
awesomer
syntax
QUERY ARGUMENTS
//
awesomer
syntax
$params
=
array(
 "get"
=>
"something
awesome",
 "for"
=>
"Matt",
 "because"
=>
"He's
awesome"
);
QUERY ARGUMENTS
//
awesomer
syntax
$params
=
array(
 "get"
=>
"something
awesome",
 "for"
=>
"Matt",
 "because"
=>
"He's
awesome"
);

$url
=
add_query_arg(
$params,
$url
);
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
BONUS ROUND
//
can't
remember
if
your
URL
has
//
a
slash
at
the
end?

$slashed_url
=
trailingslashit($url);

//
No
slashes

$unslashed_url
=
untrailingslashit($url);
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);
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
);
}
NOW YOU’LL DO THINGS THE

WordPress WAY
THANKS!
     By Matt Wiebe
  Theme Designer, Plugin Developer
Soma Design – http://somadesign.ca/
       Twitter – @mattwiebe

More Related Content

Doing Things the WordPress Way