skip to Main Content

Updating PHP on old WordPress website got an Uncaught Error: Call to undefined function each() in 186 line of class-vc-mapper.php

public function callElementActivities( $tag ) {
        do_action( 'vc_mapper_call_activities_before' );
        if ( isset( $this->element_activities[ $tag ] ) ) {
                while ( $activity = each( $this->element_activities[ $tag ] ) ) {  << line
                list ( $method, $params ) = $activity[1];
                switch ( $method ) {
                    case 'drop_param':
                        WPBMap::dropParam( $params['name'], $params['attribute_name'] );
                        break;
                    case 'add_param':
                        WPBMap::addParam( $params['name'], $params['attribute'] );
                        break;
                    case 'mutate_param':
                        WPBMap::mutateParam( $params['name'], $params['attribute'] );
                        break;
                    case 'drop_shortcode':
                        WPBMap::dropShortcode( $params['name'] );
                        break;
                    case 'modify':
                        WPBMap::modify( $params['name'], $params['setting_name'], $params['value'] );
                        break;
                }
            }
        }

    }

I believe that it should be changed for ‘foreach’ but I don’t know how to do it correctly.
Pls help me 😀

2

Answers


  1. public function callElementActivities( $tag ) {
        do_action( 'vc_mapper_call_activities_before' );
        if ( isset( $this->element_activities[ $tag ] ) ) {
            foreach ( $this->element_activities[ $tag ] as $activity ) {
                list ( $method, $params ) = $activity;
                switch ( $method ) {
                    case 'drop_param':
                        WPBMap::dropParam( $params['name'], $params['attribute_name'] );
                        break;
                    case 'add_param':
                        WPBMap::addParam( $params['name'], $params['attribute'] );
                        break;
                    case 'mutate_param':
                        WPBMap::mutateParam( $params['name'], $params['attribute'] );
                        break;
                    case 'drop_shortcode':
                        WPBMap::dropShortcode( $params['name'] );
                        break;
                    case 'modify':
                        WPBMap::modify( $params['name'], $params['setting_name'], $params['value'] );
                        break;
                }
            }
        }
    }
    
    Login or Signup to reply.
  2. Note that each() function has been deprecated as of PHP 7.2.0, and REMOVED as of PHP 8.0.0. Relying on this function is highly discouraged.

    Also note that each() function is typically used in conjunction with list() to traverse an array.

    So the alternative is to use the foreach() function on the array key / value pairs (but not only the values), to traverse $this->element_activities[ $tag ] array.

    So the correct revisited working code should be:

    public function callElementActivities( $tag ) {
        do_action( 'vc_mapper_call_activities_before' );
    
        if ( isset( $this->element_activities[ $tag ] ) ) {
            foreach ( $this->element_activities[ $tag ] as $method => $params ) {
                switch ( $method ) {
                    case 'drop_param':
                        WPBMap::dropParam( $params['name'], $params['attribute_name'] );
                        break;
                    case 'add_param':
                        WPBMap::addParam( $params['name'], $params['attribute'] );
                        break;
                    case 'mutate_param':
                        WPBMap::mutateParam( $params['name'], $params['attribute'] );
                        break;
                    case 'drop_shortcode':
                        WPBMap::dropShortcode( $params['name'] );
                        break;
                    case 'modify':
                        WPBMap::modify( $params['name'], $params['setting_name'], $params['value'] );
                        break;
                }
            }
        }
    }
    

    IT should work now.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search