The add_blog_option() WordPress PHP function adds a new option for a given blog ID.
Usage
add_blog_option($blog_id, $option_name, $option_value);
Parameters
$blog_id
(int): A blog ID. Can be null to refer to the current blog.$option_name
(string): Name of the option to add. Expected to not be SQL-escaped.$option_value
(mixed): Option value, can be anything. Expected to not be SQL-escaped.
More information
See WordPress Developer Resources: add_blog_option()
Examples
Add an option to a specific blog
In this example, we add a new option called ‘custom_theme_color’ with the value ‘blue’ to a blog with ID 2.
// Add a new option to blog with ID 2 add_blog_option(2, 'custom_theme_color', 'blue');
Add an option to the current blog
In this example, we add a new option called ‘footer_text’ with the value ‘Powered by WordPress’ to the current blog.
// Add a new option to the current blog add_blog_option(null, 'footer_text', 'Powered by WordPress');
Add an option with an array value
In this example, we add a new option called ‘social_links’ with an array value to a blog with ID 3.
// Array of social links $social_links = array( 'facebook' => 'https://www.facebook.com/yourpage', 'twitter' => 'https://www.twitter.com/yourprofile', 'instagram' => 'https://www.instagram.com/yourprofile', ); // Add a new option to blog with ID 3 add_blog_option(3, 'social_links', $social_links);
Add an option to multiple blogs
In this example, we add a new option called ‘global_announcement’ with the value ‘Our website is now mobile-friendly!’ to multiple blogs with IDs 1, 2, and 3.
// Array of blog IDs $blog_ids = array(1, 2, 3); // Loop through blog IDs and add the option foreach ($blog_ids as $blog_id) { add_blog_option($blog_id, 'global_announcement', 'Our website is now mobile-friendly!'); }
Add an option to a blog using a helper function
In this example, we create a helper function called add_custom_option
to add an option to a blog. The function takes the blog ID, option name, and option value as parameters. If the blog ID is not specified, the option will be added to the current blog.
// Helper function to add an option to a blog function add_custom_option($blog_id = null, $option_name, $option_value) { add_blog_option($blog_id, $option_name, $option_value); } // Add a new option to the current blog add_custom_option(null, 'copyright_text', '© 2023 Your Website. All rights reserved.');