Assign sub-field value to the variable
- This topic has 4 replies, 2 voices, and was last updated 3 years, 3 months ago by
Prabakaran Shankar.
-
AuthorPosts
-
January 17, 2022 at 3:16 AM #33272
Prabakaran Shankar
ParticipantHello,
I added the below code for the function based on the group field value.
add_shortcode( 'deadline_date_display', function() { ob_start(); global $post; $new_date = rwmb_meta( "deadline_date" ); $new_date_format = date('m/d/Y'); echo '<p style="color:red; font-size:20px; font-weight: bold;">'; if( !empty( $new_date ) ){ $new_date_format = wp_date('D, d M Y (H:i T)', $new_date); echo '<strong>Apply before the Deadline</strong>' . '<br>'; echo '<span style="color:black;">' . esc_html( $new_date_format ) . '</span>'; } else { echo esc_textarea( 'Open until filled' ); } echo '</p>'; $old_url_email = rwmb_meta( "j_url_email" ); if( !empty( $old_url_email ) ){ echo '<a href="' . esc_html( $old_url_email ) . '" target="_blank" rel="noopener">Apply Now</a>'; } else{ $app_details = rwmb_meta( 'app_details' ); $method = $app_details['method']; //Error message: Uncaught TypeError: Cannot access offset of type string on string if($method == 'online'){ $apply_now = $app_details['url']; echo '<a href="' . esc_html( $apply_now ) . '" target="_blank" rel="noopener">Apply Now</a>' . '<br><strong>' . esc_textarea('Online Application') . '</strong>'; } elseif($method == 'email'){ $apply_now = $app_details['email']; echo '<a href="' . esc_html( $apply_now ) . '" target="_blank" rel="noopener">Apply Now</a>' . '<br><strong>' . esc_textarea('eMail Application') . '</strong>'; } } return ob_get_clean(); );
I'm getting the error message (Uncaught TypeError: Cannot access offset of type string on string). How to solve this.
Thanks.
January 17, 2022 at 11:03 AM #33277Long Nguyen
ModeratorHi,
Can you please share the code that creates a field group on your site? And let me know where did you add the shortcode to show the value?
January 17, 2022 at 2:01 PM #33281Prabakaran Shankar
ParticipantHello,
I added the shortcode in the function.php/Child theme and used that shortcode in the elements module [Generatepress] for page template.
Group metabox code is below
[ 'name' => __( 'Application Details', 'your-text-domain' ), 'id' => $prefix . 'app_details', 'type' => 'group', 'fields' => [ [ 'name' => __( 'Application Method', 'your-text-domain' ), 'id' => $prefix . 'method', 'type' => 'radio', 'options' => [ 'online' => __( 'Online Application', 'your-text-domain' ), 'email' => __( 'Send Email', 'your-text-domain' ), ], 'required' => true, 'inline' => false, ], [ 'name' => __( 'Url', 'your-text-domain' ), 'id' => $prefix . 'url', 'type' => 'url', 'desc' => __( 'paste the application url', 'your-text-domain' ), 'required' => true, 'visible' => [ 'when' => [['method', '=', 'online']], 'relation' => 'or', ], ], [ 'name' => __( 'Email', 'your-text-domain' ), 'id' => $prefix . 'email', 'type' => 'email', 'desc' => __( 'paste the email address', 'your-text-domain' ), 'required' => true, 'prepend' => 'mailto:', 'visible' => [ 'when' => [['method', '=', 'email']], 'relation' => 'or', ], ], [ 'name' => __( 'Ref No', 'your-text-domain' ), 'id' => $prefix . 'ref_no', 'type' => 'text', 'label_description' => __( 'Job Reference Number or Text given in the Post', 'your-text-domain' ), 'desc' => __( 'Default \'-\'', 'your-text-domain' ), 'required' => true, ], [ 'name' => __( 'Application Procedure', 'your-text-domain' ), 'id' => $prefix . 'procedure', 'type' => 'wysiwyg', 'label_description' => __( 'Paste the content from job advertisement', 'your-text-domain' ), 'desc' => __( 'Default - Basic documents required for the application', 'your-text-domain' ), 'required' => true, ], ], ],
January 17, 2022 at 7:45 PM #33285Long Nguyen
ModeratorHi,
If you do not pass the third parameter to the helper function
rwmb_meta()
, it will use the current post ID. So if you add the shortcode to the template builder, the helper function will use the ID of the template and try to get the field value of this post because the template is also a post type then the helper function will return nothing and you will see an error message like that.You can check the group value is not empty to avoid showing the error message.
$app_details = rwmb_meta( 'app_details' ); if( !empty( $app_details ) ) { $method = $app_details['method']; ... }
Read more on the documentation https://docs.metabox.io/rwmb-meta/
January 18, 2022 at 11:17 AM #33297Prabakaran Shankar
ParticipantThank you so much. It is working.
-
AuthorPosts
- You must be logged in to reply to this topic.