PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

populate drop down with wp_list_categories

  1. JayFec
    Member

    I have two standard drop down fields that I want to dynamically populate with wp_list_categories. The user will select the category in the drop down field and once the form is submitted, It is displayed on a page. I want to display the selected category with its permalink. This possible?

    Here is what I go so far

    add_filter("gform_field_value_test", "populate_test");
    function populate_test(){
    $cat = wp_list_categories('orderby=name&exclude=1')
      return $cat;
    }
    Posted 12 years ago on Friday April 27, 2012 | Permalink
  2. David Peralty

    I'm assuming you are using the custom fields section for this? Set it to drop down and are going to populate it dynamically?

    If so, then the display of the permalink will not be a Gravity Forms thing, but a theme coding issue. The dropdown won't have the permalink URL's saved to it. But in your theme, you could show the custom field appending the value from their selection into a link.

    Something like:

    <a href="http://sitename.com/category/<?php echo $selectedcat; ?>"><?php echo $selectedcat; ?></a>

    Of course you'll have to look through WordPress' custom field tutorials to be able to create/display the selected category in the front end.

    http://codex.wordpress.org/Custom_Fields

    Hopefully, this helps.

    Posted 12 years ago on Friday April 27, 2012 | Permalink
  3. JayFec
    Member

    I actually just want to use the standard "drop down" field. These entries are not post. It is populating kinda like a spread sheet which displays on the front end. I have read this http://www.gravityhelp.com/documentation/page/Using_Dynamic_Population but cant achieve the desired results. The I cannot get the categories to display in the drop down. They populate outside of the form field.

    Posted 12 years ago on Friday April 27, 2012 | Permalink
  4. David Peralty

    Hmm, then you could use the following hook to change the data before it is saved:
    http://www.gravityhelp.com/documentation/page/Gform_pre_submission

    Then you could change just the category name into a permalink URL.

    Posted 12 years ago on Friday April 27, 2012 | Permalink
  5. JayFec
    Member

    Thanks, it looks like I got some work to do. this is what I tried, but still wont populate the drop down

    add_filter("gform_pre_submission_4", "pre_submission");
    function pre_submission_handler($form){
    $_POST["input_8"] = wp_list_categories('orderby=name');
    }
    Posted 12 years ago on Friday April 27, 2012 | Permalink
  6. David Peralty

    Sorry, it looks like we are trying to solve two different problems. I was assuming you already got the category population to work and I had moved on to trying to get the permalink stored in your database instead of the category name.

    Take a look at this for getting your categories pre-rendered into the form:
    http://www.gravityhelp.com/documentation/page/Gform_pre_render

    There is even an example at the bottom that could help you.

    Posted 12 years ago on Friday April 27, 2012 | Permalink
  7. JayFec
    Member

    Well here is what I got so far. I am getting an invalid argument for foreach but will continue to work on it. Maybe this code will get perfected by someone :).

    Posted 12 years ago on Friday April 27, 2012 | Permalink
  8. David Peralty

    You can't run a foreach with the two items being the same. You'll notice in the demo it is $posts as $post in yours it is $category as $category.

    Posted 12 years ago on Friday April 27, 2012 | Permalink
  9. Hi JayFec,

    The wp_list_categories() function automatically formats the values in a list element and then echos these list items to the page. This would explain your trouble with getting the values to populate within the drop down.

    The function you'll probably want to use is get_categories(); however, you might also be interested in this plugin which will populate a taxonomy's (aka category) terms into a drop down for you.

    http://wordpress.org/extend/plugins/gravity-forms-custom-post-types/

    Posted 12 years ago on Wednesday May 2, 2012 | Permalink
  10. JayFec
    Member

    David,

    Thank for the tip.. I tried get_categories before, but today I was able to get it to work! *grin*. The problem I am having now is that the entry is not being saved. That whole entire field if I go to view it in the entries is missing. What am I missing? Do I need an after submission hook? Here is the code as promised. I am sure someone else can use and critique it.

    (side note, this code is not perfect and can probably use a lot of critiquing)

    Posted 12 years ago on Wednesday May 2, 2012 | Permalink
  11. Before we get too much further into the code, is there a reason why you are unable to use the Post Category field to accomplish this? It is available in the "Post Fields" section.

    Posted 12 years ago on Wednesday May 2, 2012 | Permalink
  12. JayFec
    Member

    I'm not creating a post for each entry. Its a spread sheet that displays on the front end and is updated by the user. Plus I want to automatically link the category that is selected to their designated category page, So when the results are displayed on the front end, the category is a link to its category page :). Thanks for looking at it

    Posted 12 years ago on Wednesday May 2, 2012 | Permalink
  13. JayFec
    Member

    Any suggestions on why the entry submission for that particular field is not being saved to the entries or where I should start looking? Thanks!

    Posted 12 years ago on Thursday May 3, 2012 | Permalink
  14. JayFec
    Member

    Im still at it. I still cant get the field to save in the entries once it is submitted. This is what I have so far in addition to the code above.

    add_action("gform_pre_submission_4", "pre_submission_handler");
    function pre_submission_handler($form){
    $category = get_categories();
    foreach((get_the_category()) as $category)
        $_POST["input_10"] = '<a href="'.$category->cat_ID .'">'.$category[0]->cat_name.'</a>';
    }

    Here is a similar question.
    http://www.gravityhelp.com/forums/topic/dynamically-populated-checkboxes-not-saved

    Posted 12 years ago on Wednesday May 9, 2012 | Permalink
  15. Hi JayFec,

    I've re-read this thread and I'm still not 100% sure what you are trying to accomplish. To address your last question, if you are using a drop down field you should not have to repopulate the custom field values on the pre-submission filter; that only applies to checkboxes.

    Do you have a live example where we could preview your form and hopefully get a better idea of the end goal?

    Posted 12 years ago on Wednesday May 9, 2012 | Permalink
  16. JayFec
    Member

    Hi David,

    It is currently on a testing environment, but I will be glad to set it up live if you can take the time to look at it. If you can provide me an email, I will send you a link with log in details as soon as I get it set up.

    Thanks in advance

    Posted 12 years ago on Wednesday May 9, 2012 | Permalink
  17. JayFec
    Member

    Its me again :). Form is now on a live server and lo and behold, the entries are now being saved, however the permalink's are being stripped from it. Any suggestions? I have a log in created if someone would like to look.

    Thanks

    Posted 12 years ago on Friday May 11, 2012 | Permalink
  18. David Peralty

    Can you send me the details at peralty@rocketgenius.com

    Posted 12 years ago on Friday May 11, 2012 | Permalink
  19. JayFec
    Member

    Info sent, thanks David.

    Posted 12 years ago on Friday May 11, 2012 | Permalink
  20. David Peralty

    So, filling in the categories into the dropdowns wasn't the hard part. Having the categories linked in the entries page and for front end display was more difficult, but I was able to figure it out. Check out the following code and hopefully it will help others:

    http://pastebin.com/rbepWh1G

    Posted 12 years ago on Tuesday May 15, 2012 | Permalink
  21. JayFec
    Member

    Awesome support from these guys. Thanks! http://pastebin.com/b1uWnLTw

    Posted 12 years ago on Tuesday May 15, 2012 | Permalink