1

I get this error when this code is run as a Wordpress code snippet using WP Code plugin. I'm sure it has to do with the code provided by the plugin developer being out of date, not in-line with current PHP version but I don't have the skills on how to update the code and haven't heard back from plugin developer.

--> Trying to access array offset on value of type bool

There are two parts to the code doing different things, but the error is in the second. The line of code its highlighting as the problem is where it says:

<input type="text" name="custom_text_field" class="form-control" value= <?php print_r($hh[0]) ?> />

Can anyone assist in how to update this to get avoid the error?



/** * Add Custom Tab in add product page.   
* @author WC Marketplace    * @Version 3.3.0    
*/  
function add_custom_product_data_tabs( $tabs ) 
{      
$tabs['advanced'] = array
('label'=> __( 'Delivery Time', 'your-text-domain' ),          
'target'   => 'custom_tab_product_data',
'class'=> array(),
'priority' => 100,     
);     
return $tabs;   
}   
add_filter( 'mvx_product_data_tabs', 'add_custom_product_data_tabs' );


/*** Add Custom Tab content in add product page.    
* @author WC Marketplace    
* @Version 3.3.0    
*/  
function add_custom_product_data_content( $pro_class_obj, $product, $post ) 
{     
$hh = get_post_meta($product->get_id() , '_custom_text_field' );        
?>     
<div role="tabpanel" class="tab-pane fade" id="custom_tab_product_data"><!-- just make sure tabpanel id should replace with your added tab target -->          
<div class="row-padding">              
<div class="form-group">                   
<label class="control-label col-sm-3 col-md-3">Estimated Delivery time</label>                 <div class="col-md-6 col-sm-9">                     
**<input type="text" name="custom_text_field" class="form-control" value= <?php print_r($hh[0]) ?> />**                
</div>             
</div>         
</div>     
</div>     
<?php   
}   
add_action( 'mvx_product_tabs_content', 'add_custom_product_data_content', 10, 3 );

I've tried to look up how to change this and get an idea but as I don't have the skills to change I hope someone can help :-)

5
  • 1
    Seems $hh is not returning anything. Put condition before you access offset for it. Like !empty($hh) ? $hh[0] : '' Moreover you should add your print statement outside of the value attribute. Commented Jul 7, 2024 at 5:26
  • developer.wordpress.org/reference/functions/get_post_meta this function can return false, you should validate what you receive from this functions return before consuming it. or use this shorthand to print an empty string as a fall back echo $hh[0] ?? ''; Commented Jul 7, 2024 at 6:28
  • Thank you! Where do I put this code in? Commented Jul 7, 2024 at 7:46
  • print_r($hh[0] ?? '') Commented Jul 7, 2024 at 10:52
  • Thank you - this gave me some good thoughts and could approach the developer confidently. I was advised to use "echo esc_attr " which seems to have resolved the issue. Commented Jul 10, 2024 at 5:11

1 Answer 1

1

Thank you - this gave me some good thoughts and could approach the developer confidently. I was advised to use "echo esc_attr " which seems to have resolved the issue.

 <input type="text" name="image_dimensions" class="form-control" value="<?php echo esc_attr( $image_dimensions ); ?>" />
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.