Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
Using a taxonomy field on an options page, when saving the value, it throws a fatal error:
Fatal error: Uncaught Error: Cannot use object of type WP_Term as array in /wp-content/plugins/acfml/classes/strategy/repeater-shuffle/OptionsPage.php:89
Solution:
https://wpml.org/errata/advanced-custom-fields-pro-fatal-error-when-using-theme-options-with-a-taxonomy-field/

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

Tagged: ,

This topic contains 10 replies, has 2 voices.

Last updated by sissiB 1 year, 7 months ago.

Assisted by: Long Nguyen.

Author Posts
March 23, 2023 at 12:22 pm #13315057

sissiB

Fatal error: Uncaught Error: Cannot use object of type WP_Term as array in contentpluginsacfmlclassesstrategyrepeater-shuffleOptionsPage.php:89

March 27, 2023 at 2:55 am #13333425

Long Nguyen
Supporter

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi Sissi,

Thank you for contacting WPML support, I'd be happy to help you with this issue.

I've tried to create a taxonomy field in a field group, assign it to an options page and save the field value on the options page but do not see that issue. Please enable a minimal environment as below and see if it helps:
- deactivate all plugins except WPML, ACF and their addon plugins
- switch to a standard theme of WordPress
❌ IMPORTANT: Please backup your database and website before proceeding ❌
You can use this plugin to create the backup: 
https://wordpress.org/plugins/duplicator/

I also need more information on your site by sharing the debug info. If you don’t know how to share the debug info, please follow this article https://wpml.org/faq/provide-debug-information-faster-support/
Your next reply is enabled to share the debug info.

Look forward to your reply.
Thanks

March 27, 2023 at 9:18 am #13336069

sissiB

Hello,
it seems that happens when a field of the type acfe_taxonomy_terms is tried to be saved. that comes from the plugin "acf extended pro", here an example of my json field settings.

{
            "key": "field_63ff9ba8cd283",
            "label": "Filter Nachhaltigkeit",
            "name": "filter_nachhaltigkeit",
            "aria-label": "",
            "type": "acfe_taxonomy_terms",
            "instructions": "",
            "required": 0,
            "conditional_logic": 0,
            "wrapper": {
                "width": "33",
                "class": "",
                "id": ""
            },
            "wpml_cf_preferences": 3,
            "taxonomy": [
                "tx-filter"
            ],
            "allow_terms": "",
            "allow_level": "",
            "field_type": "select",
            "max": "",
            "default_value": [],
            "return_format": "object",
            "ui": 0,
            "allow_null": 0,
            "multiple": 0,
            "save_terms": 0,
            "load_terms": 0,
            "acfe_field_group_condition": 0,
            "choices": [],
            "ajax": 0,
            "placeholder": "",
            "search_placeholder": "",
            "layout": "",
            "toggle": 0,
            "allow_custom": 0,
            "other_choice": 0,
            "min": ""
        },

I will also create a ticket at acf extended about this issue.
Best, Sissi

March 27, 2023 at 10:04 am #13336797

Long Nguyen
Supporter

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi,

Thank you for your feedback.

It could be a compatibility issue between ACF Extended and WPML, we will address that by reporting this to the compatibility team.

To be able to report the compatibility issues we need to replicate the issue on a clean installation. I created a clean installation of WordPress, WPML, and all necessary WPML add-ons. You can access the WordPress dashboard using the link below:
hidden link

Kindly follow the steps below:

- Install ACF Extended plugin.
- Try to replicate the issue.
- Then let me know the steps to replicate it.

This will help us to report the probable issue to the compatibility team and solve the possible problem faster.

Look forward to your reply.
Thanks

March 27, 2023 at 11:51 am #13338127

sissiB

Hello,

alright, I could reproduce the error (or an error, since I cannot see the error log). I created an options page in that a field group with a taxonomy term with the settings I have) and on saving the options page the error appears.

March 27, 2023 at 12:21 pm #13338305

sissiB

Also here is the reply from ACF Extended Developer - it seems that happens also just with ACF Pro:

Hello,

Thanks for the feedback!

It looks like the issue come from the ACFML source code here, as suggests the error message. In fact, in the ACFML code /acfml/classes/strategy/repeater-shuffle/OptionsPage.php line:89, WPML does the following conditional to check the value during their save logic:

if ( $value instanceof \WP_Post || isset( $value['ID'] ) ) {
However, in the case of the ACFE Taxonomy Terms field, the $value here is a WP_Term object, so the first condition is not met. Then the code try the other condition: isset( $value['ID'] ), but it doesn't doesn't check if $value is an array before trying to accessing the ID key, and since the value is an object, PHP throws an error.

By the way, that same error can be reproduced with ACF Pro only (without ACF Extended), using the native ACF Taxonomy Field displayed as "Select" and with a "Return Format: Term Object". This is because the ACFML code logic rely on get_fields() line:72 with format_value = true, which means all values will be formatted as in the Field UI Settings (see documentation). I wouldn't recommend to enable the format_value in get_fields() so the logic can assume that it will always retrieve raw database data.

I would recommend to contact the WPML support to let them know.

Here is the Field Group json of a native ACF Taxonomy Field (without ACF Extended) producing the same issue, if you want to test it:

[
    {
        "key": "group_642182b1190c4",
        "title": "My Options Page",
        "fields": [
            {
                "key": "field_642184bf457ea",
                "label": "Taxonomy",
                "name": "taxonomy",
                "aria-label": "",
                "type": "taxonomy",
                "instructions": "",
                "required": 0,
                "conditional_logic": 0,
                "wrapper": {
                    "width": "",
                    "class": "",
                    "id": ""
                },
                "wpml_cf_preferences": 3,
                "acfe_save_meta": 0,
                "taxonomy": "category",
                "add_term": 0,
                "save_terms": 0,
                "load_terms": 0,
                "return_format": "object",
                "field_type": "select",
                "allow_null": 0,
                "multiple": 0,
                "min": "",
                "max": ""
            }
        ],
        "location": [
            [
                {
                    "param": "options_page",
                    "operator": "==",
                    "value": "my-options-page"
                }
            ]
        ],
        "menu_order": 0,
        "position": "normal",
        "style": "default",
        "label_placement": "left",
        "instruction_placement": "label",
        "hide_on_screen": "",
        "active": true,
        "description": "",
        "show_in_rest": 0,
        "acfml_field_group_mode": "localization",
    }
]
Hope it helps!

Have a nice day!

Regards.
March 28, 2023 at 2:27 am #13344453

Long Nguyen
Supporter

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi,

Thank you for your feedback.

I'm asking our 2nd tier support about this issue and I will update you here when I have something to share.

There is one note: If you use the ACF Pro taxonomy field, you can use the Appearance option multiple values: Checkbox or Multi Select to prevent this issue. Please check this screenshot hidden link

Thanks.

March 28, 2023 at 10:57 am #13348303

sissiB

Hello,

even if I change the layout option to any other option I still get the same php error, even if I switch to "multiple values" allowed ...

March 29, 2023 at 2:44 am #13353971

Long Nguyen
Supporter

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi,

Thank you for your feedback.

I've escalated this issue to our development team. I'll update you as soon as I have something to share.

Thanks.

March 29, 2023 at 9:55 am #13356365

Long Nguyen
Supporter

Languages: English (English )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Hi,

I got a workaround from our 2nd tier support, please check it below.

❌ IMPORTANT: Please backup your database and website before proceeding ❌

Edit the file wp-content/plugins/acfml/classes/strategy/repeater-shuffle/OptionsPage.php, around line 88-107, replace the code

private function addNormalizedValuesForFieldState( $options, $prefix, $key, $value ) {
	if ( $value instanceof \WP_Post || isset( $value['ID'] ) ) {
		return array_merge( $options, [ "${prefix}${key}" => Obj::prop( 'ID', $value ) ] );
	} elseif ( $this->isArrayOfStrings( $value ) ) {
		return array_merge( $options, [ "${prefix}${key}" => $value ] );
	} elseif ( is_array( $value ) ) {
		foreach ( $value as $index => $item ) {
			if ( is_numeric( $index ) ) {
				foreach ( $item as $field => $field_value ) {
					$options = array_merge( $options, $this->addNormalizedValuesForFieldState( $options, "${prefix}${key}_${index}_", $field, $field_value ) );
				}
			} else {
				$options = $this->addNormalizedValuesForFieldState( $options, "${prefix}${key}_", $index, $item );
			}
		}
		return $options;
	} else {
		return array_merge( $options, [ "${prefix}${key}" => $value ] );
	}
}

with this one

private function addNormalizedValuesForFieldState( $options, $prefix, $key, $value ) {
	if ( $value instanceof \WP_Post || is_array($value) && isset( $value['ID'] ) ) {
		return array_merge( $options, [ "${prefix}${key}" => Obj::prop( 'ID', $value ) ] );
	} elseif ( $value instanceof \WP_Term || isset( $value->term_id ) ) {
		return array_merge( $options, [ "${prefix}${key}" => Obj::prop( 'term_id', $value ) ] );
	} elseif ( $this->isArrayOfStrings( $value ) ) {
		return array_merge( $options, [ "${prefix}${key}" => $value ] );
	}  elseif ( is_array( $value ) ) {
		foreach ( $value as $index => $item ) {
			if ( is_numeric( $index ) ) {
				foreach ( $item as $field => $field_value ) {
					$options = array_merge( $options, $this->addNormalizedValuesForFieldState( $options, "${prefix}${key}_${index}_", $field, $field_value ) );
				}
			} else {
				$options = $this->addNormalizedValuesForFieldState( $options, "${prefix}${key}_", $index, $item );
			}
		}

		return $options;
	} else {
		return array_merge( $options, [ "${prefix}${key}" => $value ] );
	}
}

You can keep using the workaround until the next update of ACFML includes the fix for this issue.

Look forward to your reply.
Thanks

March 29, 2023 at 10:52 am #13357029

sissiB

That works, thank you.