WP Manifestindependent plugin directory
manifest / utilities / discount-code

Discount Code

Wordpress plugin that show a message for a discount code through a shortcode

by dreidjo · github.com/dreidjo/discount-code

0stars
0forks

Install

No release zip yet. The repository archive installs, but the folder name will carry the branch suffix and updates will not flow:

wp plugin install https://github.com/dreidjo/discount-code/archive/refs/heads/main.zip

A small WordPress plugin that lets a site owner display a current discount code and promo message anywhere on the site via a shortcode, editable from a normal wp-admin settings page — no code edits needed to change the promo.

What it does

  • Adds a Discount Code menu item in wp-admin with two fields:
    • Discount Slider Textcode — e.g. SAVE10
    • Discount Slider Message — e.g. 10% off
  • Provides a [discount_code] shortcode that outputs both values in a bordered box, wherever it's placed (post, page, widget, etc.)
  • If either field hasn't been filled in yet, the shortcode outputs nothing — no empty/broken-looking box on the frontend.

Usage

  1. Activate the plugin.
  2. Go to wp-admin → Discount Code, fill in both fields, click Save Settings.
  3. Drop [discount_code] into any post, page, or widget area that supports shortcodes.
  4. To change the promo later, just update the two fields in wp-admin — every instance of the shortcode across the site updates automatically.

How it's built

  • Storage: both fields are saved as a single array under one option, discount_code_options, via the WordPress Settings API (register_setting, add_settings_section, add_settings_field).
  • Validation: discount_code_validate() sanitizes every submitted field with sanitize_text_field() before saving, and guards against non-array input (e.g. a malformed or forged submission) by returning an empty array instead of erroring.
  • Security:
    • CSRF protection comes from settings_fields(), which outputs a nonce that options.php verifies before saving — forged/injected form submissions are rejected.
    • The settings page is restricted to the manage_options capability.
    • Output is escaped with esc_html() in the shortcode and esc_attr() in the settings field inputs.
  • Empty state: the shortcode returns '' (not null/false, per WP shortcode convention) if either field is empty, so nothing renders until both are set.
  • settings_errors() is called on the settings page so WordPress's own "Settings saved." confirmation message actually displays after saving.

File structure

Currently a single-file plugin:

discount-code/
└── discount-code.php   All logic: shortcode, admin menu, settings
                         registration, field callbacks, validation

At this size a single file is appropriate — no need to split into an MVC structure the way a larger plugin (e.g. one with a database table or multiple screens) would warrant.

Possible extensions

  • Combine the two fields into a single sentence output (e.g. "Use code SAVE10 at checkout for 10% off!") instead of two stacked lines, if that better matches the visual style wanted.
  • Add an "active/inactive" toggle so the code can be hidden without clearing the fields.
  • Add a settings-page preview of what the shortcode currently renders.