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.

This topic contains 12 replies, has 2 voices.

Last updated by Radu 2 months, 3 weeks ago.

Assigned support staff: Radu.

Author Posts
June 17, 2019 at 6:26 pm #4037771

uditS

Since WordPress 5.2 there is a built-in feature that detects when a plugin or theme causes a fatal error on your site, and notifies you with this automated email.

In this case, WordPress caught an error with one of your plugins, WPML Multilingual CMS.

Error Details
=============
An error of type E_ERROR was caused in line 629 of the file {SITE}/wp-content/plugins/sitepress-multilingual-cms/inc/taxonomy-term-translation/wpml-term-translations.class.php. Error message: Uncaught Error: Cannot use object of type WP_Error as array in {SITE}/wp-content/plugins/sitepress-multilingual-cms/inc/taxonomy-term-translation/wpml-term-translations.class.php:629

There is this code on line no 629 in /sitepress-multilingual-cms/inc/taxonomy-term-translation/wpml-term-translations.class.php

$new_term = wp_insert_term( $term_name, $taxonomy, array( 'slug' => self::term_unique_slug( sanitize_title( $term_name ), $taxonomy, $post_lang ) ) );

if ( isset( $new_term[ 'term_taxonomy_id' ] ) ) {
							
}

wp_insert_term is expected to return an array or WP_Error object. There should be a check if $new_term is an array or a WP_Error object. If it is a WP_Error object then please show error accordingly.

June 18, 2019 at 7:42 am #4041027

Radu
Supporter

Languages: English (English )

Timezone: Europe/Bucharest (GMT+03:00)

Hey,

Thank you for reaching out the WPML support!

Can you make sure that you're using the latest version of the plugins, please? Also, is this happening when you're trying to do something specific in your website like editing a translation, for example? I just need a few more details so I can try to replicate the issue on my side and see why is this happening.

Thanks,
Radu

June 19, 2019 at 9:06 am #4049787

uditS

Hi Radu,

Thanks for your reply.

We are using the following versions:

Wordpress: Version 5.2.2
WPML Multilingual CMS: Version 4.2.7.1

I can't provide you any steps to reproduce the issue, because this issue was reported in error email by WordPress. We don't even what original error was as it was overwritten by another unhandled error (thrown by your plugin).

I have pointed out specific lines of code which are responsible for this buggy behavior. If you can look into code file, then you can clearly see the cause of this issue. Let me explain to you again.

You can see this code on line no 628 in /sitepress-multilingual-cms/inc/taxonomy-term-translation/wpml-term-translations.class.php

$new_term = wp_insert_term($term_name, $taxonomy, array('slug' => self::term_unique_slug(sanitize_title($term_name), $taxonomy, $post_lang)));

if (isset($new_term['term_taxonomy_id'])) {
    $ttid_in_correct_lang = $new_term['term_taxonomy_id'];
    $trid = $bulk ? $sitepress->get_element_trid($ttid, 'tax_' . $taxonomy) : false;
    $sitepress->set_element_language_details($ttid_in_correct_lang, 'tax_' . $taxonomy, $trid, $post_lang);
}

If you refer to https://codex.wordpress.org/Function_Reference/wp_insert_term, you can see that "wp_insert_term" is expected to return value of type "array|WP_Error". Which means it can return either an array or a WP_Error object. If there is any error while creating term, wp_insert_term will return a WP_Error object but in line no 629, you are treating returned value as an array without checking the type of returned value.

Your code suppresses any error returned by "wp_insert_term" function.

Thanks

June 19, 2019 at 1:22 pm #4051803

Radu
Supporter

Languages: English (English )

Timezone: Europe/Bucharest (GMT+03:00)

Hey,

I understand what you said and I checked the wp_insert_term documentation the first time. I think that a call to the is_wp_error function like:

if ( isset( $new_term[ 'term_taxonomy_id' ] ) && !is_wp_error( $new_term ) ) {

might take care of this. But first I need to see how can I trigger this issue. This way I can check if this little change is doing the trick. I guess that the dashboard was inaccessible when this error was triggered. Have you managed to access to the dashboard? All this info would help me replicate the issue and look for a fix.

Thanks,
Radu

June 20, 2019 at 5:55 am #4056755

uditS

Hi Radu,

You are making me worried about code used in WPML plugins. First of all your solution won't work because of logical operator precedence. It works from left to right. So the correct version should be

if (!is_wp_error( $new_term ) && isset( $new_term[ 'term_taxonomy_id' ] ) ) {

The second thing is WordPress returned wp_error object for a reason. It implies that due to some reason term is not inserted in "term" table. In that case, we must show some warning etc.

As for info to reproduce the issue, I didn't see any error. It was reported by WordPress in email. So I am not sure how to reproduce it. However, one way to test your code should be to create a duplicate category.

Thanks

June 25, 2019 at 1:27 pm #4084915

Radu
Supporter

Languages: English (English )

Timezone: Europe/Bucharest (GMT+03:00)

Hey,

Can you tell me if this error was thrown again on your website, please? If yes, do you remember what was the last change that you made? I'm asking this because I want to replicate the issue on my side. I responded a bit late because I tried to create terms translations in order to see this error but everything worked fine every time. We could just add an error check here but I need to be able to replicate the issue in order to check how it can be handled.

Thanks,
Radu

June 26, 2019 at 5:53 am #4089199

uditS

Hi,

I haven't seen any error on my website. So I don't know what triggered this error. I received this error in an email from my website (WordPress's inbuilt error reporting system). When I checked the reason for error in files, I came across this condition.

This is content of email received.

Since WordPress 5.2 there is a built-in feature that detects when a plugin or theme causes a fatal error on your site, and notifies you with this automated email.

In this case, WordPress caught an error with one of your plugins, WPML Multilingual CMS.

Error Details
=============
An error of type E_ERROR was caused in line 629 of the file {SITE}/wp-content/plugins/sitepress-multilingual-cms/inc/taxonomy-term-translation/wpml-term-translations.class.php. Error message: Uncaught Error: Cannot use object of type WP_Error as array in {SITE}/wp-content/plugins/sitepress-multilingual-cms/inc/taxonomy-term-translation/wpml-term-translations.class.php:629

As you can see in the error, details related to original error are suppressed due to another error in "$new_term[ 'term_taxonomy_id' ]". In my opinion, this condition must be fixed and error must be logged/shown if an object of WP_Error is returned.

Thanks

June 26, 2019 at 11:27 am #4092021

Radu
Supporter

Languages: English (English )

Timezone: Europe/Bucharest (GMT+03:00)

Hey,

Since I cannot replicate the issue, all I can do just report this to the development team and let them add a check for the case when the variable is WP_Error.
So, as you said, this error message only appeared once but you don't remember what was the change that you did on your website in order to make it appear, am I right? I just want to make sure that I pass all the info you have about this since we don't have any specific steps to make the issue appear.

Thanks,
Radu

June 27, 2019 at 5:20 am #4096685

uditS

Hi Radu,

Please report this the development team and forward them all the details provided by me in the issue. I am sure they will understand.

Thanks

June 27, 2019 at 9:20 am #4098425

Radu
Supporter

Languages: English (English )

Timezone: Europe/Bucharest (GMT+03:00)

Hey,

I just reported this to the development team. Right after I'll have an update from them, I'll come back to you with another message. They are pretty fast so this shouldn't take long.

Thanks,
Radu

July 11, 2019 at 8:43 am #4187569

Radu
Supporter

Languages: English (English )

Timezone: Europe/Bucharest (GMT+03:00)

Hey,

Sorry for this delay but I tried a few more times to replicate this issue, thinking that maybe I'll be able to find some more details about this.
I asked my colleagues to have a look at this, as I promised, but they asked for a full trace of the stack. The error here might be that the term does not belong to the taxonomy or similar. In order to be able to handle the case when an error is returned, we need to be able to see that error first.
If you want, you can add an if statement for checking the $new_term for being WP_Error (and display it using var_dump/print_r) and, if this happens, again, just send us the whole error message (that should contain the trace this time, when using var_dump). This way, we'll be able to see exactly what happens and handle it.

Thanks,
Radu

July 23, 2019 at 2:47 pm #4267107

uditS

Hi,

So this error happened again and we were able to capture the error successfully. We are still unable to reproduce the issue by ourselves but here is error captured by logs.

WP_Error Object
(
    [errors] => Array
        (
            [term_exists] => Array
                (
                    [0] => A term with the name provided already exists in this taxonomy.
                )

        )

    [error_data] => Array
        (
            [term_exists] => 12780
        )

)

Thanks

July 23, 2019 at 8:04 pm #4268799

Radu
Supporter

Languages: English (English )

Timezone: Europe/Bucharest (GMT+03:00)

Hey,

Thank you for coming back with an error example. I already send it to my colleague and I'll give you another message as soon as he checks this out to let you know what can be done about it.

Thanks,
Radu

The topic ‘[Closed] Cannot use object of type WP_Error as array in wpml-term-translations’ is closed to new replies.