Discount Code
Wordpress plugin that show a message for a discount code through a shortcode
★ 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.zipA 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
- Discount Slider Textcode — e.g.
- 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
- Activate the plugin.
- Go to wp-admin → Discount Code, fill in both fields, click Save Settings.
- Drop
[discount_code]into any post, page, or widget area that supports shortcodes. - 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 withsanitize_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 thatoptions.phpverifies before saving — forged/injected form submissions are rejected. - The settings page is restricted to the
manage_optionscapability. - Output is escaped with
esc_html()in the shortcode andesc_attr()in the settings field inputs.
- CSRF protection comes from
- Empty state: the shortcode returns
''(notnull/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.