• Resolved Matt Gibson

    (@gothickgothickorguk)


    Hi,

    I’m writing a plugin where I’m using custom (user) taxonomies to add custom fields to a user profile, editable in the admin interface for that user. So, for example, I’ve registered a custom taxonomy called “member-status”.

    I can manage my custom taxonomies fine — I can add new terms, add translations, etc. And on the admin pages for the taxonomies, I can use the language selector in the admin bar to view only English terms, only Japanese terms, and so on.

    I’ve now added a show_user_profile filter so I can add a list of “member-status” terms on each user profile page, and tried to get hold of the terms like this in my filter code:

    $terms = get_terms($key, array('hide_empty'=>false));

    ($key is my taxonomy slug, e.g. ‘member-name’)

    But that gets me back all the terms, in all languages. I was expecting get_terms to respond to the language selector in the admin bar — when I choose English in the language selector, I still get all the terms.

    I’ve experimented by doing this, which I thought would manually select the English terms only:

    $terms = get_terms($key, array('hide_empty'=>false, 'lang' => 'en'));

    …but even then, I still get all the taxonomy terms, for all languages.

    Any hints? I’m using WP3.7.1, Polylang 1.2.1.

    Thanks,

    Matt

    https://www.ads-software.com/plugins/polylang/

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Author Chouby

    (@chouby)

    That’s how it should work. Did you activate the languages and translations for this custom taxonomy in Polylang settings?

    Shouldn’t be $key the taxonomy name rather than the taxonomy slug?

    Thread Starter Matt Gibson

    (@gothickgothickorguk)

    Thanks for getting back to me so quickly.

    My custom taxonomies are all ticked under the “Custom taxonomies” setting of the Polylang settings page — is that all it should need?

    And yes, sorry, it’s the name I’m using. (I just tend to think of it as a slug because it looks like one — I’m definitely passing the same value that I passed as the first parameter to register_taxonomy when I set it up.)

    Plugin Author Chouby

    (@chouby)

    My bad. I made some tests and clearly the admin filter does not work on all screens. I should double check how to improve this.

    However the direct query with:

    $terms = get_terms($key, array('hide_empty'=>false, 'lang' => 'en'));

    should always work.

    Is it possible that you made a previous query on the same taxonomy with the same cache domain? I use the filter ‘terms_clauses’ to make this query possible. Here is quote of WordPress code comments for get_terms

    * The ‘cache_domain’ argument enables a unique cache key to be produced when this query is stored
    * in object cache. For instance, if you are using one of this function’s filters to modify the
    * query (such as ‘terms_clauses’), setting ‘cache_domain’ to a unique value will not overwrite
    * the cache for similar queries. Default value is ‘core’.

    Thread Starter Matt Gibson

    (@gothickgothickorguk)

    Hi,

    Aha! Yes, your suspicions are right. I just stepped through the code in xdebug, and get_terms() hits the cache on my call, because earlier on, during taxonomy set-up, I check to see if any terms exist using the same query (so I can pre-populate them with some helpful values if none exist.)

    Hah! Ironically, now that I can see what’s going on, the first Google hit I came across was someone fixing the same issue with WPML ?? https://wpml.org/forums/topic/get_terms-and-caching-bug/

    I’ll see if I can fix my issue in a similar way. (Presumably I can use pll_current_language() to grab the right language from the admin bar drop-down, then use that to shove appropriate things in both ‘lang’ and ‘domain’..?)

    Thanks for the pointers!

    Matt

    PS:

    Thread Starter Matt Gibson

    (@gothickgothickorguk)

    Ah… except that if I try to figure out what the current language is using pll_current_language(), it looks for $polylang->curlang, which doesn’t exist. Any hints as to a workaround? Guessing this isn’t available in this way in PLL_Admin?

    Thanks,

    Matt

    Plugin Author Chouby

    (@chouby)

    A cache domain per language. Looks like a good idea. I will investigate this for Polylang.

    Note that you can’t use pll_current_language() on admin side. You should use $polylang->pref_lang instead. This is a language object containing either the language selected by the languages admin filter or the default language if “Show all languages” is selected.

    Note that if your plugin should also be compatible with WPML, Polylang provides ICL_LANGUAGE_CODE on admin side since 1.2. (I am trying to improve the compatibility with the WPML API).

    Thread Starter Matt Gibson

    (@gothickgothickorguk)

    Fab, thanks, that seems to do the trick:

    // Use Polylang's selected language if set. Otherwise we can default to pretty much
    // anything and get the same result, so I'll just choose 'en'.
    $admin_lang = defined('ICL_LANGUAGE_CODE') ? ICL_LANGUAGE_CODE : 'en';
    $terms = get_terms($key,
        array(
            'hide_empty'=>false,
            'lang' => $admin_lang,
            'cache_domain' => $admin_lang)
        );

    Plugin Author Chouby

    (@chouby)

    I would just correct like this:

    $admin_lang = defined('ICL_LANGUAGE_CODE') && 'all' != ICL_LANGUAGE_CODE ? ICL_LANGUAGE_CODE : 'en';

    as unlike $polylang->pref_lang which is always set to a valid language, ICL_LANGUAGE_CODE can value 'all'.

    You can also use pll_default_language() to get the default language instead of 'en' in case English is not an existing language.

    Thread Starter Matt Gibson

    (@gothickgothickorguk)

    Excellent; thanks for all the help.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Using get_terms() in admin interface from a plugin?’ is closed to new replies.