• Resolved julianoe

    (@julianoe)


    I’m trying to use pll_register_string and did it like this :

    
    if ( function_exists( 'pll_register_string' ) ) :
    	/**
    	 * Register some string from the customizer to be translated with Polylang
    	 */
    	function plugin_pll_register_string() {
    		$mystring = get_option( 'an_option');
    		pll_register_string('Simple text', $mystring);
    	}
    	add_action( 'admin_init', 'plugin_pll_register_string' );
    endif;
    

    The problem is the function_exists( ‘pll_register_string’ ) returns false when the plugins are actives. Odd thing is if i comment out the if statement, it works and my string is available in string translations, proving that the function really exists. What is this mystery?

    • This topic was modified 5 years, 6 months ago by julianoe.
    • This topic was modified 5 years, 6 months ago by julianoe.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Chouby

    (@chouby)

    Hello,

    There is no mistery. You are testing that the function exists before it is defined (probably in a plugin as soon as it is loaded) and you are using the function after it is defined (in the function hooked to admin_init). The best is to check for the existence just before you use the function.

    
    	/**
    	 * Register some string from the customizer to be translated with Polylang
    	 */
    	function plugin_pll_register_string() {
    		if ( function_exists( 'pll_register_string' ) ) {
    			$mystring = get_option( 'an_option');
    			pll_register_string('Simple text', $mystring);
    		}
    	}
    	add_action( 'admin_init', 'plugin_pll_register_string' );
    

    FYI, the function pll_register_string() is defined in a function hooked to plugins_loaded with priority 1.
    See: https://github.com/polylang/polylang/blob/2.6.4/include/class-polylang.php#L42

    Thread Starter julianoe

    (@julianoe)

    Perfect answer to a perfectly stupid question. Thanks for taking the time, it works now ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Issue with function_exists in a plugin file’ is closed to new replies.