I want to add a theme option to the Wordpress admin so that I don't need to commit code every time I need to "activate" (or deactivate by commenting out) an Optimizely javascript snippet I only want run occasionally.

I googled this question, but most of the posts I saw were related to to running CSS or JS on the admin page, and weren't related to adding a theme option.

I'm not sure how to get started. Do I have to register a setting (http://codex.wordpress.org/Settings_API#Adding_Setting_Fields)?

If so, in which file do I add add_settings_field function? Functions.php?


Yep you can use add_settings() to create the setting itself. There is an example in the codex.

This example code could go in your themes functions.php file or in a plugin file, it's up to you.

Then in the header template all you would have to do is use an if statement to check what the value of the aforementioned setting was and either output the JS link or not based upon this variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.