4. Me (developer)
Writing code without an API
More complex and scattered code
When the app changes, my code breaks!
Usually quite “hacky”
Application
5. Me (developer)
Writing code using an API
Simple and standardized code
API
When the app changes, the API takes
care of it, your code doesn’t break!
Lower barrier for entry
Application
6. A Real World Example
Fetch posts in WordPress without an API
Work out the query you need, check the database?
SELECT ID FROM wp_posts WHERE post_type = 'post' AND post_status =
'publish' ORDER BY post_date DESC LIMIT 20
Loop the results manually
foreach ( (array) $posts as $post ) ....
What about security? Oh dear...
wp_esc() ... wp_filter_kses() ??
7. A Real World Example
Fetch posts in WordPress without an API
If the database schema changes in a new version of
WordPress, your code breaks.
If the post fields change in a new version of WordPress,
your code knows nothing about them.
Your code is hacker fodder.
8. A Real World Example
Fetch posts in WordPress using the API
Fetch the posts you need, who cares about the database?
get_posts( ‘numberposts=20’ );
Loop the results using the built in WordPress loop
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
Security? No worries.
the_content(); the_title(); the_tags();
9. A Real World Example
Fetch posts in WordPress using the API
No need to worry about database changes, get_posts() will
update in new versions of WordPress.
If new post fields are available, you can use them in your
code using new template tags.
Your code is safe.... hopefully. :)
10. The WordPress API is Everywhere
wp_authenticate()
get_posts()
WP_Widget
get_usermeta()
add_filter()
add_action()
current_user_can()
update_usermeta()