Advertisement
Guest User

Untitled

a guest
May 21st, 2013
74
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. add_filter("gform_pre_render", "populate_dropdown");
  2. function populate_dropdown($form){
  3. if($form["id"] != FORM_EDIT_POST) { return $form; }
  4.  
  5. if( isset( $_GET['postid'] ) ) {
  6. $postid = esc_attr( $_GET['postid'] );
  7. $post = get_post($postid);
  8.  
  9. foreach($form["fields"] as &$field) {
  10.  
  11. if( $field["type"] == 'post_category' ){
  12.  
  13. $categories = get_the_category($postid);
  14. if($categories){
  15. foreach($categories as $category) {
  16. $selected_cat = $category->term_id;
  17. }
  18. }
  19.  
  20. if(isset($selected_cat)) {
  21. $args = array(
  22. 'orderby' => 'name',
  23. 'order' => 'ASC'
  24. );
  25. $categories = get_categories( $args );
  26. foreach( $categories as $category ) {
  27. $is_selected = $category->term_id == $selected_cat ? true : false;
  28. $choices[] = array( "text" => $category->name, "value" => $category->term_id, "isSelected"=> $is_selected );
  29.  
  30. }
  31. $field["choices"] = $choices;
  32. }
  33. }
  34. }
  35. }
  36. return $form;
  37. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement