Support

Account

Forum Replies Created

  • Setting the IP on-the-fly was just for example’s sake 🙂

    Yes… that rule would affect the whole Field Group… but.. you can have multiple field groups. The other way, is to set that field to “read-only” using the filter above. You can make it read-only always, or under certain conditions.

    Hope that helps!

  • Here is a simple example of how I went about assigning a value to a custom field automatically.

    Notes:

    + I used the https://www.advancedcustomfields.com/resources/acf-prepare_field/ prepare_field filter to accomplish this.

    + I set a Rule for the Field Group so that the field only gets displayed to the User *after* the Post is Published. This satisfies the criteria for: “default value couldn’t be changed by that user when creating new posts” (I also included an optional “read only” line of code to prevent any manual editing of the field – readonly can be set conditionally as well)

    + I also used the “field key” within the filter call. You can expose the “key” from the Screen Options tab at the top of the Edit Fields page.

    + Further criteria: Only update if the field doesn’t already have a value, and only update it for a specific User.

    Here’s the code to store the IP Address of the User in a custom field on-the-fly (not overly useful, but serves as an example):

    
    <?php
    function my_acf_prepare_field( $field ) {
      $field['readonly'] = true; // optionally make the field read-only
      if ( $field['value'] ) { return $field; } // bail if field has a value already
      $for_user = 2;
      $current_user_id = (int)get_current_user_id();
      if ( $current_user_id != $for_user ) { return $field; } // bail if not specified user
      $ip = my_get_ip_function();
      if ( $ip ) {
        $field['value'] = $ip;
      }
      return $field;
    }
    add_filter('acf/prepare_field/key=field_59d7dc6ac103a', 'my_acf_prepare_field');
    
    // this functions gets the IP Address
    function my_get_ip_function() {
      if ( !empty( $_SERVER['HTTP_CLIENT_IP'] ) ) {
        $ip_address = $_SERVER['HTTP_CLIENT_IP'];
      } else if ( !empty( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
        $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
      } else {
        $ip_address = $_SERVER['REMOTE_ADDR'];
      }
      return $ip_address;
    }
    ?>
    
  • Here is a link in case anyone wants a native solution to Bidirectional Relationships using just ACF.

    Read the Example below the code first, then, sifting through the code may make more sense. It introduces code that may be a little foreign, but the solution works really well, and it is flexible too!

  • Hello…

    You are close here.

    the_field(‘which_category’) echo’s the value…
    get_field(‘which_category’) retrieves it (to store in a variable, pass to an array, etc.)

    Use: get_field(‘which_category’) in this case, and that should fix this up for you.

  • Hi @puur

    Demonstrated in this Taxonomy doc, you could change the line in your code where you create the “li” and echo $ook to be something like:

    
    <li><?php echo $ook->name; ?></li>
    

    In the same code on the above linked page, it also shows how to get the link and description.

    Hope that helps!
    Keith

  • hi Aldo,

    Can you link me to one of your threads here and I will see how I could help?

    Thanks,
    Keith

  • hi @v3nt

    Check out the guide for adding fields to menu items

    That’s precisely how it’s supposed to behave.

    You choose which Menu/Location for the Menu Items that you want your ACF fields to be attached to.

    If you read this guide too, it should become clear.

  • hey @wilfredo123

    Try this for the date comparison line:

    
    if ( strtotime( date('Y-m-d') ) > strtotime( $fecha ) ) {
    

    That will get you proper results.

    I don’t know how to do the translation, sorry about that.

  • Hi Rob,

    To run the above code for only specific post types, you can add this code to the top:

    
    // store the current post type in a variable
    $current_post_type = get_post_type( $post_id );
    
    // you can use an array to add 1 or more post types that you
    // want this function to apply to, like so:
    $allowed_post_types = array( 'post-type-1', 'post-type-2', 'post-type-3' );
    
    // then, say... if 'not' in array, return $value untouched
    if ( !in_array( $current_post_type, $allowed_post_types ) ) {
      return $value;
    }
    

    For the last part of your question…
    … hmmm ….

    You could “disable” certain fields based on certain criteria (like post status, or if a custom field is empty)… then… re-enable them once the criteria is met.

    It’s simple enough to disable regular ACF fields (text, number, etc.) and in fact… sometimes you can use the “rules” for field groups to hide them based on post status.

    Here’s a resource about the acf/prepare_field filter that explains how you might disable ACF fields.

    To disable multiple fields, you can call the filter function multiple times:

    
    add_filter('acf/prepare_field/key=field_1', 'my_acf_prepare_field');
    add_filter('acf/prepare_field/key=field_2', 'my_acf_prepare_field');
    

    Be sure that you run your conditions within the function. Check the value of a different field to determine whether to disable the field or not. Use something like:

    
    if ( get_field( 'mandatory-field-key', get_the_id() ) ) {
      // field has a value, probably don't do anything
    } else {
      // field doesn't have a value, disable the other fields
    }
    

    To disable WordPress standard fields like the Title, you can try something like the code below.

    Note: this uses the CSS ID of the HTML element to “select” it, then “disable” it… you can do the same to target other non-text ACF fields. You can control the ID of all ACF fields (look at the Wrapper Attributes).

    
    /* START: Disables some fields until certain criteria is met */
    function my_set_fields_to_read_only() {
    
      $allowed_post_types = array('projects');
      if ( !in_array( get_post_type(), $post_types ) ) { return; }
      
      // if the important field has a value,
      // we don't need to do anything else
      if ( get_field( 'mandatory-field-key' ) ) { return; }
      
      // JavaScript to disable fields
      echo "<script type='text/javascript'>";
      echo "  ( function ( $ ) {";
      echo "    $( document ).ready( function () {";
      echo "      $('#title').attr('disabled','disabled');";
      echo "      $('#title').css({'background': '#f8f8f8'});";
      echo "    });";
      echo "  }( jQuery ) );";
      echo "</script>";
    
      return;
    }
    add_action( 'edit_form_advanced', 'my_set_fields_to_read_only' );
    /* END: Disables some fields until certain criteria is met */
    

    I know that’s a lot thrown in one reply. I hope that you can pick it apart and are able to move forward with your project! Let me know.

  • Hi @rslater

    Are you trying it this way:

    Suppose a Group field named ‘hero’ with a sub field named ‘image’… it will be saved to the database using the meta name ‘hero_image’

  • Hello,

    As long as you have the field in question setup with ACF, you are set on that end of things.

    It’s then just a matter of displaying the correct data on the front-end to your Users, based on the date in that field (compared to today’s date).

    There are several ways to accomplish it… here are 2 ideas that came to me:

    1) Using PHP (probably the easiest)

    Here are the basic steps:

    a) Create an HTML/CSS template for what you want to accomplish. Something like below (based on your screenshot):

    
    <ul>
    <li><strong>Employment Status:</strong> <span style="color:green;">Current</a></li>
    <li><strong>This job will expire:</strong> December 25, 2017</li>
    </ul>
    

    b) Now, within your code, you will want to re-create the above dynamically. You can do something like (perhaps in a loop if you have multiple records to display):

    
    <?php
    $expiry_date = get_field('expiry_date');
    $is_expired = FALSE;
    $status_text = 'Current';
    $status_text_2 = 'This job will expire on:';
    $status_color = 'green';
    if ( strtotime(date('Y-m-d')) > date('Y-m-d', strtotime($expiry_date)) ) {
      $is_expired = TRUE;
      $status_text = 'Expired';
      $status_text_2 = 'This job expired on:';
      $status_color = 'red';
    }
    echo '<ul>';
    echo '<li><strong>Employment Status:</strong> <span style="color:' . $status_color . ';">' . $status_text . '</a></li>';
    echo '<li><strong>' . $status_text_2 . '</strong> ' . date("F j, Y", strtotime($expiry_date)) . '</li>';
    echo '</ul>';
    ?>
    

    Resources:

    2) Using vanilla JavaScript (or JQuery).

    Here are the basic steps:

    a) Print the expiration date to the page, perhaps within a ‘list item’ tag of an unordered list. The unordered list should have a unique ID, so that you can ‘select’ it later with JavaScript.

    b) Within the ‘list item’ tag, you can have a ‘paragraph’ tag to display your desired message, namely: Current or Available

    c) In the JavaScript (that you add to the footer of the web page), “select” the ‘unordered list’, then iterate through the ‘list items’, check if the date has passed, and set the HTML of the ‘paragraph’ tag appropriately.

  • Hi @bgallagh3r

    Good idea, I will pass it along to the developer. Thanks for the suggestion.

    In the meantime, want a workaround?

    Try this code:

    
    function my_custom_admin_styles() {
      echo '<style>.acf-field-59cb76248bc31 ul li { display:inline; }</style>';
    }
    add_action( 'admin_head', 'my_custom_admin_styles' );
    

    You will need to use your field key of course. You can expose the “key” from Screen Options in the Edit Fields area.

    The code can go in a Custom Plugin or functions theme file.

    Hope that helps, and thanks again!

  • You’re welcome 🙂

    That code you posted looks right… it should display the category names, separated by commas, without links.

    Is it not working?

  • No problem..

    You can safely remove this line:

    echo 'Places: ';

  • Hi @eyesx

    Sorry for the late reply..

    Yes.. creating terms is an option for the Taxonomy field in the Pro version (Added in v5.2.3). I didn’t realize myself that it wasn’t in the free version. However, the free version just released with an optional v5 upgrade, so you can play more with the free version before committing if you like, because it has the feature you are after.

  • That should work based on what I see in the screenshot… the only thing is that maybe that field group is not attached to an option page? Check in Location Rules below the field set to be sure that Options Page is set. And then of course, be sure it has a value 🙂

  • Hi @davsev

    See here: https://www.advancedcustomfields.com/resources/group/

    The field name would become: upper_front_cube_title_1 when using the ACF Group field.

    Hope that helps! Let me know.

  • Hi @wilfredo123

    The use of get_term_link and passing in the term item should do the trick for you for getting the link.

    You can try the code below… I setup a counter in the test code to be sure not to have a comma at the end of your list. It should display as shown in your image, but please test it for bugs.

    Hope this helps! Let me know.

    
    <?php
    $empresa_terms = get_field( 'empresa' );
    if ( $empresa_terms ) {
      $total = count( $empresa_terms );
      $count = 1;
      echo 'Places: ';
      foreach ( $empresa_terms as $empresa_term ) {
        echo '<a href="' . get_term_link( $empresa_term ) . '">';
        echo $empresa_term->name;
        echo '</a>';
        if ( $count < $total ) {
          echo ', ';
        }
        $count++;
      }
    }
    ?>
    
  • hi Jakub,

    Maybe try adding ‘type’ => NUMERIC to the query, or remove the quotes from the number?

  • ahhh… silly mistake on my part… the “if” is missing the closing bracket… sorry about that.

    if ( is_post_type_archive( 'my_custom_post_type' ) && !empty( $query->query['post_type'] == 'my_custom_post_type' ) ) {

  • That’s weird. Can’t be sure why that is.

    I have a different approach:

    1) Create a regular text field. Make note of the ‘key’. You can expose the key by clicking Screen Options at the top right of the Edit Fields screen.

    2) Add the code below to a Custom Plugin or functions file.

    In the ‘add_filter’ line, of course, use your key from Step 1.
    You can also change ‘full’ to some other image size, to get the image filename you are after.

    
    function my_acf_load_value( $value, $post_id, $field ) {
      global $my_row_number;
      if ( !$my_row_number ) { $my_row_number = 0; }
      $meta_key = 'allgemein_mitglied_'.$my_row_number.'_allgemein_profilbild';
      $sub_field_value = get_post_meta( $post_id, $meta_key, true );
      $image_attributes = wp_get_attachment_image_src( $sub_field_value, 'full' );
      $value = basename( $image_attributes[0] );
      $my_row_number++;
      return $value;
    }
    add_filter('acf/load_value/key=field_59c9182f21022', 'my_acf_load_value', 10, 3);
    
  • I can’t be sure what the problem is.. this comes back to the old tried an true, long testing…

    1) deactivate all plugins – if it works then, activate 1 plugin at a time to see the interfering plugin

    2) change to WP default them, see if it works

    With that said, I have seen that exact same thing, and it’s a known issue (and as far as I know is being looked into)… with the plugin WPGlobus… see here.

    Is it random and intermittent in your case?

Viewing 25 posts - 26 through 50 (of 118 total)