Support

Account

Home Forums Bug Reports acf/update_value for page_link

Solving

acf/update_value for page_link

  • This issue might be related to some previous users having similar problem but was not solved properly. It’s very important for us to make this work properly so I had to dig down and search for problem/solution.

    I’ll try to be as much as possible descriptive. I am using combination of options and repeater.

    1) If I put field page_link in CMS and change values through CMS pages (options page) then page_link is a HTML select element and it has value of integer and displaying string e.g.
    <option value='2'>some page in CMS</option>
    Saving this will properly save in database since it gets integer relation to posts table. No issue here.

    2) When trying to programmatically update field then it goes through function update_value()
    What happens is that value of page_link field is actual string representation (one that is displayed) and not an integer relation to posts table. So you can assume already what happens, it tries to put string in integer and gets reset to 0 as is not valid integer.

    Basically, all relations that are existing will be broken, might want to recheck if there are any more issues.

    3) I don’t like changing contribute plugin code but I had to here in hope that will be implemented ASAP. Ok, what I come up to is following:

    File:
    advanced-custom-fields/core/fields/_functions.php
    function update_value()
    This part:

    else
    {
    // for some reason, update_option does not use stripslashes_deep.
    // update_metadata -> http://core.trac.wordpress.org/browser/tags/3.4.2/wp-includes/meta.php#L82: line 101 (does use stripslashes_deep)
    // update_option -> http://core.trac.wordpress.org/browser/tags/3.5.1/wp-includes/option.php#L0: line 215 (does not use stripslashes_deep)
    
    $value = stripslashes_deep($value);
    			
    // If page_link is not numeric, return numeric id from path.
      if($value && !is_array($value) && $field['type'] == 'page_link' && !is_numeric($value))  {
        $page = get_page_by_path(wp_make_link_relative($value));
        $value = $page->ID;
      }
    
      $this->update_option( $post_id . '_' . $field['name'], $value );
      $this->update_option( '_' . $post_id . '_' . $field['name'], $field['key'] );
    }
    		
    // update the cache
    wp_cache_set( 'load_value/post_id=' . $post_id . '/name=' . $field['name'], $value, 'acf' );
    		
    }

    Condition checks explanation:
    a) Sometimes $value is empty and there’s no need to check for this
    b) Since it’s called recursively, only single elements should be processed
    c) Next comparison we’d like to see if this field is actually page_link
    d) Finally, if value is not numeric, something is wrong probably and try to get relation to posts.

    get_page_by_path() requires relative link and $value is absolute link.

    Please let me know if my fix is “pretty enough” and if it can be used as is so I can push changes and wait for next release.

    Thanks,
    Vladan

  • Is still not complete function, there’s a problem when $value is actually an array of page_links, this works only for one, will update when I have something more.

    UPDATE:

    
          // If page_link is not numeric, return numeric id from path.
          if($value && $field['type'] == 'page_link' && !is_numeric($value))  {
              if(is_array($value) && !is_numeric($value[0])) {
                foreach($value as $key => $path) {
                  $page = get_page_by_path(wp_make_link_relative($path));
                  $value[$key] = $page->ID;
                }
              }
              elseif (!is_array($value) && !is_numeric($value)) {
                $page = get_page_by_path(wp_make_link_relative($value));
                $value = $page->ID;
              }
          }
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.