Support

Account

Forum Replies Created

  • So –– the source code above got scrambled badly. And it also turned out to be a little bit more complex to solve this reliably.

    I’ve created and will maintain a gist on GitHub with my current solution:

    https://gist.github.com/hirasso/32152de5d84dbcf1c194ea9217db6166

    It creates a hidden custom post type to cache oEmbed responses from fields on options pages or the like.

  • Hi @nmaine , what’s the reason you did mark your post as private? Just curious 🙂

    Anyways – there were a few internal changes in ACF some versions ago, the gist was outdated. I have updated the code so that it should work with the latest version of ACF again.

  • Thanks @hube2 , I have also written to the ACF team directly. Just thought that until the feature lands in ACF, my little plugin could come in handy for other users as well.

    I have already received a reply from the ACF devs. They will consider introducing a similar functionality in ACF core! 🎉

  • …just tested the latest Version (5.11.3) and I can confirm that this issue is fixed. Thank you!

  • Hey @lgladdy ,

    that sounds good, thanks so much! Looking forward to test it out 🙂

  • Hi John,

    thank you. Maybe your answers will help others at some point – I needed a more general solution, based on field type checks. The above solution using wp_debug_backtrace_summary works great for me. Will mark it as solved.

  • Hi John, as always, you are the fastest! 🙂

    is_admin() is not enough for my use case, not even in combination with !wp_doing_ajax(). I often also use get_field() in the admin area, for example to display ACF field data in manage_post_posts_custom_column or the like.

    I came up with an admittedly pretty hacky solution:

    
    // inside the acf/load_value callback:
    /**
     * Don't touch the value if it was requested by <code>acf_render_fields</code>
     */
    $backtrace = wp_debug_backtrace_summary(null, 0, false);
    if( array_search('acf_render_fields', $backtrace) !== false ) return $value; 
    

    This checks if the function was invoced by acf_render_fields(), which is only used when an ACF form is being rendered (also when using acf_form() in the frontend).

    Anyways, thanks for helping me think!! 🙂

  • Works like a charm, @mcaskill ! Thank you for your work!

  • weird – after a quick edit on my reply, it disappeared from the thread, but when I try to post it again, the forum seems to detect a duplicate…

    
    ERROR: Duplicate reply detected; it looks as though you’ve already said that!
    
  • Thanks @hube2 for your quick answer! Actually, I was counting on you 😉

    With the field setting I meant a field setting for the acf-field-group-edit-screen:

    
    add_action("acf/render_field_settings/type=text", [$this, 'render_field_settings'], 9);
    

    The setting:

    
    /**
     * Render field settings for multilingual fields
     *
     * @param Array $field
     * @return void
     */
    public function render_field_settings( $field ) {
    
      acf_render_field_setting( $field, array(
        'label'         => __('Multilingual?'),
        'instructions'	=> '',
        'name'          => 'acfml_multilingual',
        'type'          => 'true_false',
        'ui'            => 1,
      ), false);
    
    }
    

    On the second try, it seems like I made it work. The solution was to hook into acf/update_value *after* the ACF hook (I used a priority of 12):

    
    add_filter("acf/format_value/type=group", [$this, 'format_multilingual_value'], 12, 3);
    

    My update_value-function:

    
    /**
     * Write the default language's $value to the group $value itself
     *
     * @param mixed $value
     * @param int $post_id
     * @param array $field
     * @return mixed
     */
    public function update_multilingual_value( $value, $post_id, $field ) {
      if( !$this->is_acfml_group($field) ) return $value;
      $default_language = acfml()->get_default_language();
      $value = get_field("{$field['name']}_$default_language", $post_id, false);
      return $value;
    }
    
  • Thanks @hube2 for your quick answer! Actually, I was counting on you 😉

    With the field setting I meant a field setting for the acf-field-group-edit-screen:

    
    add_action("acf/render_field_settings/type=text", [$this, 'render_field_settings'], 9);
    

    The setting:

    
    /**
     * Render field settings for multilingual fields
     *
     * @param Array $field
     * @return void
     */
    public function render_field_settings( $field ) {
    
      acf_render_field_setting( $field, array(
        'label'         => __('Multilingual?'),
        'instructions'	=> '',
        'name'          => 'acfml_multilingual',
        'type'          => 'true_false',
        'ui'            => 1,
      ), false);
    
    }
    

    On the second try, it seems like I made it work. The solution was to hook into acf/update_value *after* the ACF hook (I used a priority of 12):

    
    add_filter("acf/format_value/type=group", [$this, 'format_multilingual_value'], 12, 3);
    

    My update_value-function:

    
    /**
     * Write the default language's $value to the group $value itself
     *
     * @param mixed $value
     * @param int $post_id
     * @param array $field
     * @return mixed
     */
    public function update_multilingual_value( $value, $post_id, $field ) {
      if( !$this->is_acfml_group($field) ) return $value;
      $default_language = acfml()->get_default_language();
      $value = get_field("{$field['name']}_$default_language", $post_id, false);
      return $value;
    }
    
  • I know this post is oooold, but I thought I’d leave my findings here for future reference. I also had this problem, in my case it was caused by this filter:

    
    add_filter('ajax_query_attachments_args', 'my_query_attachments_args');
    

    Try removing or debugging this, it helped me getting the attachment edit popup to have contents again 🙂

  • I’m having the same problem. Has this been resolved?

  • Actually, to make the form work again after submitting it, $form.one is better than $form.on, so the e.preventDefault() is only being triggered once:

    
    /**
     * Setup the ajax submit
     */
    setupAjaxSubmit() {
    
      acf.addAction('submit', ( $form ) => {
        
        if( !$form.hasClass('is-ajax-submit') ) {
          return true;
        }
        // only prevent the default of the submit action once:
        $form.one('submit', (e) => {
          e.preventDefault();
        });
        // my custom code for handling AJAX posting:
        $form.acfFrontendForm('doAjaxSubmit');
    
      });
    
    }
    
  • @fdrv you where totally right! This was my old code:

    
    /**
     * Setup the ajax submit
     */
    setupAjaxSubmit() {
    
      $('.acf-form').on('submit', e => {
        if( $(e.currentTarget).hasClass('is-ajax-submit') ) {
          // because of this e.preventDefault the acf 'submit' action 
          // is not being triggered anymore starting with ACF 5.7.13
          e.preventDefault();
        }
      });
      
      acf.addAction('submit', ( $form ) => {
        if( !$form.hasClass('is-ajax-submit') ) {
          return true;
        }
        // my custom code for handling AJAX posting:
        $form.acfFrontendForm('doAjaxSubmit');
      });
    }
    

    …I changed it to this, now it’s working fine again (and also it’s much cleaner ;)):

    
    /**
     * Setup the ajax submit
     */
    setupAjaxSubmit() {
    
      acf.addAction('submit', ( $form ) => {
        
        if( !$form.hasClass('is-ajax-submit') ) {
          return true;
        }
        $form.on('submit', (e) => {
          e.preventDefault();
        });
        // my custom code for handling AJAX posting:
        $form.acfFrontendForm('doAjaxSubmit');
    
      });
    
    }
    
  • @ibes Actually, yes! There is a submit action now. Very handy. Also, I included a fix for empty file inputs breaking Safari, the new IE… ;(

    
    acf.addAction('submit', ( $form ) => {
    
      // Fix for Safari Webkit – empty file inputs kill the browser
      // https://stackoverflow.com/a/49827426/586823
      let $fileInputs = $('input[type="file"]:not([disabled])', $form)
      $fileInputs.each(function(i, input) {
        if( input.files.length > 0 ) {
          return;
        }
        $(input).prop('disabled', true);
      })
    
      var formData = new FormData( $form[0] );
    
      // Re-enable empty file $fileInputs
      $fileInputs.prop('disabled', false);
    
      acf.lockForm( $form );
    
      $.ajax({
        url: window.location.href,
        method: 'post',
        data: formData,
        cache: false,
        processData: false,
        contentType: false
      }).done(response => {
        console.log(response);
        acf.unlockForm( $form );
      });
      
    
    });
    
  • I know, I’m late to this post. Still, here’s a quick and easy solution:

    
    $('.acf-form').on('click', '[data-event="remove-row"]', function(e) {
       $(this).click();
    });
    

    …mimics the double click 🙂

  • Hi John, I have a lot of different field types in my form. Tested it with simple text fields only after your comment, but the error persists.

    But good news – I solved it 5 minutes ago by adding wp-util as a dependency to my main script:

    
    wp_enqueue_script( 'app', asset_uri('assets/js/app.js'), array('jquery', 'wp-util'), null, true );
    

    So maybe you guys just forgot to add it as a dependency in acf_form_head?

  • I activated SCRIPT_DEBUG and found the code that is throwing the error (line 10123 in acf-input.js):

    
    // bail early if no media views
    if( !wp || !acf.isset(wp, 'media', 'view') ) {
      return;
    }
    
  • Hi @hwk , I testet your code and it works great! The only thing now still missing is a way to hook into the validation ajax request, so that I could include image upload capabilities, as described by @valpe . Do you know how to do that? You seem like a pro 😉

    …in the meantime, I used this code (still two ajax requests, but much cleaner than my original solution):

    
    acf.add_filter('validation_complete', ( json, $form ) => {
      // if errors?
      if( json.errors ) {
        return json;
      }
    
      var formdata = new FormData($form[0]);
    
      $.ajax({
        url: window.location.href,
        method: 'post',
        data: formdata,
        cache: false,
        processData: false,
        contentType: false,
        success: (data) => {
          acf.validation.toggle($form, 'unlock');
          //... show success message ...
        }
      });
    
      return json;
          
    });
    
  • @pjsando this only works, if you get the date field unfiltered, directly from the database:

    
    $acfDate = get_field('post_date', $post_id, false); // the last argument, 'false', gets the field unfiltered from the db.
    
Viewing 25 posts - 1 through 25 (of 41 total)