How to run an additional function when options are submitted with the Settings API





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I am using the Settings API to store the settings a user makes for a plugin. In addition, when he or she hits the "Save" button on the plugin page, I want to make some calls to another API within wordpress (generate/update a CF7 Form) and not just make the post to 'options.php'



Saving the settings works having followed the tutorials and guidelines for using the settings api. Attached below is




  • a hook I defined which I would expect to be called when the option is updated

  • the definition of the callback function in the hook

  • the 'form' tag (escaped from php) of the HTML code in the function to display the actual admin page.


This is the code excerts:



add_action('option_update_ordered_fields_in_form','set_cf7_form');
.....
function setup_cf7_form($oldval,$newval,$option) {
...
}
....
function stpz_settings_page() {
....?>....
<form method="post" action="options.php">
<?php settings_fields( 'form_my_settings' ); ?>
<?php do_settings_sections( 'form_my_settings' ); ?>
<tr>
<td><input id="result" type="hidden" name="ordered_fields_in_form" value="
<?php echo esc_attr( get_option('ordered_fields_in_form') ); ?>" /></td>
<td><?php submit_button(); ?></td>
</tr>
</form>
.....
<?php
}
?>


the function setup_cf7_form does not seem to be called at all when the submit button is clicked. I would naively suspect that this would update the option 'ordered_fields_in_form' and therefore trigger the hook.



(it actually does update the option which I can see if I reload the settings page for the plugin)



I suspect it may be the completely wrong way of doing this.










share|improve this question





























    0















    I am using the Settings API to store the settings a user makes for a plugin. In addition, when he or she hits the "Save" button on the plugin page, I want to make some calls to another API within wordpress (generate/update a CF7 Form) and not just make the post to 'options.php'



    Saving the settings works having followed the tutorials and guidelines for using the settings api. Attached below is




    • a hook I defined which I would expect to be called when the option is updated

    • the definition of the callback function in the hook

    • the 'form' tag (escaped from php) of the HTML code in the function to display the actual admin page.


    This is the code excerts:



    add_action('option_update_ordered_fields_in_form','set_cf7_form');
    .....
    function setup_cf7_form($oldval,$newval,$option) {
    ...
    }
    ....
    function stpz_settings_page() {
    ....?>....
    <form method="post" action="options.php">
    <?php settings_fields( 'form_my_settings' ); ?>
    <?php do_settings_sections( 'form_my_settings' ); ?>
    <tr>
    <td><input id="result" type="hidden" name="ordered_fields_in_form" value="
    <?php echo esc_attr( get_option('ordered_fields_in_form') ); ?>" /></td>
    <td><?php submit_button(); ?></td>
    </tr>
    </form>
    .....
    <?php
    }
    ?>


    the function setup_cf7_form does not seem to be called at all when the submit button is clicked. I would naively suspect that this would update the option 'ordered_fields_in_form' and therefore trigger the hook.



    (it actually does update the option which I can see if I reload the settings page for the plugin)



    I suspect it may be the completely wrong way of doing this.










    share|improve this question

























      0












      0








      0








      I am using the Settings API to store the settings a user makes for a plugin. In addition, when he or she hits the "Save" button on the plugin page, I want to make some calls to another API within wordpress (generate/update a CF7 Form) and not just make the post to 'options.php'



      Saving the settings works having followed the tutorials and guidelines for using the settings api. Attached below is




      • a hook I defined which I would expect to be called when the option is updated

      • the definition of the callback function in the hook

      • the 'form' tag (escaped from php) of the HTML code in the function to display the actual admin page.


      This is the code excerts:



      add_action('option_update_ordered_fields_in_form','set_cf7_form');
      .....
      function setup_cf7_form($oldval,$newval,$option) {
      ...
      }
      ....
      function stpz_settings_page() {
      ....?>....
      <form method="post" action="options.php">
      <?php settings_fields( 'form_my_settings' ); ?>
      <?php do_settings_sections( 'form_my_settings' ); ?>
      <tr>
      <td><input id="result" type="hidden" name="ordered_fields_in_form" value="
      <?php echo esc_attr( get_option('ordered_fields_in_form') ); ?>" /></td>
      <td><?php submit_button(); ?></td>
      </tr>
      </form>
      .....
      <?php
      }
      ?>


      the function setup_cf7_form does not seem to be called at all when the submit button is clicked. I would naively suspect that this would update the option 'ordered_fields_in_form' and therefore trigger the hook.



      (it actually does update the option which I can see if I reload the settings page for the plugin)



      I suspect it may be the completely wrong way of doing this.










      share|improve this question














      I am using the Settings API to store the settings a user makes for a plugin. In addition, when he or she hits the "Save" button on the plugin page, I want to make some calls to another API within wordpress (generate/update a CF7 Form) and not just make the post to 'options.php'



      Saving the settings works having followed the tutorials and guidelines for using the settings api. Attached below is




      • a hook I defined which I would expect to be called when the option is updated

      • the definition of the callback function in the hook

      • the 'form' tag (escaped from php) of the HTML code in the function to display the actual admin page.


      This is the code excerts:



      add_action('option_update_ordered_fields_in_form','set_cf7_form');
      .....
      function setup_cf7_form($oldval,$newval,$option) {
      ...
      }
      ....
      function stpz_settings_page() {
      ....?>....
      <form method="post" action="options.php">
      <?php settings_fields( 'form_my_settings' ); ?>
      <?php do_settings_sections( 'form_my_settings' ); ?>
      <tr>
      <td><input id="result" type="hidden" name="ordered_fields_in_form" value="
      <?php echo esc_attr( get_option('ordered_fields_in_form') ); ?>" /></td>
      <td><?php submit_button(); ?></td>
      </tr>
      </form>
      .....
      <?php
      }
      ?>


      the function setup_cf7_form does not seem to be called at all when the submit button is clicked. I would naively suspect that this would update the option 'ordered_fields_in_form' and therefore trigger the hook.



      (it actually does update the option which I can see if I reload the settings page for the plugin)



      I suspect it may be the completely wrong way of doing this.







      php wordpress plugins options






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 4 at 14:43









      user3117071user3117071

      63




      63
























          1 Answer
          1






          active

          oldest

          votes


















          0














          ok, the problem was just a few syntax errors. Leaving this here as what I described above actually works perfectly as intended



          The correct line to add the hook is



          add_action('update_option_ordered_fields_in_form','setup_cf7_form',10,3);


          I am not sure, if the '10,3' arguments are necessary, but maybe they are, as the target function take 3 arguments. The main issue was however the missing 'up' in the function name and the reversed to words in the hook name (update_option instead of option_update), well :-/






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54041118%2fhow-to-run-an-additional-function-when-options-are-submitted-with-the-settings-a%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            ok, the problem was just a few syntax errors. Leaving this here as what I described above actually works perfectly as intended



            The correct line to add the hook is



            add_action('update_option_ordered_fields_in_form','setup_cf7_form',10,3);


            I am not sure, if the '10,3' arguments are necessary, but maybe they are, as the target function take 3 arguments. The main issue was however the missing 'up' in the function name and the reversed to words in the hook name (update_option instead of option_update), well :-/






            share|improve this answer




























              0














              ok, the problem was just a few syntax errors. Leaving this here as what I described above actually works perfectly as intended



              The correct line to add the hook is



              add_action('update_option_ordered_fields_in_form','setup_cf7_form',10,3);


              I am not sure, if the '10,3' arguments are necessary, but maybe they are, as the target function take 3 arguments. The main issue was however the missing 'up' in the function name and the reversed to words in the hook name (update_option instead of option_update), well :-/






              share|improve this answer


























                0












                0








                0







                ok, the problem was just a few syntax errors. Leaving this here as what I described above actually works perfectly as intended



                The correct line to add the hook is



                add_action('update_option_ordered_fields_in_form','setup_cf7_form',10,3);


                I am not sure, if the '10,3' arguments are necessary, but maybe they are, as the target function take 3 arguments. The main issue was however the missing 'up' in the function name and the reversed to words in the hook name (update_option instead of option_update), well :-/






                share|improve this answer













                ok, the problem was just a few syntax errors. Leaving this here as what I described above actually works perfectly as intended



                The correct line to add the hook is



                add_action('update_option_ordered_fields_in_form','setup_cf7_form',10,3);


                I am not sure, if the '10,3' arguments are necessary, but maybe they are, as the target function take 3 arguments. The main issue was however the missing 'up' in the function name and the reversed to words in the hook name (update_option instead of option_update), well :-/







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 4 at 20:51









                user3117071user3117071

                63




                63
































                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54041118%2fhow-to-run-an-additional-function-when-options-are-submitted-with-the-settings-a%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Monofisismo

                    Angular Downloading a file using contenturl with Basic Authentication

                    Olmecas