Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- add_filter("gform_pre_render", "populate_dropdown");
- function populate_dropdown($form){
- if($form["id"] != FORM_EDIT_POST) { return $form; }
- if( isset( $_GET['postid'] ) ) {
- $postid = esc_attr( $_GET['postid'] );
- $post = get_post($postid);
- foreach($form["fields"] as &$field) {
- if( $field["type"] == 'post_category' ){
- $categories = get_the_category($postid);
- if($categories){
- foreach($categories as $category) {
- $selected_cat = $category->term_id;
- }
- }
- if(isset($selected_cat)) {
- $args = array(
- 'orderby' => 'name',
- 'order' => 'ASC'
- );
- $categories = get_categories( $args );
- foreach( $categories as $category ) {
- $is_selected = $category->term_id == $selected_cat ? true : false;
- $choices[] = array( "text" => $category->name, "value" => $category->term_id, "isSelected"=> $is_selected );
- }
- $field["choices"] = $choices;
- }
- }
- }
- }
- return $form;
- }
Advertisement
Advertisement
Advertisement
RAW Paste Data
Copied
Advertisement