Title: Polylang functions in functions.php
Last modified: August 31, 2016

---

# Polylang functions in functions.php

 *  Resolved [Loïc Antignac](https://wordpress.org/support/users/webaxones/)
 * (@webaxones)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/polylang-functions-in-functionsphp/)
 * Hi (once again),
 * I would like to dynamicaly affect custom fields to each translated post of a 
   current post. The idea is to set an array of every pages in french and to affect
   the location of the custom fields with every translations id.
 * How can I get the translations id in my must-use plugin OR in functions.php ?
 * Polylang functions seems to be declared (if( function_exists… is good) but return
   anything…
 * This does’nt work :
 *     ```
       global $idLangs;
   
       $myFrenchId = '1696';
       global $polylang;
       if(isset($polylang)){
           foreach ($polylang->get_languages_list() as $term){
               $idLangs = pll_get_post($myFrenchId, $term->slug);
           }
       }
       print_r($idLangs);
       ```
   
 * Thanks for any answer, any idea… ^^
 * [https://wordpress.org/plugins/polylang/](https://wordpress.org/plugins/polylang/)

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

 *  [Česlav Przywara](https://wordpress.org/support/users/chesio/)
 * (@chesio)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/polylang-functions-in-functionsphp/#post-7196230)
 * Hi Manhy,
 * Are you aware that you overwrite single variable (`$idLangs`) again and again
   in your for loop? This way, `print_r($idLangs);` prints only ID of translation
   in the very last language that is returned by `$polylang->get_languages_list()`.
 * Also, it would help if you add some context to your code. Where is it placed?
   From where is it executed: global scope or a function?
 * Greets,
    Česlav
 *  Thread Starter [Loïc Antignac](https://wordpress.org/support/users/webaxones/)
 * (@webaxones)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/polylang-functions-in-functionsphp/#post-7196232)
 * Oops : sorry for the mistake, it’s an array :
    $idLangs[] = pll_get_post….
 * I’m not good neither in php neither in wordpress, but here is what i’m doing :
 * I got several customs fields made with ACF and declared in a mu-plugin.
    It works.
   I would like to put in an array ids of each page of the site and dynamically 
   locate the custom fields with the array values. Actual ACF location : `’location’
   => array ( array ( array ( ‘param’ => ‘page’, ‘operator’ => ‘==’, ‘value’ => ‘
   1696’, ), ), array ( array ( ‘param’ => ‘page’, ‘operator’ => ‘==’, ‘value’ =
   > ‘1762’, ), ), ),’ I want to do this dinamically. And I need to get the ids 
   of all the translations of one page, but I can’t : it seems that polylang functions
   are not declared when my mu-plugin is set.
 * So I tried to make a function to get ids in functions.php with an add_action 
   called after mu plugin :
    add_action( ‘muplugins_loaded’, ‘my_function’ ) but
   this didn’t work 🙁
 *  Thread Starter [Loïc Antignac](https://wordpress.org/support/users/webaxones/)
 * (@webaxones)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/polylang-functions-in-functionsphp/#post-7196233)
 * And thanks for the answer of course ^^
 *  [Česlav Przywara](https://wordpress.org/support/users/chesio/)
 * (@chesio)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/polylang-functions-in-functionsphp/#post-7196238)
 * Do you have Polylang installed as must-use plugin as well? If not, you should
   hook `my_function` to [plugins_loaded](https://developer.wordpress.org/reference/hooks/plugins_loaded/)
   action instead of [muplugins_loaded](https://developer.wordpress.org/reference/hooks/muplugins_loaded/).
 * Also, I recommend (whenever possible) to use functions that are part of [Polylang API](https://polylang.wordpress.com/documentation/documentation-for-developers/functions-reference/)–
   this way you’re less likely to find out that your code does not work after you
   update Polylang in the future.
 * Try the following code:
 *     ```
       function my_function() {
         global $idLangs;
   
         // Sanity check
         if ( !function_exists('pll_languages_list') ) { return; }
   
         $myFrenchId = 1696;
         foreach ( pll_languages_list() as $slug ) {
           $idLangs[] = pll_get_post($myFrenchId, $slug);
         }
       }
       add_action( 'plugins_loaded', 'my_function' );
       ```
   
 *  Thread Starter [Loïc Antignac](https://wordpress.org/support/users/webaxones/)
 * (@webaxones)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/polylang-functions-in-functionsphp/#post-7196240)
 * Hmm… I must be very silly but :
 * In my mu plugin (polylang isn’t installed as must-use) :
 *     ```
       global $idLangs;
   
       function my_function() {
   
           // Sanity check
           if ( !function_exists('pll_languages_list') ) { return; }
           echo 'yes'; <-it prints
           $myFrenchId = 1696;
           foreach ( pll_languages_list() as $slug ) {
               $idLangs[] = pll_get_post($myFrenchId, $slug);
           }
           print_r($idLangs);<-it prints
           return $idLangs;
       }
       add_action( 'plugins_loaded', 'my_function' );
   
       echo 'resultat:';
       print_r($idLangs);
       ```
   
 * <-it doesn’t print
 * How can I get back the array out of my function, cause I can’t put all the creation’s
   code of custom fields in that function o_O
 *  Thread Starter [Loïc Antignac](https://wordpress.org/support/users/webaxones/)
 * (@webaxones)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/polylang-functions-in-functionsphp/#post-7196241)
 * The last <-it doesn’t print is for the print_r outside of course
 *  Thread Starter [Loïc Antignac](https://wordpress.org/support/users/webaxones/)
 * (@webaxones)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/polylang-functions-in-functionsphp/#post-7196242)
 * Bouaaaah ! just a $toto = myfunction() of course !
    Merveilleux !!!!!!!! Thank
   you so Chesio !
 *  Thread Starter [Loïc Antignac](https://wordpress.org/support/users/webaxones/)
 * (@webaxones)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/polylang-functions-in-functionsphp/#post-7196244)
 * Oops, no : cache fake : I can’t get back my array out of my function…
 *  [Česlav Przywara](https://wordpress.org/support/users/chesio/)
 * (@chesio)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/polylang-functions-in-functionsphp/#post-7196245)
 * It seems the problem you have is that you want to fetch data from Polylang before
   it is loaded. Must-use plugins are loaded before normal plugins, so if you call`
   my_function` from must-use plugin code, you will not get any results.
 * One solution is to move your custom fields declaration from must-use plugin to
   normal plugin. Even then I recommend to wrap the code that runs `my_function`
   in function hooked to `plugins_loaded`, because you have no way to determine 
   in which order WordPress loads your plugins.
 * Yet another solution is to install Polylang as must-use plugin, but I have no
   experience with that and don’t know if Polylang works that way.
 *  [Česlav Przywara](https://wordpress.org/support/users/chesio/)
 * (@chesio)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/polylang-functions-in-functionsphp/#post-7196246)
 * I would suggest to try something like:
 *     ```
       function my_function() {
         // as above
       }
   
       function my_custom_fields() {
         // Get translated IDs.
         $idLangs = my_function();
   
         // Define your custom fields - you can use data from $idLangs here...
       }
   
       add_action('plugins_loaded', 'my_custom_fields');
       ```
   
 * This code works the same way if included in must-use or normal plugin.
 *  Thread Starter [Loïc Antignac](https://wordpress.org/support/users/webaxones/)
 * (@webaxones)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/polylang-functions-in-functionsphp/#post-7196247)
 * Ok. I’ve understood.
    Gonna sleep, and test all of your recommendations tomorrow
   morning.
 * Thanks u very much for help ^^
 *  Thread Starter [Loïc Antignac](https://wordpress.org/support/users/webaxones/)
 * (@webaxones)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/polylang-functions-in-functionsphp/#post-7196298)
 * Well besides that I fought hard with multidimensionnal php arrays , I can now
   dynamically assign my ACF custom fields in each Polylang language, thanks to 
   you. So again : thank you very much.
 *  [Česlav Przywara](https://wordpress.org/support/users/chesio/)
 * (@chesio)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/polylang-functions-in-functionsphp/#post-7196306)
 * I’m glad I could help!

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

The topic ‘Polylang functions in functions.php’ is closed to new replies.

 * ![](https://ps.w.org/polylang/assets/icon-256x256.png?rev=3433336)
 * [Polylang](https://wordpress.org/plugins/polylang/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/polylang/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/polylang/)
 * [Active Topics](https://wordpress.org/support/plugin/polylang/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/polylang/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/polylang/reviews/)

## Tags

 * [acf polylang](https://wordpress.org/support/topic-tag/acf-polylang/)

 * 13 replies
 * 2 participants
 * Last reply from: [Česlav Przywara](https://wordpress.org/support/users/chesio/)
 * Last activity: [10 years, 2 months ago](https://wordpress.org/support/topic/polylang-functions-in-functionsphp/#post-7196306)
 * Status: resolved