1
votes

So I have Advanced custom fields linked to my custom post type named: members. and I want to link the member field to the Wordpress users with the users getting the subscriber role.

my code for the custom post type is as follows:

function init_members() {
    $labels = array(
        'name'               => 'Members',
        'singular_name'      => 'Member',
        'menu_name'          => 'Members',
        'name_admin_bar'     => 'Member',
        'add_new'            => 'New member',
        'add_new_item'       => 'New member',
        'new_item'           => 'New member',
        'edit_item'          => 'Edit member',
        'all_items'          => 'All members',
        'search_items'       => 'Search member',
        'not_found'          => 'No members found',
        'not_found_in_trash' => 'No members found in trash'
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'exclude_from_search' => true,
        'rewrite' => array('slug' => 'member'),
        'has_archive' => false,
        'supports' => array('title'),
        'show_in_rest' => true,
        'menu_icon' => 'dashicons-groups'

    );
    register_post_type('members', $args);
}
add_action('init', 'init_members');

function add_member_columns ( $columns ) {
    unset($columns['date']);
    return array_merge ( $columns, array (
        'contactperson'   => __ ( 'Contactperson' ),
        'phone_number'   => __ ( 'Phonenumber' ),
        'email'   => __ ( 'Email' ),
    ) );
}
add_filter ('manage_members_posts_columns', 'add_member_columns' );

function fill_member_columns ( $column, $post_id ) {
    switch ( $column ) {
        case 'contactperson':
            echo get_post_meta ( $post_id, 'contactperson', true );
            break;
        case 'phone_number':
            echo get_post_meta ( $post_id, 'phone_number', true );
            break;
        case 'email':
            echo get_post_meta ( $post_id, 'email', true );
            break;
    }
}
add_action ('manage_members_posts_custom_column', 'fill_member_columns', 10, 2 );

I've looked around here but I didn't really find any posts about this.

1
Your code is to add a column to the custom post type. Can you please explain a little better what you're after? - Howard E
yeah, so I've linked advanced custom fields to this custom post type. and I want it to when I'm done filling in the info for the post type in the ACF fields. to make a Wordpress account with the role subscriber - Asta
I think you want to build an admin page developer.wordpress.org/reference/functions/add_menu_page and then implement a CRUD method. Or use the add member post to trigger a wp_create_user developer.wordpress.org/reference/functions/wp_create_user - Howard E

1 Answers

1
votes

Here's an answer... This will add a new user. The ACF Field ID's are available when you look at ACF -> Tools -> Generate PHP

For example:

'fields' => array(
    array(
        'key' => 'field_5e592f61e1c2f',
        'label' => 'Contact Person',
        'name' => 'contactperson',

Anyway... This works.. but you may want to refine your solution.

add_action( 'publish_members', 'so_member_add_60451627', 10, 2 );
function so_member_add_60451627($postid, $post){
    // fetch post data
    $name = sanitize_text_field($_POST['acf']['field_5e592f61e1c2f']); // ACF Field Key for name
    $phone = sanitize_text_field($_POST['acf']['field_5e592f6fe1c30']); // ACF Field Key for phone
    $email = sanitize_email($_POST['acf']['field_5e592f77e1c31']); // ACF Field Key for email

    // Create Userdata Array
    $userdata = array(
        'user_pass'     => wp_generate_password(),
        'user_login'    => $name,
        'user_nicename' => $name,
        'user_email' => $email
    );
    $user = wp_insert_user($userdata);

    // Set user meta field for phone depending on what you want it to be called
    update_user_meta($user, 'user_phone', $phone);
}