Support » Plugin: Polylang » [Plugin: Polylang] filter meta

  • zbrass

    (@zbrass)


    Hi

    How do i use this filter?

    add_filter('pll_copy_post_metas', 'copy_post_metas'); function copy_post_metas($metas) {
         return $metas+array('my_post_meta');
    }

    I have a custom_value persons the content i want to export to the translated post. how?

    best regards,

    Arthur

    http://wordpress.org/extend/plugins/polylang/

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

    (@chouby)

    If you create a custom filed with *name* ‘persons’, then Polylang will copy the custom field value if you add this code:

    add_filter('pll_copy_post_metas', 'copy_post_metas');
    function copy_post_metas($metas) {
         return $metas+array('persons');
    }

    You can add this code to your theme functions.php or create your own plugin with these lines

    <?php
    /*
    Plugin Name: My Plugin
    */

    and then the code mentionned above.

    Thread Starter zbrass

    (@zbrass)

    Hi Chouby thanks for the answer.

    But for me return $metas+array('persons'); does not work
    I use return $metas=array('persons'); so instead of a + a =
    and that works. Is this a bug? Or is this safe for the future?

    regards,

    Andis

    (@andydegroo)

    That would disable copying of other meta fields when translating. You would loose page template and featured image which are stored as meta values.

    More reliable would be to use array_merge rather than array concatenation which works differently with numeric keys.

    Try this:

    add_filter('pll_copy_post_metas', 'copy_post_metas');
    function copy_post_metas($metas) {
         return array_merge($metas, array('persons', 'other_custom_meta'));
    }

    @chouby This might be a bug or a feature, depending on what you want to achieve – read about array operators in PHP manual.
    I’d also suggest to use array_unique($metas) in Polylang_Admin_Filters::save_post() so you don’t have to loop through duplicates. That would be a performance improvement if there are many meta fields to copy or sync.
    Maybe it would be even faster to use get_post_custom($post_id) and then iterate over the returned array rather than calling get_post_meta() for every entry which may be slower.

    Thread Starter zbrass

    (@zbrass)

    Works on all 🙂

    Great thanx Andy!

    Plugin Author Chouby

    (@chouby)

    Of course you are right ! I will modify the documentation, and will look for your suggestion for the save_post function. Thanks a lot !

    Plugin Author Chouby

    (@chouby)

    @andydegroo. In the new dev version (0.8.0.7), I tried to follow your advice. As a bonus, it is now possible to synchronize metas which have multiple values for the same meta key.

    Thread Starter zbrass

    (@zbrass)

    Hi Chouby and AndyDeGroo,

    The pll_copy_post_metas works great but what do i do with meta data from pages?
    The filter does not see these metas.

    Do you have a solution?

    Thanks!!

    Arthur

    Plugin Author Chouby

    (@chouby)

    There should be no differences between posts and pages. My tests work for both.

    Thread Starter zbrass

    (@zbrass)

    Thanks.

    Strange i try to filter the metas produced by the plugin Advanced Custom Fields but that does not seem to work.
    Any idea?

    Arthur

    Plugin Author Chouby

    (@chouby)

    I made a small test with advanced custom fields and it works perfect for me using this code snippet:

    add_filter('pll_copy_post_metas', 'copy_post_metas');
    function copy_post_metas($metas) {
    	return array_merge($metas, array('my field name'));
    }

    Did you take care to use the field name and not the field label ?

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘[Plugin: Polylang] filter meta’ is closed to new replies.