- Stato
- Aperto
- Tag dell’argomento
- WCML
Panoramica del problema
La descrizione breve del prodotto WooCommerce si basa sul campo riassunto dell’articolo, che di solito è un semplice campo di testo che non gestisce gli shortcode. Pertanto, non ci si aspetta che incollando gli shortcode del page builder nel riassunto dell’articolo questi funzionino, e quando traduce i riassunti degli articoli, WPML non cerca di gestire tali shortcode.
Tuttavia, WC gestisce questi “riassunti” in modo un po’ diverso e gli shortcode funzionano, ma WPML continua a comportarsi come se non funzionassero, e se lo shortcode include attributi da tradurre o link interni da gestire automaticamente, ciò non è facilmente possibile.
Soluzione alternativa
Abbiamo preparato un codice che può essere aggiunto come plugin per fornire questa funzionalità. Copia e incolla il codice seguente in un file che potresti chiamare wpmlpb-excerpts.php e salvalo nella tua directory wp-content/plugins/, quindi attiva il plugin.
<?php
/**
* Plugin Name: WPML Page Builders - Proces Excerpts
* Description: Parses shortcode strings in exceprts.
* Version: 1.0
*/
namespace WPMLPB686;
( new Excerpts() )->add_hooks();
class Excerpts {
const SEPARATOR_START = '<!--WPML_EXCERPT_START-->';
const SEPARATOR_END = '<!--WPML_EXCERPT_END-->';
public function add_hooks() {
add_filter( 'wpml_pb_shortcode_content_for_translation', [ $this, 'joinWithContent' ], 10, 2 );
add_filter( 'wpml_tm_translation_job_data', [ $this, 'excludeFromJob' ], 10, 2 );
add_action( 'wpml_pro_translation_completed', [ $this, 'splitFromContent' ], 999 );
}
public function joinWithContent( $content, $id ) {
$post = get_post( $id );
if ( $post && $post->post_excerpt ) {
$content .= self::SEPARATOR_START . $post->post_excerpt . self::SEPARATOR_END;
}
return $content;
}
public function excludeFromJob( $package, $post ) {
if ( preg_match( '/[[^]]+]/', $post->post_excerpt ) ) {
$package['contents']['excerpt']['translate'] = 0;
$package['contents']['excerpt']['data'] = '';
}
return $package;
}
public function splitFromContent( $post_id ) {
$post = get_post( $post_id );
if ( $post && false !== strpos( $post->post_content, self::SEPARATOR_START ) ) {
$start_pos = strpos( $post->post_content, self::SEPARATOR_START );
$end_pos = strpos( $post->post_content, self::SEPARATOR_END, $start_pos );
if ( false !== $end_pos ) {
$content = substr( $post->post_content, 0, $start_pos );
$excerpt = substr( $post->post_content, $start_pos + strlen( self::SEPARATOR_START ), $end_pos - $start_pos - strlen( self::SEPARATOR_START ) );
if ( $excerpt ) {
wp_update_post(
[
'ID' => $post_id,
'post_content' => $content,
'post_excerpt' => $excerpt,
]
);
}
}
}
}
}