• I’m having a bizarre issue and I’m hoping someone can help me. I have a contact form plugin I created myself. To prevent multiple form submissions, a unique stamp is generated when the form loads. Upon successful form submission, the stamp is inserted into a dedicated database table and must be unique. If it’s unique, the insertion is successful, the form content is emailed, and the user is shown a “thank you” message. Otherwise, the user is shown a message that the form has already been submitted and processed. I use the form plugin on several sites and it works as expected: the “thank you” message is displayed upon initial submission, then the “already submitted” message is displayed if the user refreshes the page and resubmits the form.

    I made the plugin translation-ready and my original “load textdomain” function was this:

    function my_plugin_load_textdomain() {
    	$domain = 'my_plugin';
    	$locale = apply_filters( 'plugin_locale', get_locale(), $domain );
    	$mofile = $domain . '-' . $locale . '.mo';
    	/* Check the global language folder */
    	$files = array( WP_LANG_DIR . '/my_plugin/' . $mofile, WP_LANG_DIR . '/' . $mofile );
    	foreach ( $files as $file ){
    		if ( file_exists( $file ) )
    			return load_textdomain( $domain, $file );
    	}
    	// Fallback to the plug-in language folder.
    	load_plugin_textdomain('my_plugin', false, dirname( plugin_basename( __FILE__ ) ).'/languages');
    }
    add_action( 'plugins_loaded', 'my_plugin_load_textdomain' );

    I have the plugin installed on one multilingual site and it works fine. I also use it on a few other sites that are not multilingual.

    Yesterday, I revised my “load textdomain” function to more closely match similar functions used by other plugins:

    function my_plugin_load_textdomain() {
    	$domain = 'my_plugin';
     	$locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
    	$locale = apply_filters( 'plugin_locale', $locale, $domain );
    	/**
    	 * Load Localisation files.
    	 * The first-loaded translation file overrides any following ones if the same translation is present.
    	 */		
    	unload_textdomain( $domain );
    	load_textdomain( $domain, WP_LANG_DIR . '/'.$domain.'/'.$domain.'-' . $locale . '.mo' );
    	load_plugin_textdomain( $domain, false, plugin_basename( dirname( __FILE__ ) ) . '/languages' );
    }
    add_action( 'plugins_loaded', 'my_plugin_load_textdomain' );

    I tried it on just one site. The problem is I am now getting multiple stamp insertion attempts. Upon submission the form content is emailed, but the user is shown the “already submitted” message (as if the user tried to submit the form more than once). And my site’s error log shows one or more “Duplicate entry” errors.

    I returned my load_textdomain function to its original form, but the duplicate insertion problem persists. The plugin still works perfectly fine on other sites where I did NOT test this new load textdomain function.

    I confess I do not have a firm understanding of WordPress’s load textdomain functionality, and I assume that’s why I can’t understand and fix this problem. Can anyone help me understand what went wrong and how I can fix it? If more details are needed regarding the contact form plugin itself, let me know and I will provide them.

The topic ‘Duplicate $wpdb->insert after modifying load_textdomain function’ is closed to new replies.