Shortcode vs
- This topic has 3 replies, 2 voices, and was last updated 3 years, 7 months ago by
Long Nguyen.
-
AuthorPosts
-
September 2, 2021 at 10:39 AM #30548
Ghislain Malardier
ParticipantHi,
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.
September 3, 2021 at 12:27 PM #30566Long Nguyen
ModeratorHi,
I think it's not a good idea because you don't know exactly the meta key that is saved into the database based on the field
custom_sms_text
value. Using the shortcode to show the SMS text would be better.September 3, 2021 at 10:54 PM #30574Ghislain Malardier
ParticipantHi,
Thank for the feedback. But I'm not sure to understand what you mean by "you don't know exactly the meta key that is saved into the database based".The "custom_sms_text" field is already created, but until now, I had to manually enter the information in it. So the idea is to automatically combine and save the information into that field.
The shortcode works great, but if I'm right, it means that the website is processing the information on every page load. Knowing that this SMS field pretty much never change once created for the user, I though having it save in the custom field would save some processing power.
Now, I have two sets of custom fields. The first one "contact-card-fields" is the custom fields visible to the users (front-end submission), and the "contact-card-fields-admin" only visible to administrator of the site.
The "custom_sms_text" is part of the admin fields, but the other fields (the one to combine) are part of the user fields.Could that be the reason why I isn't working on my side?
Thanks again for your help!
September 4, 2021 at 10:29 AM #30578Long Nguyen
ModeratorHi,
- Regarding the shortcode:
You should wrap the SMS link in a<a>
tag to allow the user to click on a button to send the SMS instead of sending SMS on page load. Like this - Regarding the admin field:
Ok, I understand your expectation. If you want to update a value to a field, please use the function update_post_meta(). The function to update the admin field value should be
return '<a href="sms:12345">Link</a>';
function admin_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(); $text_message = 'sms:'.$meta_phone.'?&body=Hey there, it\'s '.$meta_name.'. '.$meta_custom_message.' '.$page_url; update_post_meta( $post_id, 'custom_sms_text', $text_message ); //here, the field ID (meta key) should be predefined }
- Regarding the shortcode:
-
AuthorPosts
- You must be logged in to reply to this topic.