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.

Specific author dropdown guest blog submission form

  1. tfmwa
    Member

    First I'd like to thank Shelly, other forum users and administrators for the thread that can be found here: http://www.gravityhelp.com/forums/topic/post-authors and threads linked to from that thread.

    It has allowed me to piece together a dropdown in a form. It contains all users, which allows people to create their blog post without logging in and without having to create 1 form for every guest blogger.

    We'd like to build on this and have the user dropdown list only users that have a specific Wordpress user role. So the list won't be as long and filled with names that don't need to be in there.

    My knowledge of PHP is passive, so I'm limited to breaking it (often) and otherwise interpreting and modifying existing code when I look at it and/or break it repeatedly until I get it right.

    Would anyone be willing to build on the code provided in the other threads and help us out by providing the code that needs to be added or modified, so the dropdown only lists users with a specific user role?

    [edit]
    Just discovered that the author field is blank in the WP dashboard if submitting the form when NOT logged in. I'm investigating this at the moment, but if anyone wants to chime in on that before I remove this message, please do so. :D
    [/edit]

    The owner and myself (and future users) would be very grateful. Thanks in advance. :)

    The role we want to populate the dropdown with is the Wordpress 'Author' role (Auteur in nl_NL).
    The code I'm currently using at http://goo.gl/xO7GA is:

    add_filter("gform_pre_render_8", populate_dropdown);
    
    function populate_dropdown($form){
    
    global $wpdb;
    $wp_user_search = $wpdb->get_results("SELECT ID, display_name FROM $wpdb->users ORDER BY ID");
    
    //Creating drop down item array.
    $items = array();
    
    //Adding initial blank value.
    $items[] = array("text" => "", "value" => "");
    
     //Adding post titles to the items array
    foreach ( $wp_user_search as $userid )
    	$items[] = array("value" => $userid->display_name, "text" => $userid->display_name);
    
     //Adding items to field id 8. field_1_2
        foreach($form["fields"] as &$field)
    
            if($field["id"] == 21){
                $field["type"] = "select";
                $field["choices"] = $items;
    
            }
    
        return $form;
    }
    //Override default author with author selected from dropdown
    add_filter('gform_post_data_8', 'update_post_author', 10, 2); // "2" in "gform_post_data_2" is the form ID.
    function update_post_author($post_data, $form) {
        $author_id = rgpost("input_21"); // grab the value from field 5
        $post_data['post_author'] =  $author_id;
        return $post_data;
    }
    Posted 12 years ago on Monday July 30, 2012 | Permalink
  2. Hi tfmwa,

    Try using this code to only retrieve an array of users of a specific role:

    http://www.wprecipes.com/how-to-get-all-users-having-a-specific-role

    If you include that function, you could then replace this in your code:

    [php]
    $wp_user_search = $wpdb->get_results("SELECT ID, display_name FROM $wpdb->users ORDER BY ID");

    ...with this:

    [php]
    $authors = getUsersWithRole('editor');

    You will need to update the rest of the code accordingly to use this new variable.

    Posted 11 years ago on Monday August 6, 2012 | Permalink