Support » Developing with WordPress » Hook function to submit_button when saving options

  • Hi everyone!

    I need some help here! I’m working on a plugin that is intended to save a manifest.json file in the root of WordPress installation. This plugin has a settings page in where the administrator can setup some values for the manifest.json such as the background color, theme color, short name, fcm sender Id, etc.
    All those values are saved correctly in the database as an option in the wp_options table. The thing is, I have another function in my plugin that has to be fired once those values are successfuly saved in the database in order to generate the manifest.json. I thought that this could be done with some hook like added_options or updated_options, but neither of those seems to work.

    Is there another way to hook my function to the submit button so it gets fired once the submit button is clicked or even better is there a way to fire the function once the options are saved to database?

    Any advice will be helpful!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello @cubeinthebox,

    Looking at the sources (core files, wp-includes/option.php) you can always find your target hook tags:

    add_action('added_option', 'callback_function', 10, 2);
    add_action('updated_option', 'callback_function', 10, 3);

    or you can use setting api or option api for that.

    please check below links for reference,

    https://developer.wordpress.org/plugins/settings/options-api/

    https://developer.wordpress.org/plugins/settings/settings-api/

    Thanks & Regards

    • This reply was modified 1 month, 3 weeks ago by anant101289.
    • This reply was modified 1 month, 3 weeks ago by bcworkz. Reason: code fixed
    Moderator bcworkz

    (@bcworkz)

    I’d use the option name specific hooks “add_option_{$option}” and “update_option_{$option}” so the callbacks run only when your specific option is added/updated, saving your callback the trouble of checking.

    If the ‘added_option’ and ‘updated_option’ hooks aren’t working for you, something else is going on beyond whether you are using the right hooks or not. Perhaps you should share the pertinent parts of your code. If the code is sizable, please use pastebin.com or gist.github.com to share your code and just place the resulting link here.

    If the values are saved in the DB, why repeat that in a manifest.json file? Redundant data should be avoided.

    @anant101289 – thanks for contributing. When you provide code snippets in these forums, please demarcate with backticks or use the code button. Otherwise the forum’s parser corrupts your code. I fixed the code in your earlier reply.

    Hello @bcworkz,

    Thanks for your suggestion,next time i will keep in mind.

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.