WPML Causes HTTP 500 Error On Save When Post Type Is Public

[ad_1]

When saving a recipe with the post type set to public, and the WPML plugin enabled, the REST API returns a HTTP status code of 500.

Error log (with paths shortened)

Stack trace:
#0 /web/app/plugins/wpml-multilingual-cms/sitepress.class.php(1493): Rareloop\Lumberjack\Bootstrappers\RegisterExceptionHandler->handleError(2, 'Undefined array...', '/shared/httpd/t...', 1493)
#1 /web/wp/wp-includes/class-wp-hook.php(324): SitePress->set_element_language_details_action(Array)
#2 /web/wp/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters('', Array)
#3 /web/wp/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#4 /web/app/plugins/wp-recipe-maker/includes/public/class-wprm-compatibility.php(534): do_action('wpml_set_elemen...', Array)
#5 /web/app/plugins/wp-recipe-maker/includes/public/class-wprm-recipe-saver.php(235): WPRM_Compatibility::set_language_for(17510, 'en')
#6 /web/app/plugins/wp-recipe-maker/includes/public/api/class-wprm-api-recipe.php(98): WPRM_Recipe_Saver::update_recipe(17510, Array, true)
#7 /web/wp/wp-includes/class-wp-hook.php(324): WPRM_Api_Recipe::api_insert_update_recipe(Object(WP_Post), Object(WP_REST_Request), true)
#8 /web/wp/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters('', Array)
#9 /web/wp/wp-includes/plugin.php(517): WP_Hook->do_action(Array)
#10 /web/wp/wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php(708): do_action('rest_insert_wpr...', Object(WP_Post), Object(WP_REST_Request), true)
#11 /web/wp/wp-includes/rest-api/class-wp-rest-server.php(1230): WP_REST_Posts_Controller->create_item(Object(WP_REST_Request))
#12 /web/wp/wp-includes/rest-api/class-wp-rest-server.php(1063): WP_REST_Server->respond_to_request(Object(WP_REST_Request), '/wp/v2/wprm_rec...', Array, NULL)
#13 /web/wp/wp-includes/rest-api/class-wp-rest-server.php(439): WP_REST_Server->dispatch(Object(WP_REST_Request))
#14 /web/wp/wp-includes/rest-api.php(428): WP_REST_Server->serve_request('/wp/v2/wprm_rec...')
#15 /web/wp/wp-includes/class-wp-hook.php(324): rest_api_loaded(Object(WP))
#16 /web/wp/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters('', Array)
#17 /web/wp/wp-includes/plugin.php(565): WP_Hook->do_action(Array)
#18 /web/wp/wp-includes/class-wp.php(418): do_action_ref_array('parse_request', Array)
#19 /web/wp/wp-includes/class-wp.php(813): WP->parse_request('')
#20 /web/wp/wp-includes/functions.php(1336): WP->main('')
#21 /web/wp/wp-blog-header.php(16): wp()
#22 /web/index.php(6): require('/shared/httpd/t...')
#23 {main}

It looks like on line 534 of class-wprm-compatibility.php you’re calling the wpml_set_element_language_details action and failing to set the trid(translation group id) in the second argument array — which is required.

https://wpml.org/wpml-hook/wpml_set_element_language_details/

if ( $multilingual ) {
// WPML.
if ( 'wpml' === $multilingual['plugin'] ) {
do_action( 'wpml_set_element_language_details', array(
'element_id' => $recipe_id,
'element_type' => 'post_' . WPRM_POST_TYPE,
'language_code' => $language ? $language : null,
) );
}
}

Should probably look something like

if ( $multilingual ) {
// WPML.
if ( 'wpml' === $multilingual['plugin'] ) {
$post_type="post_" . WPRM_POST_TYPE;
$translation_group_id = apply_filters( 'wpml_element_trid', NULL, $recipe_id, $post_type );

do_action( 'wpml_set_element_language_details', array(
'element_id' => $recipe_id,
'trid' => $translation_group_id ?: false,
'element_type' => $post_type,
'language_code' => $language ? $language : null,
) );
}
}

This seems to fix the issue. Though there might be some side effects I’m not aware of.

Also a bit of a tangent, but the short form the short form ternary operator has been available since PHP 5.3. So it’s probably safe to write $language ? $language : null as $language ?: null.

Any help you could offer would be greatly appreciated.

Thanks,

Andrew

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer