February 2021 Developer Meetup

We had a great Meetup in February! Thank you to all that attended and participated. Ryan and Teri did a joint presentation on creating a plugin. Ryan live-coded a plugin while Teri described what was happening. Then, we opened it up to a group conversation about best practices and plugin development advice.

Here is the outline we used, as well as resources mentioned during the presentation. At the end, we’ve included the full plugin. Finally, other resources mentioned during the Meetup.

  1. Just like a theme, a plugin needs very little to get bootstrapped: Header Requirements | Plugin Developer Handbook | WordPress Developer Resources
  2. Doesn’t need a folder, but that’s good for organization
    1. Create a folder and file for your plugin.
    2. Add the comment
  3. Anything you would put in your `functions.php` you could move to a plugin pretty much with no work
    1. If you are referencing the path to the theme, you’ll have to update those.
    2. Referencing plugin directory: plugin_dir_path( __FILE__ ) – we’ll see an example of this later
  4. Build out a full comment block to bootstrap plugin.
  5. Add a CPT: Custom Post Types | Learn WordPress
    1. Add additional features for our CPT
      1. Preview for CPT
        add_filter( 'preview_post_link', 'cpt_preview_query_vars', 10, 2 );
        function cpt_preview_query_vars( $link, $post ) {
        	if ( 'portfolio_item' === $post->post_type ) {
        		return add_query_arg( [ 'post_type' => 'portfolio_item' ], $link );
        	}
        	return $link;
        }
        
      2. Custom single_template for CPT
        add_filter( 'single_template', 'load_cpt_template' ), 10 );
        add_filter( 'page_template', 'load_cpt_template' ), 10 );
        function load_cpt_template( $template ) {
        	global $post;
        
        	if ( 'portfolio_item' === $post->post_type && 'portfolio_item' !== locate_template( array( 'single-portfolio_item.php' ) ) ) {
        		return plugin_dir_path( __FILE__ ) . 'single-portfolio_item.php';
        	}
        	return $template;
        }
        
  6. Change the post excerpt length
    add_filter( 'excerpt_length', function($length) {
    	return 20;
    } );
    
  7. Move plugin to Must Use (MU)
    1. Create mu-plugins
    2. Add load.php
      1. Must Use Plugins
      2. Manually load plugin file
  8. Lessons Learned/Advice
    1. Know what you want to create before you start – and plan it out. Don’t just start coding – unless you don’t mind refactoring later.
    2. Follow WP coding standards and best practices for plugins
    3. Functionality goes in plugin or child theme? Depends on your situation and needs
    4. We’d love to open it up to you now to share any other lessons learned or advice and then we will move on to questions/open discussion after that.

Full plugin code:

<?php
/**
 * Plugin Name:       My Basics Plugin
 * Description:       Does some cool stuff.
 * Version:           1.0.0
 * Requires at least: 5.6
 * Requires PHP:      7.3
 * Author:            Ryan Tvenge
 * Author URI:        https://hoverboardstudios.com
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       my-plugin
 */

//adding our own portfolio item
function mp_my_custom_portfolio_item() {
  $labels = array(
    'name'               => _x( 'Portfolio items', 'post type general name' ),
    'singular_name'      => _x( 'Portfolio item', 'post type singular name' ),
    'menu_name'          => 'Portfolio'
  );
    $args = array(
    'labels'        => $labels,
    'description'   => 'Holds our custom portfolio items',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
    'show_in_rest'  => true,
  );
  register_post_type( 'portfolio_item', $args );
}
add_action( 'init', 'mp_my_custom_portfolio_item' );

// allow preview of CPT.
add_filter( 'preview_post_link', 'cpt_preview_query_vars', 10, 2 );
function cpt_preview_query_vars( $link, $post ) {
  if ( 'portfolio_item' === $post->post_type ) {
    return add_query_arg( [ 'post_type' => 'portfolio_item' ], $link );
  }
  return $link;
}

/* Filter the single_template with our custom function. */
add_filter( 'single_template', 'load_cpt_template', 10 );
add_filter( 'page_template', 'load_cpt_template', 10 );
function load_cpt_template( $template ) {
  global $post;

  if ( 'portfolio_item' === $post->post_type && 'portfolio_item' !== locate_template( array( 'single-portfolio_item.php' ) ) ) {
    return plugin_dir_path( __FILE__ ) . 'single-portfolio_item.php';
  }
  return $template;
}

add_filter( 'excerpt_length', function($length) {
  return 20;
} );

Other resources mentioned:

Share this!