Support Forum
Hi,
I was able to create a shortcode (which is used in an Elementor button) that automatically create a phone text message based on specific custom fields.
Here is the code:
add_shortcode( 'sms_text', 'sms_text' );
function sms_text( $post_id ) {
$meta_name = rwmb_meta( 'main_contact', '', $post_id );
$meta_phone = rwmb_meta( 'cellular_phone_number', '', $post_id );
$meta_custom_message = rwmb_meta( 'custom_text_message', '', $post_id);
$page_url = get_permalink();
return 'sms:'.$meta_phone.'?&body=Hey there, it\'s '.$meta_name.'. '.$meta_custom_message.' '.$page_url;
}
Although it is working well, my first idea was to use the "rwmb_{$field_id}_after_save_field" action + the function update_post_meta() to save the value into another custom field...
Unfortunately, I wasn't able to make it work (I don't do much backend coding!). My first question is:
Is using a shortcode a good or bad idea for this purpose?
If yes, what could be the problem in the following code?
add_action( 'rwmb_contact-card-fields_after_save_post', 'sms_text' );
// Get the field value
function sms_text( $post_id ) {
$meta_name = rwmb_meta( 'main_contact', '', $post_id );
$meta_phone = rwmb_meta( 'cellular_phone_number', '', $post_id );
$meta_custom_message = rwmb_meta( 'text_message', '', $post_id);
$page_url = get_permalink();
$custom_field_to_save_value = rwmb_meta( 'custom_sms_text', '', $post_id); //This is the custom field I want the value to be saved in
$text_message = 'sms:'.$meta_phone.'?&body=Hey there, it\'s '.$meta_name.'. '.$meta_custom_message.' '.$page_url;
// Preprare update post
$my_post = array(
'ID' => $post_id,
$custom_field_to_save_value => $text_message,
);
wp_update_post( $my_post );
}
Thank you.