Dynamically change the shortcode
-
Hi,
I have a question about using shortcode and change them dymically on the flow.
I’m using WPML en HTML Forms, these plugins are working fine, no issues with this.
But I have different HTML Forms that need to be displayed on my website depending on the language of the page (Dutch or English). I can dynamically do the following:
- Detect the current language of the page using WPML (WordPress Multilingual Plugin).
- Change the shortcode whether the page is in Dutch (
nl
) or English (en
).
When a user visits the webpage with detected language “en” the initiatial slug is [hf_form slug=”contactformulier-nl”. Because the visitor has language “en” I need to change the shortcode to [hf_form slug=”contactformulier-en”]
My problem is that it looks like function is not exected and when I change the code I come into a recursive loop.
I search many hours for a solution but still not solved.
This is the source code:
add_action( ‘init’, ‘register_dynamic_html_form_shortcode’ );
function register_dynamic_html_form_shortcode() {
add_shortcode( ‘hf_form’, ‘dynamic_html_form_shortcode’ );
}function dynamic_html_form_shortcode( $atts ) {
error_log( ‘dynamic_html_form_shortcode called’ );
$current_language = apply_filters( ‘wpml_current_language’, NULL ); error_log( ‘Current language: ‘ . $current_language );$atts = shortcode_atts( array(
‘slug’ => ”, ), $atts );
error_log( ‘Slug: ‘ . $atts[‘slug’] );if ( $current_language == ‘en’ && strpos( $atts[‘slug’], ‘-nl’ ) !== false ) {
$atts[‘slug’] = str_replace( ‘-nl’, ‘-en’, $atts[‘slug’] );
error_log( ‘Modified slug for English: ‘ . $atts[‘slug’] );
}$henk = ‘[hf_form slug=”‘ . esc_attr( $atts[‘slug’] ) . ‘”]’;
error_log( ‘Generated shortcode: ‘ . $henk );return do_shortcode( $henk );
}The page I need help with: [log in to see the link]
- You must be logged in to reply to this topic.