1

Warning: array_keys() expects parameter 1 to be array, string given in /home/hennacur/public_html/shop/wp-content/themes/montezuma/includes/image_meta.php on line 34

Warning: Invalid argument supplied for foreach() in /home/hennacur/public_html/shop/wp-content/themes/montezuma/includes/image_meta.php on line 40

                                                 function bfa_image_size() {
$meta = wp_get_attachment_metadata();
echo $meta['width']. '×' . $meta['height'];
    }


    function bfa_image_meta( $args = '' ) {

$defaults = array(
    'keys' => '',
    'before' => '', 
    'after' => '',
    'item_before' => '', 
    'item_after' => '',
    'item_sep' => ' · ',
    'key_before' => '',
    'key_after' => ': ',
    'value_before' => '',
    'value_after' => '',
    'display_empty' => FALSE    
);

$r = wp_parse_args( $args, $defaults );
extract( $r, EXTR_SKIP );

$meta = wp_get_attachment_metadata();

$string_array = array();

       // All keys, alphabetically sorted, as provided by wp_get_attachment_metadata()
if( $keys == '' ) {
    $array_keys = array_keys( $meta['image_meta'] );  ---***line34***       
// Only keys specificed in parameter:
} else {
    $array_keys = array_map( 'trim', explode( ',', $keys ) );
}

foreach( $array_keys as $key ) { --***line 40***

    $value = $meta['image_meta'][$key];

    if( $display_empty === TRUE || ( $value != '' && $value != '0' ) ) {

        if( $key == 'created_timestamp' )
            // Transform timestamp into readable date, based on default WP date/time settings:
            $value = date( get_option('date_format') . ' - ' . get_option('time_format'), $value );

        // Prettify key
        $key = ucwords( str_replace( '_', ' ', $key ) );
        $key = $key == 'Iso' ? 'ISO' : $key;


        $key = str_replace( 
            array(
                'Aperture',
                'Credit',
                'Camera',
                'Caption',
                'Created Timestamp',
                'Copyright',
                'Focal Length',
                'ISO',
                'Shutter Speed',
                'Title'
            ),
            array(
                __( 'Aperture', 'montezuma' ),
                __( 'Credit', 'montezuma' ),
                __( 'Camera', 'montezuma' ),
                __( 'Caption', 'montezuma' ),
                __( 'Timestamp', 'montezuma' ),
                __( 'Copyright', 'montezuma' ),
                __( 'Focal Length', 'montezuma' ),
                __( 'ISO', 'montezuma' ),
                __( 'Shutter Speed', 'montezuma' ),
                __( 'Title', 'montezuma' )
            ),      
            $key
        );

I have the above code. What could be cause of the warnings that are appearing?

1
  • Resisit from naming your variables like reserved words - it's just a good habit that will keep you away from errors in future. And add var_dump($meta['image_meta']); to see what $meta['image_meta'] really is and contains Commented Nov 25, 2012 at 17:41

2 Answers 2

1

Try this (you can see this warning because $array_keys have type as string or something alse) if stetmen:

 if(is_array($array_keys) && !empty($array_keys)) { // foreach stetment here }
Sign up to request clarification or add additional context in comments.

Comments

0

The warning explains it all.

array_keys() expects parameter 1 to be array, string given is telling you that a string was passed to array_keys() instead of an array.

Thus, $meta['image_meta'] is probably not an array.

As a result, $array_keys is not being set as an array, leading to your next warning, when you try to apply foreach to it.

4 Comments

function bfa_image_size() { $meta = wp_get_attachment_metadata(); echo $meta['width']. '×' . $meta['height']; } function bfa_image_meta( $args = '' ) { $defaults = array( 'keys' => '', 'before' => '', 'after' => '', 'item_before' => '', 'item_after' => '', 'item_sep' => ' · ', 'key_before' => '', 'key_after' => ': ', 'value_before' => '', 'value_after' => '', 'display_empty' => FALSE ); $r = wp_parse_args( $args, $defaults ); extract( $r, EXTR_SKIP ); $meta = wp_get_attachment_metadata(); $string_array = array();
@karu: Please don't put code in a comment. It become incomprehensible. Also... what are you trying to say in this comment?
actually it is defined as a array
If, as WebnetMobile.com suggests, you add var_dump($meta['image_meta']); to your code, you'll probably find that it's not. PHP is warning you that it isn't.

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.