Exciting News! Flipper Code is now WePlugins! Same commitment to excellence, brand new identity.

How to use add_site_option action in WordPress

Sandeep Kumar Mishra
Sandeep Kumar Mishra
April 5, 2023
5 minutes read

Hey there, fellow WordPress enthusiast! If you’re diving into the world of WordPress development, you might have stumbled upon the add_site_option action hook. This hook fires right after a network option is successfully added. It’s pretty handy when you’re working with multisite setups and need to perform additional tasks whenever a new option is added. Let’s break it down with some examples!

Example 1: Logging New Network Option

In this example, we’ll log the details of the newly added network option to a custom log file each time the add_site_option action is triggered.

    function weplugins_log_new_network_option($option, $value, $network_id) {
        $log_entry = "New network option added: {$option}, Value: {$value}, Network ID: {$network_id}n";
        file_put_contents(ABSPATH . 'network_option_log.txt', $log_entry, FILE_APPEND);
    }
    add_action('add_site_option', 'weplugins_log_new_network_option', 10, 3);
    

Example 2: Email Notification on Option Add

This example sends an email notification to the site admin every time a new network option is added, keeping them informed of changes.

    function weplugins_notify_admin_on_option_add($option, $value, $network_id) {
        $admin_email = get_option('admin_email');
        $subject = "New Network Option Added";
        $message = "Option: {$option}, Value: {$value}, Network ID: {$network_id}";
        wp_mail($admin_email, $subject, $message);
    }
    add_action('add_site_option', 'weplugins_notify_admin_on_option_add', 10, 3);
    

Example 3: Custom Functionality on Option Add

Suppose we have a custom functionality that needs to be executed whenever a new network option is added. Here’s how you can set it up:

    function weplugins_custom_function_on_option_add($option, $value, $network_id) {
        // Custom code here
    }
    add_action('add_site_option', 'weplugins_custom_function_on_option_add', 10, 3);
    

Access Premium WordPress Plugins

If you need any help customizing this hook or have any specific requirements, feel free to Contact Us. Our team is always ready to assist with your WordPress development needs!

Sandeep Kumar Mishra

Sandeep Kumar Mishra

Sandeep Kumar Mishra writes about WordPress and Artificial Intelligence, offering tips and guides to help you master your website and stay updated with the latest tech trends.

Explore the latest in WordPress

Trying to stay on top of it all? Get the best tools, resources and inspiration sent to your inbox every Wednesday.