Assign sub-field value to the variable

Support MB Group Assign sub-field value to the variableResolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #33272
    Prabakaran ShankarPrabakaran Shankar
    Participant

    Hello,

    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.

    #33277
    Long NguyenLong Nguyen
    Moderator

    Hi,

    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?

    #33281
    Prabakaran ShankarPrabakaran Shankar
    Participant

    Hello,

    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,
                        ],
                    ],
                ],
    #33285
    Long NguyenLong Nguyen
    Moderator

    Hi,

    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/

    #33297
    Prabakaran ShankarPrabakaran Shankar
    Participant

    Thank you so much. It is working.

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