php warning issue
-
Hello,
Can you please advise how to fix this issue? Or you need to update this theme.
Jun 08, 07:58:39
Warning: Undefined array key “width” in /home/mysite.com/wp-content/themes/twentyeleven/image.php on line 39Jun 08, 07:58:39
Warning: Undefined array key “height” in /home/mysite.com/wp-content/themes/twentyeleven/image.php on line 40Thanks advance!
-
Hi @cfm168 – could you please check for me if this error still occurs when you have no plugins activated on the site?
I can find these two warnings when visiting the attachment page for an SVG because WordPress does not set their (full-size) dimensions in metadata.
Unless you would prefer disabling all image attachment pages, you could try adding a filter to set both SVG dimensions to
0:function svg_attachment_metadata_dimensions( $data ) {
if (
( ! isset( $data['width'] ) || ! isset( $data['height'] ) )
&& isset( $data['sizes']['thumbnail']['mime-type'] )
&& $data['sizes']['thumbnail']['mime-type'] === 'image/svg+xml'
) {
$data['width'] = 0;
$data['height'] = 0;
}
return $data;
}
add_filter( 'wp_get_attachment_metadata', 'svg_attachment_metadata_dimensions', 10, 1 );Hello, Thank you all.
Surprisingly the errors disappeared after installing a popular cache plugin. I will wait more days to see if it is a real.
Sorry, actually the issue persists.
Hello,
I have WP6.7.2. I try to get help from IA and get the following advise:
This error typically occurs when a script tries to access an array key that doesn’t exist. In your case, the “height” key is missing in the array being referenced in the image.php file. Here’s how you can fix it:
Check if the Key Exists: Before accessing the “height” key, ensure it exists using the isset() function. For example:
phpif (isset($array[‘height’])) {
$height = $array[‘height’];
} else {
$height = ‘default_value’; // Provide a default value if needed
}*Debug the Code: Look at the code in image.php around line 40. Identify where the array is being populated and ensure the “height” key is being set correctly.
*Update the Theme: If you’re using an outdated version of the Twenty Eleven theme, update it to the latest version. Sometimes, these errors are resolved in newer updates.
I checked the line 40. Hope the author can test it and update the theme. Or some one can help to create a related script for my Child theme as alternate solution.
Thanks in advance!
If you have a child theme, you have multiple options. The simplest might be wrapping the
printfin a condition likeif ( isset( $metadata['width'] ) && isset( $metadata['height'] ) ). That would skip adding the entry meta if the image does not define width and/or height, so you might also want anelsefallback.Steps:
- Add Twenty Eleven’s
image.phptemplate in your child theme (if you have not done that already). - Edit the
printfcode that begins after$metadata = wp_get_attachment_metadata();with one of the code snippets below (or something else, as you like). - Save changes to the file, and upload it if you are not editing within the admin.
- Visit the attachment pages for images, especially any pages you know had the error messages.
Snippet 1: Checks for width and height before outputting entry meta, and shows only the date if it does not find both dimensions.
<?php
$metadata = wp_get_attachment_metadata();
if ( isset( $metadata['width'] ) && isset( $metadata['height'] ) ) :
printf(
/* translators: 1: Time, 2: Date, 3: Image permalink, 4: Image width, 5: Image height, 6: Parent permalink, 7: Parent post title, 8: Parent post title. */
__( '<span class="meta-prep meta-prep-entry-date">Published </span> <span class="entry-date"><abbr class="published" title="%1$s">%2$s</abbr></span> at <a href="%3$s" title="Link to full-size image">%4$s × %5$s</a> in <a href="%6$s" title="Go to %7$s" rel="gallery">%8$s</a>', 'twentyeleven' ),
esc_attr( get_the_time() ),
get_the_date(),
esc_url( wp_get_attachment_url() ),
$metadata['width'],
$metadata['height'],
esc_url( get_permalink( $post->post_parent ) ),
esc_attr( strip_tags( get_the_title( $post->post_parent ) ) ),
get_the_title( $post->post_parent )
);
else:
printf(
'<span class="entry-date"><abbr class="published" title="%1$s">%2$s</abbr></span>',
esc_attr( get_the_time() ),
get_the_date()
);
endif;
?>Snippet 2: Checks for width and height individually within the entry meta
printf, and gives fallback text if the dimension is not found. (The fallback could be'0'or'unknown', for example.)<?php
$metadata = wp_get_attachment_metadata();
printf(
/* translators: 1: Time, 2: Date, 3: Image permalink, 4: Image width, 5: Image height, 6: Parent permalink, 7: Parent post title, 8: Parent post title. */
__( '<span class="meta-prep meta-prep-entry-date">Published </span> <span class="entry-date"><abbr class="published" title="%1$s">%2$s</abbr></span> at <a href="%3$s" title="Link to full-size image">%4$s × %5$s</a> in <a href="%6$s" title="Go to %7$s" rel="gallery">%8$s</a>', 'twentyeleven' ),
esc_attr( get_the_time() ),
get_the_date(),
esc_url( wp_get_attachment_url() ),
isset( $metadata['width'] ) ? $metadata['width'] : '0',
isset( $metadata['height'] ) ? $metadata['height'] : '0',
esc_url( get_permalink( $post->post_parent ) ),
esc_attr( strip_tags( get_the_title( $post->post_parent ) ) ),
get_the_title( $post->post_parent )
);
?>-
This reply was modified 1 year, 1 month ago by
Stephen Bernhardt.
Hi @sabernhardt,
Thank you so much for your help. I will try with your Snippet and see.
Hope the theme author can say something here or make an update.
Hello @sabernhardt,
I got his fatal error log today:
Fatal error: Uncaught TypeError: Cannot access offset of type string on string in /home/SorryHidedmysite/SorryHidedmysite.com/wp-includes/media.php:795
Stack Trace
image_get_intermediate_size(456, Array)
/home/SorryHidedmysite/SorryHidedmysite.com/wp-includes/media.php:236
image_downsize(456, Array)
/home/SorryHidedmysite/SorryHidedmysite.com/wp-includes/media.php:970
wp_get_attachment_image_src(456, Array, false)
/home/SorryHidedmysite/SorryHidedmysite.com/wp-includes/media.php:1062
wp_get_attachment_image(456, Array)
/home/SorryHidedmysite/SorryHidedmysite.com/wp-content/themes/twentyeleven/image.php:104
include(‘/home/chopstick…’)
/home/SorryHidedmysite/SorryHidedmysite.com/wp-includes/template-loader.php:106
require_once(‘/home/chopstick…’)
/home/SorryHidedmysite/SorryHidedmysite.com/wp-blog-header.php:19
require(‘/home/chopstick…’)
/home/SorryHidedmysite/SorryHidedmysite.com/index.php:17
{main}
thrown in /home/SorryHidedmysite/SorryHidedmysite.com/wp-includes/media.php on line 795- Mar 22, 12:59:50
- Warning: Undefined array key “width” in /home/SorryHidedmysite/SorryHidedmysite.com/wp-content/themes/twentyeleven/image.php on line 39
- Mar 22, 12:59:50
- Warning: Undefined array key “height” in /home/SorryHidedmysite/SorryHidedmysite.com/wp-content/themes/twentyeleven/image.php on line 40
Can you please advise with a code that for my child theme to avoid the above issues?
Thanks in advance!
The fatal error in
image_get_intermediate_size()is apparently a separate issue, and you already opened multiple topics about that (the discussion should stay on one of those). The array key warnings appear when the image meta does not have the width or height, but attachment 456 has both of those in the page before it stops on the critical error.Redirecting the image attachment pages to the attachment itself should be the quickest way to prevent both errors. Those pages often are not very valuable, and you might benefit from removing them entirely. This is possible without a plugin since WordPress 6.4, but the All-in-One SEO plugin has the option under Search Appearance > Image SEO.
However, if you appreciate those pages and can fix the fatal error with something else, this is another code snippet to try in a child theme or plugin for the array key warnings (similar to the SVG-related filter above, without restricting the image type).
function add_missing_attachment_metadata_dimensions( $data ) {
// A value of 0 is not useful for visitors, but it could prevent errors.
$data['width'] = isset( $data['width'] ) ? $data['width'] : 0;
$data['height'] = isset( $data['height'] ) ? $data['height'] : 0;
return $data;
}
add_filter( 'wp_get_attachment_metadata', 'add_missing_attachment_metadata_dimensions', 10, 1 );Hello @jordesign,
Yes, this error still occurs when I have no plugins activated on the site. Currently I have WP6.7.2, WC Version 9.7.1, Twenty Eleven Version: 4.8.
The error log as below:
Mar 30, 10:59:43
Warning: Undefined array key “width” in /home/SorryHidedmysite/SorryHidedmysite.com/wp-content/themes/twentyeleven/image.php on line 39Mar 30, 10:59:43
Warning: Undefined array key “height” in /home/SorryHidedmysite/SorryHidedmysite.com/wp-content/themes/twentyeleven/image.php on line 40Log file: /home/SorryHidedmysite/php-errors.log (341 bytes)
Hopefully you will have new update for it soon. Please advise. Thanks!
I have theme version 4.8, WP 6.7.2 . Deactivated all plugins after 6 hours so far found the errors related to the theme:
Apr 05, 16:10:36
Warning: Undefined array key “width” in /home/SorryHidedmysitesfactory/SorryHidedmysitesfactory.com/wp-content/themes/twentyeleven/image.php on line 39Apr 05, 16:10:36
Warning: Undefined array key “height” in /home/SorryHidedmysitesfactory/SorryHidedmysitesfactory.com/wp-content/themes/twentyeleven/image.php on line 40Hope theme author to update soon.
Hello,
I have WP6.8, theme version 4.9. Deactivated all plugins by Health check plugin test, the php warning issue persists.
This is only error report I got:
Apr 19, 23:15:40
Warning: Undefined array key “width” in /home/SorryHidedmysite/SorryHidedmysite.com/wp-content/themes/twentyeleven/image.php on line 39
Apr 19, 23:15:40
Warning: Undefined array key “height” in /home/SorryHidedmysite/SorryHidedmysite.com/wp-content/themes/twentyeleven/image.php on line 40
Apr 20, 04:12:55
Warning: Undefined array key “width” in /home/SorryHidedmysite/SorryHidedmysite.com/wp-content/themes/twentyeleven/image.php on line 39
Apr 20, 04:12:55
Warning: Undefined array key “height” in /home/SorryHidedmysite/SorryHidedmysite.com/wp-content/themes/twentyeleven/image.php on line 40
Apr 20, 04:49:57
Warning: Undefined array key “width” in /home/SorryHidedmysite/SorryHidedmysite.com/wp-content/themes/twentyeleven/image.php on line 39
Apr 20, 04:49:57
Warning: Undefined array key “height” in /home/SorryHidedmysite/SorryHidedmysite.com/wp-content/themes/twentyeleven/image.php on line 40Theme version 4.9. does not resolve this errors.
This php warning issue/error disappeared (did not show up for more than a week) with unknown reason, but the following Fatal error found instead:
Apr 25, 16:02:35
Fatal error: Uncaught TypeError: Cannot access offset of type string on string in /home/SorryHidedmysite/SorryHidedmysite.com/wp-includes/media.php:800
Stack Trace
image_get_intermediate_size(309, Array)
/home/SorryHidedmysite/SorryHidedmysite.com/wp-includes/media.php:241
image_downsize(309, Array)
/home/SorryHidedmysite/SorryHidedmysite.com/wp-includes/media.php:975
wp_get_attachment_image_src(309, Array, false)
/home/SorryHidedmysite/SorryHidedmysite.com/wp-includes/media.php:1067
wp_get_attachment_image(309, Array)
/home/SorryHidedmysite/SorryHidedmysite.com/wp-content/themes/twentyeleven/image.php:105
include(‘/home/chopstick…’)
/home/SorryHidedmysite/SorryHidedmysite.com/wp-includes/template-loader.php:106
require_once(‘/home/chopstick…’)
/home/SorryHidedmysite/SorryHidedmysite.com/wp-blog-header.php:19
require(‘/home/hided…’)
/home/SorryHidedmysite/SorryHidedmysite.com/index.php:17
{main}
thrown in /home/SorryHidedmysite/SorryHidedmysite.com/wp-includes/media.php on line 800
Fix this fatal error may be can resolve the php warning error. Hope developer can solve this issue.
(I have WP6.8, Theme v4.9 and WC9.8.2. Tested with Health check plugin and deactivated all plugins the fatal error persists).
I’m running WP6.8.1, WC9.8.5 and the theme 4.9. now.
Good news is that, there is no any error report since 10 days ago. I will keep watching and update here.
- Add Twenty Eleven’s
The topic ‘php warning issue’ is closed to new replies.
