Slide 1

Slide 1 text

Develop Plugins Like a Pro

Slide 2

Slide 2 text

Alison Barrett Senior Web Engineer at 10up @alisothegeek

Slide 3

Slide 3 text

Agenda 1. Make a plugin 2. Actions & filters 3. Scripts & styles 4. Make a plugin 5. Custom queries 6. Widgets API 7. Make a plugin

Slide 4

Slide 4 text

Not Covered ‣ PHP basics ‣ Version control ‣ Internationalization ‣ WordPress.org plugin submission

Slide 5

Slide 5 text

You Should Have ‣ Local development environment ‣ IDE or text editor open to your plugins folder ‣ Browser open to the Plugins admin page

Slide 6

Slide 6 text

First Plugin Hello WordPress

Slide 7

Slide 7 text

Goals ‣ Show up on the Plugins page

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

Actions and Filters

Slide 15

Slide 15 text

Actions + Filters = Hooks

Slide 16

Slide 16 text

"Hook" into WordPress core functionality

Slide 17

Slide 17 text

action = do something ! filter = do something to something else

Slide 18

Slide 18 text

function  my_function()  {      …   }   add_action(  'init',  'my_function'  );

Slide 19

Slide 19 text

function  my_other_function(  $content  )  {      …      return  $content;   }   add_filter(  'the_content',  'my_other_function'  );

Slide 20

Slide 20 text

function  append_author_bio(  $content  )  {      $bio  =  get_the_author_meta(  'description'  );      return  $content  .  $bio;   }   add_filter(  'the_content',  'append_author_bio'  );

Slide 21

Slide 21 text

Scripts and Styles

Slide 22

Slide 22 text

Slide 23

Slide 23 text

wp_head() fires the wp_head action

Slide 24

Slide 24 text

function  myplugin_scripts()  {     ?>            

Slide 25

Slide 25 text

‣ another plugin might load jQuery ‣ this might conflict with the WordPress version of jQuery   ‣ that might not be where wp-content is

Slide 26

Slide 26 text

never hardcode Javascript or CSS files in your plugin

Slide 27

Slide 27 text

enqueue them instead

Slide 28

Slide 28 text

action: wp_enqueue_scripts ! function: wp_enqueue_script wp_enqueue_style

Slide 29

Slide 29 text

function  myplugin_scripts()  {     wp_enqueue_script(  'jquery'  );     wp_enqueue_script(  'myplugin',  plugins_url(  'stuff.js',  __FILE__  )  );   }   add_action(  'wp_enqueue_scripts',  'myplugin_scripts'  );   !

Slide 30

Slide 30 text

function  myplugin_scripts()  {     wp_enqueue_script(  'jquery'  );     wp_enqueue_script(  'myplugin',  plugins_url(  'stuff.js',  __FILE__  )  );   !   wp_enqueue_style(  'myplugin',  plugins_url(  'stuff.css',  __FILE__  )  );   }   add_action(  'wp_enqueue_scripts',  'myplugin_scripts'  );

Slide 31

Slide 31 text

plugins_url(    'myplugin.css',    __FILE__   ); path within plugin file in plugin root

Slide 32

Slide 32 text

wp_enqueue_script & wp_enqueue_style take 5 parameters

Slide 33

Slide 33 text

wp_enqueue_script(     'myplugin',     plugins_url(  'myplugin.js',  __FILE__  ),     array(  'jquery'  ),     '1.0.0',     true   ); unique ID URL to file dependencies version load in footer unique ID

Slide 34

Slide 34 text

wp_enqueue_style(     'myplugin',     plugins_url(  'myplugin.css',  __FILE__  ),     array(),     '1.0.0',     'screen'   ); unique ID URL to file dependencies version media

Slide 35

Slide 35 text

Benefits of wp_enqueue_script ‣ Each file is only loaded once ‣ Dependencies are always loaded first and in the right order ‣ CSS files are called next to each other so they can load in parallel

Slide 36

Slide 36 text

Second Plugin Authorial

Slide 37

Slide 37 text

Goals ‣ Add an author bio to the end of every post ‣ Use the author bio field from the user's profile ‣ Show the author's Gravatar

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

function  authorial_enqueue_style()  {       wp_enqueue_style(           'authorial',           plugins_url(  'authorial.css',  __FILE__  ),           array(),           '1.0.0'       );   }   add_action(  'wp_enqueue_scripts',  'authorial_enqueue_style'  );

Slide 42

Slide 42 text

function  authorial_add_bio(  $content  )  {       $bio  =  get_the_author_meta(  'description'  );       return  $content  .  $bio;   }   add_filter(  'the_content',  'authorial_add_bio'  );

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

function  authorial_add_bio(  $content  )  {       $bio  =  wpautop(  get_the_author_meta(  'description'  )  );       return  $content  .  $bio;   }   add_filter(  'the_content',  'authorial_add_bio'  );

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Bonus Lesson: ! Output Buffering

Slide 47

Slide 47 text

echo  '
';   echo  '

About  the  Author

';   echo  $bio;   echo  '
';

Slide 48

Slide 48 text

     

About  the  Author

       

Slide 49

Slide 49 text

ob_start();   ?>  
     

About  the  Author

       
 

Slide 50

Slide 50 text

function  authorial_add_bio(  $content  )  {       $bio  =  wpautop(  get_the_author_meta(  'description'  )  );       ob_start();   ?>  
     

About  the  Author

       
 

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

$bio  =  wpautop(  get_the_author_meta(  'description'  )  );   $name  =  get_the_author_meta(  'display_name'  );   ob_start();

Slide 53

Slide 53 text

     

About  

       

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

Goals ✓ Add an author bio to the end of every post ✓ Use the author bio field from the user's profile ‣ Show the author's Gravatar

Slide 56

Slide 56 text

$bio  =  wpautop(  get_the_author_meta(  'description'  )  );   $name  =  get_the_author_meta(  'display_name'  );   $avatar  =  get_avatar(  get_the_author_meta(  'ID'  ),  100  );   ! ob_start();

Slide 57

Slide 57 text

     

About  

             

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

.authorial-­‐bio  {     padding:  20px;     margin:  20px  0;     overflow:  hidden;     background:  #f5f5f5;     border:  3px  solid  #e5e5e5;   }   ! .authorial-­‐bio  .avatar  {     float:  left;     margin:  20px  20px  10px  0;   }   ! .authorial-­‐bio  h3  {     margin:  0;   }   ! .authorial-­‐bio  p  {     margin:  10px  0  0;   } authorial.css

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

Custom Queries

Slide 62

Slide 62 text

The Loop

Slide 63

Slide 63 text

query_posts

Slide 64

Slide 64 text

query_posts(  'posts_per_page=3'  );   query_posts(  'cat=13'  );   query_posts(  'orderby=title&order=ASC'  );

Slide 65

Slide 65 text

WP_Query

Slide 66

Slide 66 text

$my_query  =  new  WP_Query(  'orderby=title&order=ASC'  );   ! $my_query  =  new  WP_Query(  array(    'orderby'  =>  'title',    'order'      =>  'ASC',   )  );

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

if  (  have_posts()  )  :      while  (  have_posts()  )  :          the_post();

Slide 69

Slide 69 text

if  (  $my_query-­‐>have_posts()  )  :      while  (  $my_query-­‐>have_posts()  )  :          $my_query-­‐>the_post();

Slide 70

Slide 70 text

Widgets API

Slide 71

Slide 71 text

WP_Widget

Slide 72

Slide 72 text

class  My_Widget  extends  WP_Widget  {   !     public  function  __construct()  {           //  widget  name,  ID,  etc.       }   !     public  function  widget(  $args,  $instance  )  {           //  outputs  the  content  of  the  widget       }   !     public  function  form(  $instance  )  {           //  outputs  the  options  form  on  admin       }   !     public  function  update(  $new_instance,  $old_instance  )  {           //  processes  widget  options  to  be  saved       }   ! }

Slide 73

Slide 73 text

function  __construct()  {       parent::__construct(           'my_widget',           'My  Widget',           array(               'description'  =>  'A  Foo  Widget',           )       );   } unique ID title on Widgets page displays under title on Widgets page

Slide 74

Slide 74 text

public  function  widget(  $args,  $instance  )  {       $title  =  apply_filters(  'widget_title',  $instance['title']  );   !     echo  $args['before_widget'];   !     if  (  !  empty(  $title  )  )  {           echo  $args['before_title']  .  $title  .  $args['after_title'];       }   !     //  Widget  content  here   !     echo  $args['after_widget'];   } $args is set when the sidebar is registered $instance is data from the widget form in the admin

Slide 75

Slide 75 text

public  function  form(  $instance  )  {       if  (  isset(  $instance[  'title'  ]  )  )  {           $title  =  $instance[  'title'  ];       }       else  {           $title  =  'New  title';       }       ?>      

        get_field_id(  'title'  );  ?>">Title:         get_field_id(  'title'  );  ?>"                      name="get_field_name(  'title'  );  ?>"                      value=""  />      

     

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

public  function  update(  $new_instance,  $old_instance  )  {       $instance  =  array();             $instance['title']  =  strip_tags(  $new_instance['title']  );   !     return  $instance;   }

Slide 78

Slide 78 text

action: widgets_init ! function: register_widget

Slide 79

Slide 79 text

function  register_my_widget()  {          register_widget(  'My_Widget'  );   }   add_action(  'widgets_init',  'register_my_widget'  ); class name

Slide 80

Slide 80 text

Third Plugin Hot Topics

Slide 81

Slide 81 text

Goals ‣ Add a widget that lists popular posts ‣ Use comment count to determine popularity ‣ Allow user to change the number of posts listed ‣ Allow user to choose whether to display comment count

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

function  hot_topics_register_widget()  {       require_once  __DIR__  .  '/class-­‐hot-­‐topics-­‐widget.php';       register_widget(  'Hot_Topics_Widget'  );   }   add_action(  'widgets_init',  'hot_topics_register_widget'  );

Slide 86

Slide 86 text

class  Hot_Topics_Widget  extends  WP_Widget  {   ! } class-hot-topics-widget.php

Slide 87

Slide 87 text

public  function  __construct()  {       parent::__construct(           'hot_topics',           'Hot  Topics',           array(  'description'  =>  'A  list  of  your  most  discussed  posts',  )       );   }

Slide 88

Slide 88 text

public  function  widget(  $args,  $instance  )  {       $title  =  apply_filters(  'widget_title',  $instance['title']  );   !     echo  $args['before_widget'];   !     if  (  !  empty(  $title  )  )           echo  $args['before_title']  .  $title  .  $args['after_title'];   !     echo  $args['after_widget'];   }

Slide 89

Slide 89 text

public  function  widget(  $args,  $instance  )  {       $title  =  apply_filters(  'widget_title',  $instance['title']  );       $hot_topics_query  =  new  WP_Query(  array(           'orderby'                          =>  'comment_count',   !     )  );       echo  $args['before_widget'];   !     if  (  !  empty(  $title  )  )           echo  $args['before_title']  .  $title  .  $args['after_title'];   !     echo  $args['after_widget'];   }

Slide 90

Slide 90 text

public  function  widget(  $args,  $instance  )  {       $title  =  apply_filters(  'widget_title',  $instance['title']  );       $hot_topics_query  =  new  WP_Query(  array(           'orderby'                          =>  'comment_count',           'ignore_sticky_posts'  =>  true,       )  );       echo  $args['before_widget'];   !     if  (  !  empty(  $title  )  )           echo  $args['before_title']  .  $title  .  $args['after_title'];   !     echo  $args['after_widget'];   }

Slide 91

Slide 91 text

public  function  widget(  $args,  $instance  )  {       $title  =  apply_filters(  'widget_title',  $instance['title']  );       $hot_topics_query  =  new  WP_Query(  array(           'orderby'                          =>  'comment_count',           'ignore_sticky_posts'  =>  true,       )  );       if  (  $hot_topics_query-­‐>have_posts()  )  :           echo  $args['before_widget'];   !         if  (  !  empty(  $title  )  )               echo  $args['before_title']  .  $title  .  $args['after_title'];   !         echo  $args['after_widget'];       endif;   }

Slide 92

Slide 92 text

  if  (  $hot_topics_query-­‐>have_posts()  )  :           echo  $args['before_widget'];   !         if  (  !  empty(  $title  )  )               echo  $args['before_title']  .  $title  .  $args['after_title'];   !         echo  '
    ';           while(  $hot_topics_query-­‐>have_posts()  )  :               $hot_topics_query-­‐>the_post();               $comments  =  get_comment_count(  get_the_ID()  );               ?>              
  • ">                                          
  •               ';   !         echo  $args['after_widget'];       endif;

Slide 93

Slide 93 text

public  function  form(  $instance  )  {       if  (  isset(  $instance[  'title'  ]  )  )  {           $title  =  $instance[  'title'  ];       }       else  {           $title  =  'Hot  Topics';       }       ?>      

        get_field_id(  'title'  );  ?>">Title:         get_field_id(  'title'  );  ?>"                      name="get_field_name(  'title'  );  ?>"                      value=""  />      

     

Slide 94

Slide 94 text

public  function  update(  $new_instance,  $old_instance  )  {       $instance  =  array();             $instance['title']  =  strip_tags(  $new_instance['title']  );   !     return  $instance;   }

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

Goals ✓ Add a widget that lists popular posts ✓ Use comment count to determine popularity ‣ Allow user to change the number of posts listed

Slide 97

Slide 97 text

    if  (  isset(  $instance[  'title'  ]  )  )  {           $title  =  $instance[  'title'  ];       }       else  {           $title  =  'Hot  Topics';       }   !     if  (  isset(  $instance['number_of_posts']  )  )  {           $number_of_posts  =  (int)  $instance['number_of_posts'];       }  else  {           $number_of_posts  =  5;       } form()

Slide 98

Slide 98 text

   

        get_field_id(  'title'  );  ?>">Title:         get_field_id(  'title'  );  ?>"                      name="get_field_name(  'title'  );  ?>"                      value=""  />      

     

        get_field_id(  'number_of_posts'  );  ?>">Number  of  posts  to  show:         get_field_id(  'number_of_posts'  );  ?>"                      name="get_field_name(  'number_of_posts'  );  ?>"                      value=""                      size="3"  />      

Slide 99

Slide 99 text

public  function  update(  $new_instance,  $old_instance  )  {       $instance  =  array();             $instance['title']  =  strip_tags(  $new_instance['title']  );   !     if  (  !  empty(  $new_instance['number_of_posts']  )  )  {           $instance['number_of_posts']  =  (int)  $new_instance['number_of_posts'];       }  else  {           $instance['number_of_posts']  =  5;       }   !     return  $instance;   }

Slide 100

Slide 100 text

public  function  widget(  $args,  $instance  )  {       $title  =  apply_filters(  'widget_title',  $instance['title']  );       $number_of_posts    =  (int)  $instance['number_of_posts'];   !     $hot_topics_query  =  new  WP_Query(  array(           'orderby'                          =>  'comment_count',           'ignore_sticky_posts'  =>  true,           'posts_per_page'            =>  $number_of_posts,       )  );

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

Goals ✓ Add a widget that lists popular posts ✓ Use comment count to determine popularity ✓ Allow user to change the number of posts listed ‣ Allow user to choose whether to display comment count

Slide 104

Slide 104 text

   

        get_field_id(  'number_of_posts'  );  ?>">Number  of  posts  to  show:         get_field_id(  'number_of_posts'  );  ?>"                      name="get_field_name(  'number_of_posts'  );  ?>"                      value=""                      size="3"  />      

     

        get_field_id(  'show_comment_count'  );  ?>"                      name="get_field_name(  'show_comment_count'  );  ?>"                        />         get_field_id(  'show_comment_count'  );  ?>">Display  comment  count?      

Slide 105

Slide 105 text

public  function  update(  $new_instance,  $old_instance  )  {       $instance  =  array();             $instance['title']  =  strip_tags(  $new_instance['title']  );   !     if  (  !  empty(  $new_instance['number_of_posts']  )  )  {         $instance['number_of_posts']  =  (int)  $new_instance['number_of_posts'];       }  else  {         $instance['number_of_posts']  =  5;       }   !     if  (  !  empty(  $new_instance['show_comment_count']  )  )  {         $instance['show_comment_count']  =  true;       }  else  {         $instance['show_comment_count']  =  false;       }   !     return  $instance;   }

Slide 106

Slide 106 text

  if  (  $hot_topics_query-­‐>have_posts()  )  :           if  (  !  empty(  $instance['show_comment_count']  )  )  {               $show_comment_count  =  true;           }  else  {               $show_comment_count  =  false;           }   !         echo  $args['before_widget']; widget()

Slide 107

Slide 107 text

        echo  '
    ';           while(  $hot_topics_query-­‐>have_posts()  )  :               $hot_topics_query-­‐>the_post();               $comments  =  get_comment_count(  get_the_ID()  );               ?>              
  • ">                                          
  •               ';

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

Thank You aliso.me/plugins101 aliso.me/plugins101files ! @alisothegeek