This is a pretty specific issue I’m addressing here but I thought I would continue the theme from my last post in regard to setting up admin settings in vBulletin. I had a need to store some config values accessible by my plugin code that directly relate to the values of the user-entered settings. There’s lots of places I could store this data, but for the sake of consistency and adhering to the existing structure of vBulletin I decided to put this data in new Setting fields. Here’s how I did it.

Creating the Settings

Referring back to my last post, let’s set up a new field with a custom Option Code. Since this field will be automatically populated, we don’t want to allow the user to be able to input anything here. They will, however, be able to see the value stored in the field. Here’s what my Option Code looks like.

<input type=\"text\" class=\"bginput\" name=\"setting[" . $setting['varname'] . "]\" id=\"it_setting_" . $setting['varname'] . "\" value=\"" . $setting['value'] . "\" size=\"40\" dir=\"ltr\" tabindex=\"1\" disabled=\"true\" />

If you look at the source generated on the Settings page you’ll notice this looks nearly identical to any other text input, although I’ve added disabled="true" to disallow user input. That’s it! Save this field and you should see it in your Settings, but you should not be able to edit it. I also put “(Auto-Populated)” in the title of this field and added a note in the description that this field does not need to be completed by the user to avoid any confusion.

Building the Plugin

Now we need to write a plugin to listen for submissions of the Settings form, check if certain conditions are met, and save the desired values into our new field. Create a new plugin within your Product and set it to run on the admin_options_processing hook. This hook runs in the middle of the save_settings function that writes setting values to the database.

* Save the setting POST var
$setting_array = $vbulletin->GPC['setting'];

* Check if the user input field is being submitted. Also
* test for the existence of a unique variable we'll append
* to the global $vbulletin object to prevent an infinite loop.
if ($setting_array['productname_userinputfield'] && !$vbulletin->productname_admin_processing) {
    // Set our unique variable to true to prevent this IF block
    // from executing multiple times.
    $vbulletin->productname_admin_processing = true;

    // Set our new value. 
    $autovalues = array("productname_autofield" => "foobar");

    // Save our value

Couple things to note here. First, note that we’re only executing our code if $vbulletin->productname_admin_processing evaluates as a negated variable. This is because our hook is running in the middle of the save_settings function, but we need to call this same function in order to save our setting values. We can save a variable as a flag on the global $vbulletin object to guarantee that it will be accessible anywhere in our application. Now, when save_settings executes again our code won’t infinitely nest executions of the save_settings function within itself. Also, save_settings takes one parameter, an array of key-value pairs where the key is the fieldname. Do not name this variable $settings in your plugin as it will overwrite the existing $settings variable the parent save_settings function is processing, and none of the user-input fields will end up being saved.

Share this article:

If this resonates with you I'd love to help!

I help business with problems like these every day.

Click below to schedule a call.

About The Author

Bryce Hamrick

Facebook Twitter

Bryce Hamrick is an entrepreneur, business & marketing strategist, and product consultant with nearly two decades of experience in industry. Bryce has been a software engineer, product manager, and director of product management for startups as well as large enterprises. He has led teams to bring dozens of products to market and has executed numerous six-figure product launches. Today Bryce and his team focus on leveraging his product execution strategy to help businesses with growth and scale.