after_save_post on settings page - values not updated
- This topic has 5 replies, 2 voices, and was last updated 7 months ago by
Peter.
-
AuthorPosts
-
October 5, 2024 at 3:43 AM #46608
FizzPop Media
ParticipantI 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 ); } } );
October 6, 2024 at 10:20 PM #46615Peter
ModeratorHello,
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 functionrwmb_set_meta()
orupdate_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/October 7, 2024 at 7:07 PM #46618FizzPop 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
andAmount Raised
Calculate:Goal Percentage
asAmount 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.October 8, 2024 at 11:13 PM #46629Peter
ModeratorHello,
It isn't an issue with the action hook
rwmb_after_save_post
. The issue lies in the functionupdate_post_meta()
in the callback that you are using. The settings page doesn't have an object ID, so apparently, the functionupdate_post_meta()
won't work to update a post meta for a post.October 8, 2024 at 11:17 PM #46630FizzPop Media
ParticipantI am confused.
Is the documentation incorrect?How do I solve for this?
October 9, 2024 at 9:50 PM #46644Peter
ModeratorIs 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/
-
AuthorPosts
- You must be logged in to reply to this topic.