• Hi all
    i use polylang ( great plugin πŸ™‚ ) with Advanced Custom Fields.
    The syncing of the custom fields works perfect and as excepted. (if i toggle customfield syncing in the polylang settings on/off).

    Now i was looking for a way to choose wich custom fields should be synced and wich shouldn’t. When i turn custom field sync to off and add this Filter to functions.php. A single Field is synced.

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

    So far so good. Now, if i want to sync a custom field wich is based on
    an ACF Repeater (i.e. my Gallery), the above code seems not to work.

    I also tried the other way, by enabling custom field sync in polylang settings. (works with the ACF Repeater Gallery) and adding a code wich i found here, to remove the syncing of a field.

    add_filter('pll_copy_post_metas', 'copy_post_metas');
    function copy_post_metas($metas) {
          return array_splice($metas, array('my_slider_caption'));
    }

    That don’t work and i get following error while Saving a page:

    Warning: array_splice() expects parameter 2 to be long, array given in .../functions.php on line 134
    Warning: array_unique() expects parameter 1 to be array, null given in .../admin-sync.php on line 124
    Warning: Invalid argument supplied for foreach() in .../admin-sync.php on line 127
    Warning: array_splice() expects parameter 2 to be long, array given in .../functions.php on line 134
    Warning: array_unique() expects parameter 1 to be array, null given in .../admin-sync.php on line 124
    Warning: Invalid argument supplied for foreach() in .../admin-sync.php on line 127
    Warning: Cannot modify header information - headers already sent by (output started at .../functions.php:134) in .../post.php on line 233
    Warning: Cannot modify header information - headers already sent by (output started at .../functions.php:134) in .../wp-includes/pluggable.php on line 1179

    Is there a way to add sync of the acf repeater field by code?
    or is there a way to exclude certain custom fields from syncing?

    Thanks in Advice!
    Pixmeister

    https://ww.wp.xz.cn/plugins/polylang/

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

    (@chouby)

    Hi!

    Your usage of array_splice is wrong. http://php.net/manual/en/function.array-splice.php

    Maybe you want to use array_diff. http://php.net/manual/en/function.array-diff.php

    Thread Starter Pixmeister

    (@pixmeister)

    Hi Chouby.
    Thank you for your (fast) response!

    And thanks for the idea with array_diff.
    i wasn’t able to solve this yet. Maybe there’s something easy i don’t see.

    What i have:
    A custom Field Repeater in WP called galerie
    Inside this Field there are two Fields: galimage and galtitle

    shouldn’t it be possible to sync those fields with the array?

    To sync a simple text custom Field, this works:

    add_filter('pll_copy_post_metas', 'copy_post_metas');
    function copy_post_metas($metas) {
          return array_merge($metas, array('testfield'));
    }*/

    But somehow i should get those subfields galimage and galtitle synced.
    (maybe it’s just the way i define the array in the array_merge function?)

    Thread Starter Pixmeister

    (@pixmeister)

    I found that code in Advanced Custom Fields Repeater to access the rows.

    <?php
    
    $rows = get_field('repeater_field_name' ); // get all the rows
    $first_row = $rows[0]; // get the first row
    $first_row_image = $first_row['sub_field_name' ]; // get the sub field value 
    
    // Note
    // $first_row_image = 123 (image ID)
    
    $image = wp_get_attachment_image_src( $first_row_image, 'full' );
    // url = $image[0];
    // width = $image[1];
    // height = $image[2];
    ?>
    <img src="<?php echo $image[0]; ?>" />

    But i didn’t found out how to use it for the array merging.

    Thread Starter Pixmeister

    (@pixmeister)

    And i tried this without Success:

    add_filter('pll_copy_post_metas', 'copy_post_metas');
    function copy_post_metas($metas) {
    	if( have_rows('galerierep') ):
    		while( have_rows('galerie') ): the_row();
    			$first_row = the_sub_field('galimage');
    			$second_row = the_sub_field('galtitle');
    			$arrayHolder = array_merge($metas, $first_row, $second_row);
    		endwhile;
    
    		return $arrayHolder;
    
    	endif;
    }
    Plugin Author Chouby

    (@chouby)

    I am sorry but i don’t know how custom fields are stored in ACF. Maybe you should ask to the plugin author. What Polylang does is to copy customs fields as known by WordPress. The filter pll_copy_post_metas expects fields names (as they are stored in DB). If a custom field is an array, Polylang copies the whole array.

    Thread Starter Pixmeister

    (@pixmeister)

    Ok, i think i have to find another solution.

    Chouby, thank you anyway for your help!

    ACF Repeater fields are stored using a variation of the parent field name.

    For example – a repeater field called “team_player” with a child item called “team_name” will save each row added in a sequential field called “team_player_0_team_name”, “team_player_1_team_name”, etc.

    A simple way to remove all “team_player” fields might be to set-up the filter like this:

    /**
    * Remove defined custom fields from Polylang Sync
    *
    * @since       0.1
    * @param       Array       $metas
    * @return      Array       Array of meta fields
    */
    function pll_copy_post_metas( $metas )
    {
    
        // this needs to be added to the PolyLang Settings page as an option ##
        $unsync = array (
            'team_player'
        );
    
        #var_dump( $unsync );
        #var_dump( $metas );
    
        if ( is_array( $metas ) && is_array( $unsync ) ) {
    
            // loop over all passed metas ##
            foreach ( $metas as $key => $value ) {
    
                // loop over each unsynch item ##
                foreach ( $unsync as $find ) {
    
                    if ( strpos( $value, $find ) !== false ) {
    
                        unset( $metas[$key] );
    
                    }
    
                }
    
            }
    
        }
    
        #wp_die( var_dump( $metas ) );
    
        // kick back the array ##
        return $metas;
    
    }
Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘include/exclude sync of AFC Repeater Fields’ is closed to new replies.