I need to extend the user profiles in my WordPress site to include additional information that will be used for generating a vCard. The additional fields should include: Job Title: A text field where the user can enter their job title. Photo URL: A

Home » Snippets » I need to extend the user profiles in my WordPress site to include additional information that will be used for generating a vCard. The additional fields should include: Job Title: A text field where the user can enter their job title. Photo URL: A
1

Created with:

Visibility: 

public

Creator: abdulrahman

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
// Register Custom Post Type
function wpturbo_register_vcard_cpt() {
    $labels = array(
        'name'                  => _x( 'VCards', 'Post Type General Name', 'wpturbo' ),
        'singular_name'         => _x( 'VCard', 'Post Type Singular Name', 'wpturbo' ),
        'menu_name'             => __( 'VCards', 'wpturbo' ),
        'name_admin_bar'        => __( 'VCard', 'wpturbo' ),
        'archives'              => __( 'VCard Archives', 'wpturbo' ),
        'attributes'            => __( 'VCard Attributes', 'wpturbo' ),
        'parent_item_colon'     => __( 'Parent VCard:', 'wpturbo' ),
        'all_items'             => __( 'All VCards', 'wpturbo' ),
        'add_new_item'          => __( 'Add New VCard', 'wpturbo' ),
        'add_new'               => __( 'Add New', 'wpturbo' ),
        'new_item'              => __( 'New VCard', 'wpturbo' ),
        'edit_item'             => __( 'Edit VCard', 'wpturbo' ),
        'update_item'           => __( 'Update VCard', 'wpturbo' ),
        'view_item'             => __( 'View VCard', 'wpturbo' ),
        'view_items'            => __( 'View VCards', 'wpturbo' ),
        'search_items'          => __( 'Search VCard', 'wpturbo' ),
        'not_found'             => __( 'Not found', 'wpturbo' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'wpturbo' ),
        'featured_image'        => __( 'Featured Image', 'wpturbo' ),
        'set_featured_image'    => __( 'Set featured image', 'wpturbo' ),
        'remove_featured_image' => __( 'Remove featured image', 'wpturbo' ),
        'use_featured_image'    => __( 'Use as featured image', 'wpturbo' ),
        'insert_into_item'      => __( 'Insert into VCard', 'wpturbo' ),
        'uploaded_to_this_item' => __( 'Uploaded to this VCard', 'wpturbo' ),
        'items_list'            => __( 'VCards list', 'wpturbo' ),
        'items_list_navigation' => __( 'VCards list navigation', 'wpturbo' ),
        'filter_items_list'     => __( 'Filter VCards list', 'wpturbo' ),
    );
    $args = array(
        'label'                 => __( 'VCard', 'wpturbo' ),
        'description'           => __( 'VCards', 'wpturbo' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
        'taxonomies'            => array( 'category', 'post_tag' ),
        'public'                => true,
        'menu_icon'             => 'dashicons-id',
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,
        'hierarchical'          => false,
        'exclude_from_search'   => false,
        'show_in_rest'          => true,
        'rest_base'             => 'vcards',
        'rest_controller_class' => 'WP_REST_Posts_Controller',
    );
    register_post_type( 'vcard', $args );
}
add_action( 'init', 'wpturbo_register_vcard_cpt', 0 );

// Add custom fields to VCards CPT
function wpturbo_add_vcard_fields( $post ) {
    wp_nonce_field( 'wpturbo_save_vcard_fields', 'wpturbo_vcard_fields_nonce' );
    ?>
    <p>
        <label for="vcard_photo"><?php esc_html_e( 'Photo', 'wpturbo' ); ?></label><br>
        <?php
            $vcard_photo = get_post_meta( $post->ID, 'vcard_photo', true );
            echo '<input type="text" id="vcard_photo" name="vcard_photo" value="' . esc_attr( $vcard_photo ) . '" class="regular-text" />';
        ?>
    </p>
    <p>
        <label for="vcard_name"><?php esc_html_e( 'Name', 'wpturbo' ); ?></label><br>
        <?php
            $vcard_name = get_post_meta( $post->ID, 'vcard_name', true );
            echo '<input type="text" id="vcard_name" name="vcard_name" value="' . esc_attr( $vcard_name ) . '" class="regular-text" />';
        ?>
    </p>
    <p>
        <label for="vcard_title"><?php esc_html_e( 'Title', 'wpturbo' ); ?></label><br>
        <?php
            $vcard_title = get_post_meta( $post->ID, 'vcard_title', true );
            echo '<input type="text" id="vcard_title" name="vcard_title" value="' . esc_attr( $vcard_title ) . '" class="regular-text" />';
        ?>
    </p>
    <p>
        <label for="vcard_description"><?php esc_html_e( 'Description', 'wpturbo' ); ?></label><br>
        <?php
            $vcard_description = get_post_meta( $post->ID, 'vcard_description', true );
            echo '<textarea id="vcard_description" name="vcard_description" class="large-text">' . esc_textarea( $vcard_description ) . '</textarea>';
        ?>
    </p>
    <p>
        <label><?php esc_html_e( 'Social Links', 'wpturbo' ); ?></label>
        <div id="wpturbo-social-links-container">
            <?php
                $social_links = get_post_meta( $post->ID, 'social_links', true );
                if ( ! empty( $social_links ) ) {
                    foreach ( $social_links as $social_link ) {
                        $social_url = $social_link['url'];
                        $social_icon = $social_link['icon'];
                        ?>
                        <div class="wpturbo-social-link">
                            <input type="text" name="social_links[url][]" value="<?php echo esc_attr( $social_url ); ?>" class="regular-text" />
                            <select name="social_links[icon][]">
                                <option value="facebook" <?php selected( $social_icon, 'facebook' ); ?>><?php esc_html_e( 'Facebook', 'wpturbo' ); ?></option>
                                <option value="twitter" <?php selected( $social_icon, 'twitter' ); ?>><?php esc_html_e( 'Twitter', 'wpturbo' ); ?></option>
                                <option value="linkedin" <?php selected( $social_icon, 'linkedin' ); ?>><?php esc_html_e( 'LinkedIn', 'wpturbo' ); ?></option>
                            </select>
                            <button class="button wpturbo-remove-social-link"><?php esc_html_e( 'Remove', 'wpturbo' ); ?></button>
                        </div>
                        <?php
                    }
                }
            ?>
        </div>
        <button class="button wpturbo-add-social-link"><?php esc_html_e( 'Add New Link', 'wpturbo' ); ?></button>
    </p>
    <p>
        <label for="vcard_vcf_link"><?php esc_html_e( 'VCF Download Link', 'wpturbo' ); ?></label><br>
        <?php
            $vcard_vcf_link = get_post_meta( $post->ID, 'vcard_vcf_link', true );
            echo '<input type="text" id="vcard_vcf_link" name="vcard_vcf_link" value="' . esc_attr( $vcard_vcf_link ) . '" class="regular-text" readonly />';
        ?>
        <button class="button wpturbo-generate-vcf-link"><?php esc_html_e( 'Generate VCF Link', 'wpturbo' ); ?></button>
    </p>
    <?php
}
add_action( 'add_meta_boxes_vcard', 'wpturbo_add_vcard_fields' );

// Save custom fields on VCards CPT save
function wpturbo_save_vcard_fields( $post_id ) {
    if ( ! isset( $_POST['wpturbo_vcard_fields_nonce'] ) || ! wp_verify_nonce( $_POST['wpturbo_vcard_fields_nonce'], 'wpturbo_save_vcard_fields' ) ) {
        return;
    }

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    if ( ! current_user_can( 'edit_post', $post_id ) ) {
        return;
    }

    if ( isset( $_POST['vcard_photo'] ) ) {
        update_post_meta( $post_id, 'vcard_photo', sanitize_text_field( $_POST['vcard_photo'] ) );
    }

    if ( isset( $_POST['vcard_name'] ) ) {
        update_post_meta( $post_id, 'vcard_name', sanitize_text_field( $_POST['vcard_name'] ) );
    }

    if ( isset( $_POST['vcard_title'] ) ) {
        update_post_meta( $post_id, 'vcard_title', sanitize_text_field( $_POST['vcard_title'] ) );
    }

    if ( isset( $_POST['vcard_description'] ) ) {
        update_post_meta( $post_id, 'vcard_description', sanitize_textarea_field( $_POST['vcard_description'] ) );
    }

    if ( isset( $_POST['social_links'] ) ) {
        $social_links = $_POST['social_links'];
        $sanitized_social_links = array();

        if ( ! empty( $social_links['url'] ) && is_array( $social_links['url'] ) ) {
            foreach ( $social_links['url'] as $key => $url ) {
                $sanitized_social_links[] = array(
                    'url'  => esc_url_raw( $url ),
                    'icon' => sanitize_text_field( $social_links['icon'][ $key ] ),
                );
            }
        }

        update_post_meta( $post_id, 'social_links', $sanitized_social_links );
    }

    if ( isset( $_POST['vcard_vcf_link'] ) ) {
        update_post_meta( $post_id, 'vcard_vcf_link', sanitize_text_field( $_POST['vcard_vcf_link'] ) );
    }
}
add_action( 'save_post_vcard', 'wpturbo_save_vcard_fields' );
				

In this modified code, the functions, hooks, and meta fields have been updated to reflect the change from "Contact" to "VCard." The post type registration, custom field addition, and saving mechanisms have all been adjusted accordingly.

Make sure to replace all occurrences of wpturbo with your own unique prefix. Additionally, don't forget to update any references to the "Contact" post type in your theme or plugin templates, shortcodes, or queries.

Remember to test the functionality thoroughly before deploying it to your live site, and make sure to backup your site before making any changes.

Register an account to save your snippets or go Pro to get more features.