Bird
Raised Fist0
Wordpressframework~30 mins

Payment gateway configuration in Wordpress - Mini Project: Build & Apply

Choose your learning style10 modes available
Payment gateway configuration
📖 Scenario: You are building a WordPress plugin that adds a simple payment gateway configuration form to the admin dashboard. This form will allow the site admin to enter and save the payment gateway API key and enable or disable the gateway.
🎯 Goal: Create a WordPress plugin that registers a settings page with a form. The form should have a field for the API key and a checkbox to enable or disable the payment gateway. The settings should be saved and loaded correctly.
📋 What You'll Learn
Create a settings page under the WordPress admin menu
Add a text input field for the payment gateway API key
Add a checkbox to enable or disable the payment gateway
Save and retrieve these settings using WordPress options API
💡 Why This Matters
🌍 Real World
Many WordPress sites need to configure payment gateways for e-commerce or donations. This project shows how to build a simple plugin to manage such settings safely.
💼 Career
Understanding WordPress plugin development and the Settings API is essential for WordPress developers working on custom plugins or themes that require configuration options.
Progress0 / 4 steps
1
Create plugin header and register settings
Create a plugin file with the header comment for WordPress. Then register two settings called payment_gateway_api_key and payment_gateway_enabled using register_setting inside a function hooked to admin_init.
Wordpress
Hint

Use register_setting inside a function hooked to admin_init to register your settings.

2
Add admin menu page for settings
Add a function called spg_add_admin_menu that uses add_options_page to add a menu item named Payment Gateway under the Settings menu. Hook this function to admin_menu.
Wordpress
Hint

Use add_options_page inside a function hooked to admin_menu to add the settings page.

3
Create the settings page form
Define the function spg_settings_page that outputs a form with method post and action options.php. Inside the form, call settings_fields('spg_options_group') and do_settings_sections('spg-settings'). Add a text input named payment_gateway_api_key with the saved value from get_option('payment_gateway_api_key'). Add a checkbox named payment_gateway_enabled that is checked if get_option('payment_gateway_enabled') is truthy. Add a submit button labeled Save Changes.
Wordpress
Hint

Use get_option to get saved values and output a form with text input and checkbox inside spg_settings_page.

4
Ensure settings are saved and accessible
Add code to sanitize and save the payment_gateway_api_key as a string and payment_gateway_enabled as a boolean when the form is submitted. Use the sanitize_text_field function for the API key. This is handled automatically by register_setting but add a sanitization callback function called spg_sanitize_options and pass it as the third argument to register_setting. The function should accept an array with keys payment_gateway_api_key and payment_gateway_enabled and return the sanitized array.
Wordpress
Hint

Use sanitize_text_field for the API key and a custom function to convert the enabled checkbox value to 1 or 0. Pass these as sanitize_callback in register_setting.

Practice

(1/5)
1. What is the main purpose of configuring a payment gateway in a WordPress site?
easy
A. To allow customers to make payments securely on the website
B. To change the website's theme colors
C. To add new blog posts automatically
D. To improve website loading speed

Solution

  1. Step 1: Understand what a payment gateway does

    A payment gateway connects the website to payment services to process payments.
  2. Step 2: Identify the correct purpose in the options

    Only To allow customers to make payments securely on the website mentions secure payments, which matches the gateway's role.
  3. Final Answer:

    To allow customers to make payments securely on the website -> Option A
  4. Quick Check:

    Payment gateway = secure payments [OK]
Hint: Payment gateways handle payments, not design or content [OK]
Common Mistakes:
  • Confusing payment gateway with website design features
  • Thinking it manages blog posts
  • Assuming it speeds up the site
2. Which code snippet correctly adds a new payment gateway class in WooCommerce for WordPress?
easy
A. add_filter('woocommerce_payment_gateways', 'add_my_gateway'); function add_my_gateway($gateways) { $gateways[] = 'WC_Gateway_MyGateway'; return $gateways; }
B. add_action('woocommerce_payment_gateways', 'add_my_gateway'); function add_my_gateway() { return 'WC_Gateway_MyGateway'; }
C. add_filter('woocommerce_payment_gateways', 'add_my_gateway'); function add_my_gateway() { return 'WC_Gateway_MyGateway'; }
D. add_action('woocommerce_payment_gateways', 'add_my_gateway'); function add_my_gateway($gateways) { $gateways[] = 'WC_Gateway_MyGateway'; }

Solution

  1. Step 1: Identify correct WordPress hook type

    To modify gateways list, use add_filter, not add_action.
  2. Step 2: Check function signature and return

    The function must accept $gateways array, add new gateway, and return the updated array.
  3. Final Answer:

    add_filter with function adding to $gateways and returning it -> Option A
  4. Quick Check:

    Filter modifies list and returns it [OK]
Hint: Use add_filter with $gateways parameter and return updated array [OK]
Common Mistakes:
  • Using add_action instead of add_filter
  • Not returning the updated gateways array
  • Missing $gateways parameter in function
3. Given this code snippet in a WordPress plugin for WooCommerce payment gateway registration:
add_filter('woocommerce_payment_gateways', function($gateways) {
  $gateways[] = 'WC_Gateway_Custom';
  return $gateways;
});

What will happen after this code runs?
medium
A. Nothing changes because add_filter is used incorrectly
B. The payment gateways list will be replaced with only 'WC_Gateway_Custom'
C. An error will occur because the function does not return anything
D. The custom payment gateway 'WC_Gateway_Custom' will be added to the available gateways list

Solution

  1. Step 1: Understand the add_filter usage

    The anonymous function receives $gateways array, adds a new gateway, and returns the updated array.
  2. Step 2: Predict the effect on WooCommerce gateways

    The new gateway 'WC_Gateway_Custom' is appended, so it becomes available alongside existing gateways.
  3. Final Answer:

    The custom payment gateway 'WC_Gateway_Custom' will be added to the available gateways list -> Option D
  4. Quick Check:

    Appending gateway and returning list adds it [OK]
Hint: Returning updated gateways array adds new gateway [OK]
Common Mistakes:
  • Thinking the list is replaced instead of appended
  • Assuming no return means error
  • Confusing add_filter with add_action
4. You wrote this code to add a payment gateway but it causes a fatal error:
add_filter('woocommerce_payment_gateways', 'add_gateway');
function add_gateway() {
  $gateways[] = 'WC_Gateway_New';
  return $gateways;
}

What is the main problem?
medium
A. The function should use add_action instead of add_filter
B. The function add_gateway does not accept the $gateways parameter
C. The gateway class name is incorrect
D. The function does not return anything

Solution

  1. Step 1: Check function parameters for filter hook

    Filter functions must accept the value they modify, here $gateways array.
  2. Step 2: Identify missing parameter causing error

    Function add_gateway() has no parameters, so $gateways is undefined inside, causing error.
  3. Final Answer:

    The function add_gateway does not accept the $gateways parameter -> Option B
  4. Quick Check:

    Filter function needs $gateways parameter [OK]
Hint: Filter functions must accept and return the filtered value [OK]
Common Mistakes:
  • Omitting the parameter in the filter function
  • Using add_action instead of add_filter
  • Not returning the updated array
5. You want to configure a payment gateway in WooCommerce that only appears for customers from the US. Which approach correctly applies this condition in your gateway class?
hard
A. Use CSS to hide the payment option on the checkout page for non-US customers
B. Add a filter to remove the gateway from the list after checkout is complete
C. Override the is_available() method in your gateway class to check the customer's billing country and return true only if it is 'US'
D. Set the gateway to be enabled only in the WooCommerce settings without any code

Solution

  1. Step 1: Understand how to conditionally show gateways

    WooCommerce gateways can override the is_available() method to control when they appear.
  2. Step 2: Apply country check in is_available()

    Checking the customer's billing country inside is_available() and returning true only for 'US' ensures correct display.
  3. Final Answer:

    Override is_available() to check billing country and return true only for 'US' -> Option C
  4. Quick Check:

    Use is_available() for conditional gateway display [OK]
Hint: Use is_available() method to control gateway visibility by condition [OK]
Common Mistakes:
  • Trying to hide gateway with CSS (not secure or reliable)
  • Filtering gateways after checkout (too late)
  • Only enabling gateway in settings without conditions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard