• Hello,

    We are encountering a problem when inserting a contact form using the do_shortcode() function in our WordPress child theme.

    In our PHP file, we have a block of text at the top, followed by the contact form shortcode, and then more template text. Here’s an example:

    <?php
    echo "hello";
    echo do_shortcode( '[contact-form-7 id="01234" title="test"]' );
    ?>
    <h2><?php echo esc_html__('String', 'template'); ?></h2>

    The issue is that the code that appears after the do_shortcode, which is:

    <h2><?php echo esc_html__('String', 'template'); ?></h2>
    

    Is displayed in English instead of the translation corresponding to our website’s language. It seems as if the code or plugin after the do_shortcode is not working fine.

    We have looked into this matter and found a couple of tickets related to it.

    https://ww.wp.xz.cn/support/topic/breaking-the-translation/
    https://ww.wp.xz.cn/support/topic/conflict-with-loco-translate-plugin-2/

    Additionally, we have checked the “wp_postmeta” table to see if the “_locale” field exists and noticed that its value was set to “en_US,” even though our website is in Spanish (“es_ES”). By manually changing it, the translations are displayed correctly in our child theme page after using the do_shortcode().

    Here is the query we executed in the wp_postmeta table to retrieve the “_locale” field:

    SELECT * FROM wp_postmeta WHERE post_id = XXXX; -- where XXXX is the contact form ID.

    We would like to know if this behavior is normal and if there is any solution or adjustment we can apply to ensure that the translations are correctly displayed after using the do_shortcode().

    Thank you for your attention, and we look forward to your response.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    Where can we see the website in question?

    Thread Starter techwpdrift

    (@techwpdrift)

    Thanks Takayuki but for privacy reasons we can’t publish the website here. What else do you need to know in order to help?

    If you need anything other than the website, we will provide it to you without any problem.

    Plugin Author Takayuki Miyoshi

    (@takayukister)

    Thread Starter techwpdrift

    (@techwpdrift)

    Hi again. I took my time to review the thread before opening this topic. Let’s check everything together

    Welcome to the Contact Form 7 support forum. Here are some notes we’d like you to keep in mind.

    Thanks for your welcome message and notes!

    Everyone helping you here is a volunteer. Even the author of the plugin is a volunteer. No one gets paid for answering your questions. Please be kind and polite.

    And we’re very grateful for your volunteering.

    This is a public forum, so you can’t expect private support here. Support volunteers ask for the URL of the website in question and other detailed information. If you feel inconvenienced by providing information publicly, maybe this forum is not the best place to handle your problems. If you need private support, we suggest you use another channel where you can ask for support from professionals.

    How do you expect us to go to another support forum if the problem resides after installing and using one of the Contact Form 7 shortcodes?

    Beyond sharing private data or not, perhaps if you review the reason for opening this ticket, you may have wondered something that can help improve the plugin and instead of referring us to that thread, you could review it. More than 5 million users are using this plugin and I took my time checking to see if there were similar tickets. This is not the first to ask the same thing.

    What solution has been given apart from referring me to a link with the publication rules?

    One thing is being polite and have a certain moderation and another not to take into account if there may be problems with Contact form 7.

    What does the page where it is installed have to do with it? I think it has been clearly explained that the problem with the incorrect execution of the code after the do_shortcode is because the do_shortcode is from Contact Form 7.

    Some websites can’t be disclosed in a public forum but maybe your plugin has a problem (or not) even if a website is private or not.

    The official language used in this forum is English. If you want discussion in your local language, check to see if there are WordPress support forums for that language.

    Ok, trying to do my best.

    The official website for the plugin (https://contactform7.com) provides FAQ and detailed documentation covering a broad range of topics. Consulting the FAQ and documentation yourself is the quickest way to solve your problems. Ask us when you need help to find information.

    I haven’t seen a thread about the problem described in the first post with do_shortcode.

    This support forum is for the “Contact Form 7” plugin. There are a lot of WordPress plugins with similar names (like “Contact Form 7: The Form Awakens” or something). Although most of them are add-on plugins for Contact Form 7, they are by different creators and this support forum is unlikely to be able to provide solutions for third-party products.

    We know that there are commercial theme vendors who bundle third-party plugins with their theme packages. If a plugin is bundled with a theme, the plugin is part of the theme, and the responsibility of support lies with the theme vendor, not with the original plugin author. If you use the Contact Form 7 plugin that has come with the theme you use, support requests posted in this forum are likely to be turned down, and you will be asked to contact the theme vendor instead.

    My topic is about Contact Form 7, not about alternatives.

    Thanks for reading. If you can’t help us because you can’t attend all the problems we understand but we also request some respect because it is not the first time that things like this have been read in this support forum.

    And again, we also understand your point of view but we think it was necessary to say it.

    Thanks.

    We are seeing the same issue. when we enable shortcode execution for CF7 forms, the shortcode output will never be translated, even though perfectly working translation files are in place. When I place the shortcode in question outside cf7 – in some empty page or post, it works just fine and gets translated as expected.
    @takayukister So, please help and give this a try on a test installation of your own.
    thank you

    I was able to fix this issue.

    Maybe this helps someone and maybe Takayuki will change his plugin code so this issue won’t come up anymore. My fix is just a workaround and is not updateproof. You will have to implement this fix on every contact form 7 update, as the function I am modifying is not pluggable / does not provide a filter:

    find contact form 7 plugin directory, navigate to following file:

    /wp-content/plugins/contact-form-7/includes/l10n.php

    and add following lines to function wpcf7_switch_locale() :

    $wp_loc = get_blog_option(get_current_blog_id(), 'WPLANG');
    $locale = $locale !== $wp_loc ? $wp_loc : $locale;

    this is the modified function:

    /**
     * Switches translation locale, calls the callback, then switches back
     * to the original locale.
     *
     * @param string $locale Locale code.
     * @param callable $callback The callable to be called.
     * @param mixed $args Parameters to be passed to the callback.
     * @return mixed The return value of the callback.
     */
    function wpcf7_switch_locale( $locale, callable $callback, ...$args ) {
    	static $available_locales = null;
    
    	if ( ! isset( $available_locales ) ) {
    		$available_locales = array_merge(
    			array( 'en_US' ),
    			get_available_languages()
    		);
    	}
    
    	$wp_loc = get_blog_option(get_current_blog_id(), 'WPLANG');
    	$locale = $locale !== $wp_loc ? $wp_loc : $locale;
    
    	$previous_locale = determine_locale();
    
    	$do_switch_locale = (
    		$locale !== $previous_locale &&
    		in_array( $locale, $available_locales, true ) &&
    		in_array( $previous_locale, $available_locales, true )
    	);
    
    	if ( $do_switch_locale ) {
    		wpcf7_unload_textdomain();
    		// switch_to_locale( $locale );
    		wpcf7_load_textdomain( $locale );
    	}
    
    	$result = call_user_func( $callback, ...$args );
    
    	if ( $do_switch_locale ) {
    		wpcf7_unload_textdomain( true );
    		restore_previous_locale();
    		wpcf7_load_textdomain( $previous_locale );
    	}
    
    	return $result;
    }

    This fixed translation issues regarding gettext wrappers inside my custom shortcodes used in CF7 forms.

    Thread Starter techwpdrift

    (@techwpdrift)

    What do you think about it Takayuki Miyoshi?

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Problems with code after contact form do_shortcode’ is closed to new replies.