Website Development with AI
Senior 6 · ICT
Website Development with AI
Lesson 3 of 5
25 min
The cooperative's buyers keep asking the same three things: how to order, how much ten bags would cost, and how to send a message without opening their email. WordPress itself does not include an order calculator or a contact form. Rather than make the core software larger for everyone, WordPress lets each site add the features it needs as plugins. The official directory at wordpress.org/plugins lists tens of thousands of free plugins.
Definition
Plugin: a package of code that adds a feature to WordPress or changes how it behaves, without editing WordPress's own files. It can be activated, deactivated and deleted from Plugins in the dashboard.
Hook: a named point in WordPress's code where a plugin may run its own function. An action hook lets a plugin do something at that moment; a filter hook lets it change data, such as a post's text, before it is used.
Shortcode: a code in square brackets, such as [harvest_notice], that an editor types into content and a plugin replaces with generated HTML.
| Need | Kind of plugin | Widely used examples |
|---|---|---|
| Visitors send messages | Contact form | Contact Form 7, WPForms Lite |
| Stop spam comments and messages | Anti-spam | Akismet |
| Sell products online | E-commerce | WooCommerce |
| Help search engines read the site | SEO | Yoast SEO |
| Copy the site in case of disaster | Backup | UpdraftPlus |
| Make pages load faster | Caching | WP Super Cache |
Naming a plugin here is not a recommendation over its rivals. The test that follows is how you choose.
[contact-form-7 id="…" title="Contact form 1"].A form only sends email if the server can send it. Local installations usually cannot, so test mail on the real host, often with an SMTP plugin.
A plugin is ordinary PHP. The comment block at the top tells WordPress its name; the rest attaches functions to hooks. While WordPress builds a page it passes a series of hooks, and calls every function hooked onto each one.
This complete plugin adds the shortcode [harvest_notice]. Save it as wp-content/plugins/kivu-harvest-notice/kivu-harvest-notice.php; it then appears in the Plugins list, ready to activate.
<?php
/**
* Plugin Name: Kivu Hills Harvest Notice
* Description: Adds a [harvest_notice] shortcode that shows the current harvest message.
* Version: 1.0
* Author: S6 ICT class
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // stop if someone opens this file directly
}
function khc_harvest_notice( $atts ) {
$atts = shortcode_atts( array( 'season' => 'main' ), $atts, 'harvest_notice' );
$season = esc_html( $atts['season'] ); // never print user input unescaped
return '<p class="khc-notice">The ' . $season
. ' harvest is open. Deliveries 7:00 to 15:00, Monday to Saturday.</p>';
}
add_shortcode( 'harvest_notice', 'khc_harvest_notice' );
Typing [harvest_notice season="second"] in a post produces: The second harvest is open. Deliveries 7:00 to 15:00, Monday to Saturday. The code was checked with php -l and run with stand-in versions of the three WordPress functions. When someone typed season="<script>x</script>", esc_html turned the angle brackets into harmless text, which is why it is there.
Some interactivity needs no plugin at all. A Custom HTML block can hold HTML and JavaScript that run in the visitor's browser. This price calculator updates as the buyer types. Its total function was tested with node: 4 bags give 14,000 Frw, 10 bags give 31,500 Frw after the discount, and 0 or 2.5 bags are rejected.
<div class="khc-calc">
<label for="khc-qty">Bags of 250 g</label>
<input id="khc-qty" type="number" min="1" value="1">
<p id="khc-total" aria-live="polite"></p>
</div>
<script>
const PRICE = 3500; // Frw per bag (illustrative)
function total(qty) {
if (!Number.isInteger(qty) || qty < 1) return null; // reject bad input
const sum = qty * PRICE;
return qty >= 10 ? sum * 0.9 : sum; // 10% off for 10 bags or more
}
const box = document.getElementById("khc-qty");
const out = document.getElementById("khc-total");
function show() {
const t = total(Number(box.value));
out.textContent = t === null ? "Enter a whole number of bags."
: "Total: " + t.toLocaleString("en-US") + " Frw";
}
box.addEventListener("input", show);
show();
</script>Only users with the right to post unfiltered HTML (on a single site, Administrators and Editors) can save <script> in a Custom HTML block; for other roles WordPress strips it out. That is a deliberate security rule, because a script on a page can do anything the page can do.
WinningWP: What Are WordPress Plugins - And How To Use Them? Watch how a plugin is found, installed and activated from the dashboard, and the advice on how many plugins to use.
Worked example 1: choose between two plugins
Two gallery plugins look similar. Plugin P: last updated 3 weeks ago, 300,000 active installations, rating 4.6 from 900 reviews, tested with the current WordPress. Plugin Q: last updated 2 years ago, 2,000 installations, rating 5.0 from 4 reviews, "not tested with your version". Which should the cooperative install?
Worked example 2: trace the calculator
A buyer types 12. What does the page show?
Number("12") is 12, an integer of at least 1, so the input passes the check.sum = 12 × 3500 = 42,000.Warning
Every plugin is code you are trusting. Security researchers who track WordPress report that most vulnerabilities are found in plugins and themes rather than in WordPress core. Install only what you need, only from the official directory or a reputable seller, keep everything updated, delete (not just deactivate) plugins you no longer use, and keep a recent backup before any update.
Tip
Can a block do it? Before installing a plugin, check whether a built-in block (Embed, Gallery, Table, Buttons) or a few lines in a Custom HTML block already does the job. Fewer plugins means a faster, safer site.
wp-content/plugins/kivu-harvest-notice, save the plugin file in it, activate it, and use [harvest_notice] on the home page.Check: the form delivers a test message on a host that can send mail (or shows "Thank you" locally), the calculator gives 37,800 Frw for 12 bags, and every active plugin has a written reason.
Practice
season through esc_html before printing it?Number("abc") is NaN, which is not an integer, so total returns null and the page shows "Enter a whole number of bags."< and > into harmless text so nothing is run in visitors' browsers (it prevents cross-site scripting).Sources and credits
This lesson adapts CC BY-SA material and is shared under CC BY-SA 4.0.