PHP › WordPress Plugin & Theme Development

What is the difference between a WordPress plugin and a WordPress theme, and what kind of functionality does each one typically control?

Beginner

Answer

A WordPress theme controls the visual appearance and layout of a website, including how pages, posts, and other content are actually displayed to visitors, while a plugin adds new functionality or features to a WordPress site that are typically independent of visual appearance, such as adding a contact form, an online store, or search engine optimization tools, and importantly, a well built plugin's functionality should generally continue working correctly even if a site's theme is later changed to a completely different one.
// Plugin header comment, required at the top of the main plugin file
<?php
/**
 * Plugin Name: My Custom Plugin
 * Version: 1.0
 */
Real-world example A business switches their WordPress site's visual theme to give it a fresh new look, while their previously installed contact form and online store plugins continue functioning exactly the same as before, since that functionality was never tied to the specific theme in the first place.

Common follow-ups: Can a theme also include some plugin like functionality directly within it?;What is a child theme, and why is it recommended when customizing an existing theme?