Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Upcoming SlideShare
What to Upload to SlideShare
What to Upload to SlideShare
Loading in …3
×
1 of 11

Building your first plugin

1

Share

Download to read offline

WordCamp Phoenix 2018 slides for Building Your First Plugin talk.

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Building your first plugin

  1. 1. Building Your First Plugin A COMPLETE NEWBIE’S GUIDE TO CREATING A PLUGIN SCOTT DELUZIO #wcphx @scottdeluzio
  2. 2. About me: Scott DeLuzio • 14 Plugins on WordPress.org • Core contributor • Originally not a developer – you can do it too! • Paid plugins include • WP-CRM System • WP1099 • Conditional Checkout Fields • WP In-Post Ads #wcphx @scottdeluzio
  3. 3. What is a plugin? • Software that adds to or changes how WordPress works • Written in PHP – other code can be used too • Easy to add features • Myth: plugins slow your site down #wcphx @scottdeluzio
  4. 4. Theme vs. Plugin THEME • Think exterior of a car • Controls look of site • Placement of content • Header, Footer, Sidebar, Content PLUGIN • Think interior features of a car • Controls functionality of site • Creates content types • Adds features #wcphx @scottdeluzio
  5. 5. Why your site might need plugins • WordPress without plugins is limited • Power of WordPress is in extendibility • SEO • eCommerce • Connect to 3rd party services • …about 54,000 others • Or your own plugin #wcphx @scottdeluzio
  6. 6. Danger of functions.php • Tutorials say paste into theme’s functions.php file • Easier than explaining how to make a plugin • Theme updates remove changes • Use a child theme! • Can’t switch themes without losing custom code #wcphx @scottdeluzio
  7. 7. Benefits of custom plugins • Won’t be lost with theme update (functions.php) • Free to change themes • Easier to troubleshoot issues • One specific function per plugin #wcphx @scottdeluzio
  8. 8. What you need for a custom plugin • Text editor • Not Microsoft Word (or other word processors) • Notepad (Windows) / TextEdit (Mac) • Notepad++ – Windows (free/Open Source) • Atom – OSX, Windows, Linux (free/Open Source) • Other paid / not open source options available • FTP/Git/Backup • Avoid the plugin/theme editor in WordPress • Use a development site! #wcphx @scottdeluzio
  9. 9. Best practices • define( ‘WP_DEBUG’, true ); • Use WordPress coding standards • https://codex.wordpress.org/ • https://developer.wordpress.org/ • Use prefixes #wcphx @scottdeluzio
  10. 10. What will the plugin do? • Create custom post types • https://developer.wordpress.org/plugins/post-types/ • Create a shortcode • https://developer.wordpress.org/plugins/shortcodes/ #wcphx @scottdeluzio
  11. 11. Let’s Code Our Own Plugin https://github.com/ScottDeLuzio/wcphx2018 #wcphx @scottdeluzio

Editor's Notes

  • Before beginning, this is a beginner level talk.
    If anyone has developed plugins, or is an advanced developer this might be nap time.
    If you’re one of those people and are here for moral support, thank you.
    You’re more than welcome to stay but I won’t be offended if you duck out.
  • I have 14 plugins on WordPress.org
    I contributed to WordPress core, so if your site is updated to 4.9 or higher, some of my code is already on your site
    My background is not in software development.
    Have a degree in accounting
    Was a soldier
    Taught myself how to code
    You can do it too!
  • Plugins are simply software that changes how WordPress works
    They are written in PHP code, but other code like JavaScript can be used too
    Plugins make it easy to add features to your site. Out of the box WordPress doesn’t have certain features but you can add them with plugins.
    Contact forms
    Page builders
    eCommerce
    SEO
    Myth: plugins slow down your site
    Plugins aren’t inherently bad or slow
    Some hosts have perpetuated this myth by saying “you have too many plugins”
    That’s a lazy way of saying “there are a lot of plugins on your site, and one of them might be slowing your site down but I don’t care to look at each one individually”
    The hope is you get rid of some plugins, the problem magically fixes itself, and you never call back for help
    Some plugins actually speed up your site (caching, optimize images, etc.)
    Truth: badly coded plugins can slow down your site.
  • I want to make sure we understand the difference between themes and plugins

    Themes
    Imagine the exterior of a car. The color, shape, type of car (convertible, SUV, minivan)
    Themes control the look of the site
    Controls where content is placed
    Manages the appearance of the site’s header, footer, sidebar, and content areas

    Plugins
    Think about the interior features of a car. The engine, GPS, heated seats, etc.
    Plugins control the functionality of your site.
    While themes control the placement of content, plugins can create new types of content
    Think products in an eCommerce store
    Plugins can add features to your site that don’t come with WordPress
  • A plain install of WordPress is pretty limited.
    Don’t get me wrong, it is 100% all you need for a simple website.
    Can make a basic business brochure site
    Start a blog
    No plugins are needed at all for this
    Power of WordPress is in it’s extendibility
    Simple to add plugins for:
    SEO
    eCommerce
    Contact Forms
    Connecting to 3rd party services
    And about 54,000 other things
  • Online tutorials provide you with code snippets that you can copy and paste onto your site.
    Many suggest copying into your theme’s functions.php file
    I think this is a lazy “one size fits all” approach
    Sometimes functions.php is the appropriate place for this code, but not always
    The way tutorials are written, copying and pasting in their entirety may break your site.
    Nothing may be wrong with the code – operator error
    https://pippinsplugins.com/quick-introduction-to-action-hooks/

    Theme Updates
    If you aren’t using a child theme, and your theme gets an update your custom code will be lost
    Child themes will fix this issue.
    A child theme is a theme that inherits a parent theme’s features, but uses it’s own code to modify or override the parent theme a bit
    But if you put your code in a child theme’s functions.php you can’t switch themes without losing the custom code.
    This might be OK if the code is doing something specific to the theme
    Ex: Make sidebar or footer appear differently on certain pages. Or changing the size of your thumbnail images.
    Not if there is functionality that you want to keep regardless of how your site looks.
  • Custom plugins will live forever on your site regardless of theme updates, or if you’ve changed your theme.
    It’s also easier to troubleshoot issues in a custom plugin.
    Each plugin should do one thing. That way if you’re having trouble with that thing, you know exactly where to look to fix it.
    Ex: Shortcode adds content to your page. If the page acts funny or has errors when the shortcode is in it, you know the shortcode plugin is messed up. Fix it or disable it.
    Some popular contact form plugins create a form that you can use on your site.
    They also have add-on plugins that let you connect to third party services, or do other things.
    Imagine if all of the add-ons were baked into the main plugin, and the contact form stopped working correctly. Which add-on’s code caused it to stop?
    With the code separated into multiple plugins, it’s easy to disable a plugin to isolate the issue.
  • Text Editor
    Microsoft Word can technically write text, but can’t save as PHP. Reasons are technical, and not important – just don’t use it.
    Built in text editors on Windows or Mac computers will work, but not really built for this purpose.
    Free open source options available that can help catch errors in code, auto format code, and other benefits.
    Code highlighting helps identify errors.
    Paid non-open source options are also available. I use Sublime Text, but I won’t get into that here.
    FTP/Git For PHP Code
    PHP code can come from tutorials – only take from trusted sources
    Write your own code if you’re familiar with PHP
    When you do so, avoid using the plugin or theme editor in WordPress.
    If you make a mistake, it can bring the whole site down, and you won’t have a way to revert changes.
    Use FTP to upload changes to your server. That way if there’s a problem, you can CTRL+Z to undo changes on your computer and re-upload a working version.
    Get a Development Site
    Prevents your live site from crashing when something goes wrong.
    If editing CSS, the worst case is you’ll make the site look ugly.
    If editing PHP, and you’re missing one comma, quote, or semi-colon, you can bring the whole site down.
    Many hosts offer staging, or development environments. Use that if you have nothing else to use.
    Or buy a cheap hosting account to build on.
    Can also install a server locally on your computer. Makes it easy to spin up sites quickly and test.
  • define( ‘WP_DEBUG’, true );
    Put this in your wp-config.php file in the site’s root directory.
    This will output errors onto the screen, and help identify issues.
    Don’t use on a live site.

    Coding standards

    Prefix functions
    If your plugin has a generic function name that is used somewhere else, it will conflict with the other function.
    Example: your plugin adds a shortcode, and you use “add_shortcode” as a function name. Well WordPress already uses that, and you’ll cause an error.
    Call it “my_plugin_name_add_shortcode” instead.
  • What are custom post types?
    WP comes with 5 default post types
    post, page, attachment, revision, menu
    Custom post types handle specific content types
    eCommerce site might have a Products CPT
    Education site might have Assignments CPT
    Movie review site might have Movies and Actors as separate CPTs
    Think about it this way, posts is a default post type. That’s where blog posts show up. Not all content is a blog post, and shouldn’t show up in your blog’s feed.
    You wouldn’t want a new blog post to show up every time you uploaded an image, right?
    Images are your “attachment” post type.
    Just like you wouldn’t want every product to show up on your blog feed on an eCommerce site.
    What are shortcodes?
    Shortcodes provide a way to dynamically interact with your site’s content.
    You can’t put PHP code into a post or page for security reasons. You can put in a shortcode to do all sorts of things.
    Shortcodes run a PHP function, which can display data from your site, or even another site through an API.
  • Every plugin should have:
    A folder – technically this isn’t needed, but it is useful for organizing if the plugin will have more than one file.
    A PHP file with a header comment

    Header comment must at a minimum contain the plugin name.
    Name should be unique.
    Another plugin on your site with same name will be confusing – probably can’t save that way anyways, or one will override the other.
    Another plugin on WordPress.org with same name will cause your plugin to be deleted when the other plugin gets updated.
    Header comment can optionally also contain:
    Plugin URL: The homepage of the plugin – can’t use WordPress.org URL
    Description: A short description, which is shown on the Plugins page in WP Admin. Keep under 140 characters.
    Version: 1.0, 1.1, etc. This helps keep track of changes, and lets people know there is a new version available.
    Author: The name of the author, or a comma separated list of author names if more than one
    Author URL: The author’s website
    License: The name of the license that the plugin is distributed under (e.g. GPL2)
    License URL: A link to the license text
    Text Domain: A common string that denotes all text belonging to your plugin. This makes it easier to translate your plugin later on. If you’re building it for yourself and not distributing it, this isn’t necessary.
    Domain Path: The location of your language translation files.
  • ×