• Resolved andytruetone

    (@andytruetone)


    What is the correct DTX code to use for dynamictext form fields with data from a Category’s ACF custom fields – not a post’s fields?

    The form I have on an archive header only pulls in the data from the fields from the category’s first post, not the fields of the category itself. The same thing happens with CF7_get_post_var or CF7_get_custom_field

    For example, at https://truetonegroup.com/willienelson, the ACF field “release” shows the album title on that category header page, as it’s supposed to. The “download” button there opens the form. The second line of the form should be what’s in the category’s “release” field, which is “The Border,” but instead is show the “release” field for the first post below it “Long Story Short …”

    If there isn’t an exact code for this, what are my options in php?

    Is there a way to trigger custom values from the form’s CF7 shortcode like what I added at the end?

    [contact-form-7 id=”87c1bf2″ title=”Download Category” release=”The Border”]

    The page I need help with: [log in to see the link]

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Tessa (they/them), AuRise Creative

    (@tessawatkinsllc)

    I don’t believe we have a built-in shortcode for accessing meta fields on taxonomies, but you can easily achieve that with a custom shortcode, something like this perhaps?

    /**
     * Return ACF Data from Taxonomy
     *
     * @see https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/#loading-from-a-specific-term
     * @see https://support.advancedcustomfields.com/forums/topic/get-custom-taxonomy-field-value/
     *
     * @param array $atts Shortcode attributes, looking for keys field, term_id, and taxonomy.
     *
     * @return string ACF data from taxonomy if found. Empty string otherwise.
     */
    function andytruetone_get_category_meta($atts)
    {
        $atts = shortcode_atts(array(
            'field' => 'release', // Default to your custom "release" field in ACF
            'term_id' => '',
            'taxonomy' => 'category' // Default to WordPress Category taxonomy
        ), array_change_key_case((array)$atts, CASE_LOWER));
    
        // Sanitize input
        $field = trim(sanitize_text_field($atts['field']));
        $term_id = intval(trim(sanitize_text_field($atts['term_id'])));
        $taxonomy = trim(sanitize_text_field($atts['taxonomy']));
        if (!$term_id && is_archive()) {
            // If no term ID was passed and currently viewing an archive page, get the current page
            $queried_object = get_queried_object();
            $taxonomy = $queried_object->taxonomy;
            $term_id = $queried_object->term_id;
        }
    
        // Validate input
        if ($field && $term_id && $taxonomy && function_exists('get_field')) {
            return esc_attr(get_field($field, $taxonomy . '_' . $term_id));
        }
        return '';
    }
    add_shortcode('andytruetone_get_category_meta', 'andytruetone_get_category_meta');

    And then in your “Download Category” contact form, you can write the form tag like this to use your custom shortcode:

    [dynamic_text release readonly "andytruetone_get_category_meta field='release'"]

    (Please note this code is untested, I wrote it in my IDE so syntax is fine but I didn’t exactly test it)

    Thread Starter andytruetone

    (@andytruetone)

    Tessa –

    Thank you so much for this.

    Do I paste this in my child-theme functions.php or archive.php?

    Also, let’s say I need to do this for multiple fields called release, artist, and dropbox (url to a dropbox folder). Do I add those other fields in like this below, or is there a larger section I need to repeat for each field?

    … array( 'field' => 'release','artist','dropbox',

      I also saw similar steps like this I could follow to pass variables through the cf7 shortcode to form fields at this link below. This also seems very useful, though I’m curious if these scripts conflict at all, and also where to drop in the additional field names.

      https://contactform7.com/getting-default-values-from-shortcode-attributes/

      Again, thanks so much for the reply and I’ll let you know how it goes.

      Plugin Author Tessa (they/them), AuRise Creative

      (@tessawatkinsllc)

      Do I paste this in my child-theme functions.php or archive.php?

      My personal preference is in functions.php but I don’t suppose there’s no reason why it can’t be in archive.php since the function is specific to archives.

      Also, let’s say I need to do this for multiple fields called release, artist, and dropbox (url to a dropbox folder). Do I add those other fields in like this below, or is there a larger section I need to repeat for each field?

      Likely a larger section since you want to capture each of those in their own field. Though I don’t think you’ll be able to loop without creating your own form tag, or if you were dynamically creating forms, so they may just look like this in the form template (with the field name and the field value in the shortcode changing for each one):

      [dynamic_text release readonly "andytruetone_get_category_meta field='release'"]
      
      [dynamic_text artist readonly "andytruetone_get_category_meta field='artist'"]
      
      [dynamic_text dropbox readonly "andytruetone_get_category_meta field='dropbox'"]

      This also seems very useful, though I’m curious if these scripts conflict at all

      If not using a dynamic form tag, then no conflict to be had!

      If using the dynamic form tags, the only conflict concern is if you named an ACF field the same as a shortcode function, then it’d try to execute the shortcode instead of retrieving the field value.

      and also where to drop in the additional field names.

      That documentation is used to pull info from the form’s shortcode and it’s not dynamic. Meaning, if your contact form shortcode would have the values be defined manually like this:

      [contact-form-7 id="87c1bf2" title="Download Category" release="The Border" artist="Willie Nelson" dropbox="https://dropbox.com/example"]

      Then you could follow that tutorial to get the value of release which would be manually set to The Border by doing this with a native text field like this:

      [text release default:shortcode_attr]

      And then their instruction in adding the filter:

      
      /*
       * Register Custom CF7 Shortcode Attributes
       *
       * @param array $out Sequential array of shortcode attributes
       * @param mixed $pairs I don't know lol
       * @param array $atts Sequential array of shortcode attributes set
       *
       * @return array Modified array of shortcode attributes
       */
      function custom_shortcode_atts_wpcf7_filter($out, $pairs, $atts) {
          $my_attrs = array('release', 'artist', 'dropbox');
          foreach($my_attrs as $my_attr) {
              if (isset($atts[$my_attr])){
                  $out[$my_attr] = $atts[$my_attr];
              }
          }
          return $out;
      }
      add_filter('shortcode_atts_wpcf7', 'custom_shortcode_atts_wpcf7_filter', 10, 3);

      Of course, you could make that dynamic in a way by using that filter like this instead:

      
      /*
       * Register Custom CF7 Shortcode Attributes
       *
       * @param array $out Sequential array of shortcode attributes
       * @param mixed $pairs I don't know lol
       * @param array $atts Sequential array of shortcode attributes set
       *
       * @return array Modified array of shortcode attributes
       */
      function custom_shortcode_atts_wpcf7_filter($out, $pairs, $atts) {
          $my_attrs = array('release', 'artist', 'dropbox');
          foreach($my_attrs as $my_attr) {
              $out[$my_attr] = andytruetone_get_category_meta(array('field' => $my_attr)); // Calls the custom function from above
          }
          return $out;
      }
      add_filter('shortcode_atts_wpcf7', 'custom_shortcode_atts_wpcf7_filter', 10, 3);

      And then in your form template, it’d be these three:

      [text release default:shortcode_attr]
      
      [text artist default:shortcode_attr]
      
      [text dropbox default:shortcode_attr]

      (Please note this code is untested, I wrote it directly in this answer block so syntax could be sloppy)

      Plugin Author Tessa (they/them), AuRise Creative

      (@tessawatkinsllc)

      Hello, I’m marking this as resolved as I haven’t heard back from you in a while. If the issue still exists, please create a new one. Thanks!

    Viewing 4 replies - 1 through 4 (of 4 total)
    • The topic ‘Category ACF Fields to form on Archive Header’ is closed to new replies.