Support

Account

Home Forums Backend Issues (wp-admin) update_sub_field doing nothing, with or without keys. Reply To: update_sub_field doing nothing, with or without keys.

  • I’m looking at this thread: https://support.advancedcustomfields.com/forums/topic/issues-with-add_rows-and-flexible-content/

    According to this, as of 2016, add_row on flexible fields weren’t working. Instead, they used update_field, which added new rows? Anyways, I wonder if it has something to do with flexible fields…

    Additionally, I have been thinking that I may be hooking incorrectly in my functions.

    First, the full code for this specific piece:

    
    <?php
    /**
     *
     *   Update the content of a field.
     *
     */
    
    class POB_Save_Settings {
    
      public static function init() {
    
          add_action( 'wp_ajax_nopriv_save_settings_fields', array( 'POB_Save_Settings', 'save_settings_fields' ) );
          add_action( 'wp_ajax_save_settings_fields', array( 'POB_Save_Settings', 'save_settings_fields' ) );
    
      }
    
      public function update_static_content($post_id, $this_count, $the_content){
        //print_r(get_field_objects($post_id));
        $all_fields = get_field_objects($post_id);
        $this_field = $all_fields['content_type']['layouts'];
        $this_row_layout = $all_fields['content_type']['value'][$this_count]['acf_fc_layout'];
        $this_layout = array();
        foreach($this_field as $layout => $key){
          $this_layout[$key['name']] = $key['sub_fields'];
          $this_layout[$key['name']]['key'] = $layout;
        }
    
        $this_layout_key = $this_layout[$this_row_layout]['key'];
        $label_group_key = $this_layout[$this_row_layout][1]['key'];
    
        if( have_rows('content_type', $post_id) ) :
          while( have_rows('content_type', $post_id) ) : the_row();
          echo 'content_type while' . '<br>';
          echo get_row_index() . '<br>';
          echo $this_count+1 . '<br>';
            if( get_row_layout() == $this_row_layout && get_row_index() == $this_count+1 ) :
              echo 'true' . '<br>';
              update_post_meta( $post_id, 'content_type_'.$this_count.'_title', $the_content['title']);
              if( have_rows($label_group_key) ) :
                while( have_rows($label_group_key) ) : the_row();
                /*$values = array(
                  $this_layout[$this_row_layout][1]['sub_fields'][0]['key'] => $the_content['label'],
                  $this_layout[$this_row_layout][1]['sub_fields'][1]['key'] => $the_content['subtitle']
                );
                print_r($this_field);*/
                $label = 'content_type_'.$this_count.'_label_label';//$this_layout[$this_row_layout][1]['sub_fields'][0]['key'];
                $subtitle = $this_layout[$this_row_layout][1]['sub_fields'][1]['key'];//'content_type_'.$this_count.'_label_subtitle';
                echo update_sub_field( 'label', $the_content['label']);
                echo update_sub_field( 'subtitle', $the_content['subtitle']);
                //update_sub_field( array($this_layout_key, 1, $label_group_key), $values, $post_id );
                endwhile;
              endif;
              if( have_rows('css') ) :
                while( have_rows('css') ) : the_row();
                $cssclass = 'content_type_'.$this_count.'_css_classes';
                $cssid = 'content_type_'.$this_count.'_css_id';
                update_post_meta( $post_id, $cssclass, $the_content['classes']);
                update_post_meta( $post_id, $cssid, $the_content['style_id']);
                endwhile;
              endif;
            endif;
          endwhile;
        endif;
      }
    
      public static function save_settings_fields(){
        if(isset($_POST['to_save'])){
          $post_info = $_POST['details'];
          $post_id = $post_info['postid'];
          $this_count = $post_info['index'];
          $saved_content = $_POST['to_save'];
          $all_content = array();
          foreach($saved_content as $array){
            $all_content[$array['name']] = $array['value'];
          }
          print_r($all_content);
        } else {
          wp_die($_POST.' not set!');
        }
        //wp_send_json($response);
        $foobar = new POB_Save_Settings;
        $isdone = $foobar->update_static_content($post_id, $this_count, $all_content);
        if ( is_wp_error( $isdone ) ) {
            wp_send_json_error( $isdone );
        } else {
          echo 'yay!';
          wp_die();
        }
      }
    
    }
    

    So I’m using AJAX to send information to this. In my plugin’s functions.php, we got:

    
    define('PLUGIN_DIR_PATH', plugin_dir_path(__FILE__));
    
    require_once PLUGIN_DIR_PATH . 'admin/classes/pob-category-walker.php';
    include_once( plugin_dir_path( __DIR__ ) . 'advanced-custom-fields-pro/acf.php' );
    include_once PLUGIN_DIR_PATH . 'admin/pob-remote-post-class.php';
    include_once PLUGIN_DIR_PATH . 'admin/views/panes/pane_layout_radio_buttons.php';
    include_once PLUGIN_DIR_PATH . 'admin/classes/pob-save-settings.php';
    include_once PLUGIN_DIR_PATH . 'admin/classes/pob-create-field.php';
    POB_Remote_Post::get_instance();
    Radio_Buttons_Layout::init();
    POB_Save_Settings::init();
    POB_Create_Field::init();
    
    function update_acf_settings_path( $path ) {
        $path = plugin_dir_path( __DIR__ ) . 'advanced-custom-fields-pro/';
        return $path;
    }
    add_filter( 'acf/settings/path', 'update_acf_settings_path' );
    function update_acf_settings_dir( $dir ) {
        $dir = plugin_dir_url( __DIR__ ) . 'advanced-custom-fields-pro/';
        return $dir;
    }
    add_filter( 'acf/settings/dir', 'update_acf_settings_dir' );
    
    do_action('acf/input/admin_head'); // Add ACF admin head hooks
    do_action('acf/input/admin_enqueue_scripts'); // Add ACF scripts
    add_action( 'admin_init', 'add_acf_variables' );
    function add_acf_variables() {
        acf_form_head();
    }
    

    With the last pieces (after init of those classes) being added recently, making shots in the dark.

    Could someone perhaps check that I’m hooking properly to update content? Or if I’m missing something there?