after_save_post on settings page - values not updated

Support General after_save_post on settings page - values not updated

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #46608
    FizzPop MediaFizzPop Media
    Participant

    I have a settings page that uses a field group called fundraiser-fields.
    Here is a [screenshot](https://tinyurl.com/2y9aasxq)

    I followed the example titled "Combining values of 2 fields into another one:" given here: https://docs.metabox.io/actions/rwmb-after-save-post/

    I updated the code based on my field group id and variable names but nothing happens.

    add_action( 'rwmb_fundraiser-fields_after_save_post', function( $object_id ) {
        if ( isset( $_POST['campaign_name'] ) && isset( $_POST['campaign_description'] ) ) {
            $value = $_POST['campaign_name'] . $_POST['campaign_description'];
            update_post_meta( $object_id, 'text_test', $value );
        }
    } );
    #46615
    PeterPeter
    Moderator

    Hello,

    Obviously, the settings page isn't a post, so the function update_post_meta() doesn't work to update the settings page. You can use the function rwmb_set_meta() or update_option() to update fields on a settings page.

    Please follow the documentation
    https://docs.metabox.io/functions/rwmb-set-meta/
    https://developer.wordpress.org/reference/functions/update_option/
    https://docs.metabox.io/extensions/mb-settings-page/

    #46618
    FizzPop MediaFizzPop Media
    Participant

    @Peter
    According to your documentation, rwmb-after-save-post works for settings. Is that incorrect?
    https://docs.metabox.io/actions/rwmb-after-save-post/

    Although the action name is rwmb_after_save_post, it applies to all object types, including term, user, or setting. We keep the name for historical reasons.

    My ultimate goal is to calculate a value based on two numbers entered as values into settings:
    Inputs: Goal Amount and Amount Raised
    Calculate: Goal Percentage as Amount Raised / `Goal Amount
    This is why I am using the after-save-post function
    I also tried before-save-post but it also didn't work.

    #46629
    PeterPeter
    Moderator

    Hello,

    It isn't an issue with the action hook rwmb_after_save_post. The issue lies in the function update_post_meta() in the callback that you are using. The settings page doesn't have an object ID, so apparently, the function update_post_meta() won't work to update a post meta for a post.

    #46630
    FizzPop MediaFizzPop Media
    Participant

    I am confused.
    Is the documentation incorrect?

    How do I solve for this?

    #46644
    PeterPeter
    Moderator

    Is the documentation incorrect?

    The documentation is correct. But the function update_post_meta() will update the post meta, not the settings page value. Here is an example:

    add_action( 'rwmb_fundraiser-fields_after_save_post', function( $object_id ) {
        update_option(...);
    } );

    If you cannot complete the task, we offer a customization service. Please contact us here for more details https://metabox.io/contact/

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