Upgrade to Pro — share decks privately, control downloads, hide ads and more …

You, Too, Can Write a Plugin

You, Too, Can Write a Plugin

The idea of writing a WordPress plugin can be intimidating if you've never done it before. It's not nearly as hard as you think! This presentation walks you through writing a simple plugin, and goes over some tips on writing your own in the future.

Slide 4 is a video showing me searching for "WordPress plugin header" in Google, copying & pasting the example header into a plugin file, and modifying the comment text to make my own plugin.

Avatar for Alison Barrett

Alison Barrett

September 14, 2013
Tweet

More Decks by Alison Barrett

Other Decks in Programming

Transcript

  1. GOALS ✤ Put an author bio at the end of

    every post ✤ Use the bio field from the author's profile ✤ Show the author's gravatar ✤ Only show on single posts
  2. <?php /* Plugin  Name:  Super  Amazing  Author  Bio  Appender Plugin

     URI:  http://example.com/author-­‐bio-­‐appender/ Description:  Display  an  author  bio  at  the  end  of  each  post Author:  Alison  Barrett Version:  1.0 Author  URI:  http://alisothegeek.com Text  Domain:  author-­‐bio-­‐appender Domain  Path:  /lang */
  3. function  saaba_insert_bio(  $content  )  {        global  $post;

           $bio_text  =  wpautop(  get_user_meta(  $post-­‐>post_author,  'description',  true  )  );        $author_bio  =  '<div  class="saaba-­‐author-­‐bio">'  .  $bio_text  .  '</div>';        $content  .=  $author_bio;        return  $content; } add_filter(  'the_content',  'saaba_insert_bio'  );
  4. ob_start(); ?> <div  class="saaba-­‐author-­‐bio">        <div  class="saaba-­‐gravatar">  

                 <?php  echo  $avatar;  ?>        </div>        <div  class="saaba-­‐bio-­‐text">                <?php  echo  $bio_text;  ?>        </div> </div> <?php $author_bio  =  ob_get_clean();
  5. ob_start(); ?> <div  class="saaba-­‐author-­‐bio">        <div  class="saaba-­‐gravatar">  

                 <?php  echo  $avatar;  ?>        </div>        <div  class="saaba-­‐bio-­‐text">                <?php  echo  $bio_text;  ?>        </div> </div> <?php $author_bio  =  ob_get_clean();
  6. function  saaba_styles()  {        $plugin_url  =  plugins_url(  '',

     __FILE__  );        wp_enqueue_style(  'saaba',  $plugin_url  .  '/saaba-­‐style.css'  ); } add_action(  'wp_enqueue_scripts',  'saaba_styles'  );
  7. .saaba-­‐author-­‐bio  {        background:  #ffffe0;      

     border:  1px  solid  #ebe5a6;        padding:  10px;        margin:  20px  auto; } .saaba-­‐gravatar  {        float:  left;        margin-­‐right:  10px; }
  8. $bio_text        =  wpautop(  get_user_meta(  $post-­‐>post_author,  'description',  true

     )  ); $avatar            =  get_avatar(  $post-­‐>post_author,  72  ); $author_name  =  get_user_meta(  $post-­‐>post_author,  'nickname',  true  );
  9. <div  class="saaba-­‐author-­‐bio">        <div  class="saaba-­‐gravatar">      

             <?php  echo  $avatar;  ?>        </div>        <div  class="saaba-­‐bio-­‐text">                <h4>About  <?php  echo  $author_name;  ?></h4>                <?php  echo  $bio_text;  ?>        </div> </div>
  10. function  saaba_insert_bio(  $content  )  {        if  (

     !  is_single()  )  {                return  $content;        }        global  $post;        $bio_text        =  wpautop(  get_user_meta(  $post-­‐>post_author,  'description',  true  )  );        $avatar            =  get_avatar(  $post-­‐>post_author,  72  );        $author_name  =  get_user_meta(  $post-­‐>post_author,  'nickname',  true  );
  11. WHAT WE LEARNED ✤ Use hooks in plugins to extend

    WordPress functionality. ✤ Enqueue your stylesheets (same goes for Javascript files).
  12. PLUGIN FILE STRUCTURE ✤ Folder and main plugin file should

    have the same name (which should match the plugin slug). ✤ If there are a lot of files, use subfolders!
  13. WHAT SHOULD MY PLUGIN DO? ✤ Unique need on a

    client site? It could be useful to more people. ✤ Something you want to do on your own blog? Others might want to do it too! ✤ Want to learn something new? Make it a plugin & release it.
  14. SOME OF MY FIRST PLUGINS ✤ Sidebar widget that listed

    pages in different colored links ✤ Wowhead search engine sidebar widget ✤ Store locator using Google Maps and custom database tables