Front End Edit Custom Post Type

Home » Snippets » Front End Edit Custom Post Type
0

Created with:

Visibility: 

public

Creator: Shanna Middleton

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
					<?php
// Ensure the ACF form helper functions are included only once on the init hook
if (!function_exists('wpturbo_acf_form_head')) {
    function wpturbo_acf_form_head() {
        if (function_exists('acf_form_head')) {
            acf_form_head();
        }
    }
    add_action('init', 'wpturbo_acf_form_head');
}

// ACF form shortcode function for editing a user's profile
function wpturbo_edit_profile_form_shortcode($atts) {
    // Check if the user is logged in
    if (!is_user_logged_in()) {
        return '<p>You must be logged in to edit your profile.</p>';
    }

    // Get the current user's ID
    $current_user_id = get_current_user_id();

    // Get the profile post ID associated with the current user
    $profile_post_id = wpturbo_get_user_profile_post_id($current_user_id);

    // If no profile post found, return error message
    if (!$profile_post_id) {
        return '<p>Error: No profile found to edit.</p>';
    }

    // Check if the current user can edit the post
    if (!current_user_can('edit_post', $profile_post_id)) {
        return '<p>You do not have permission to edit this profile.</p>';
    }

    // Prepare form options for updating the profile
    $options = array(
        'post_id'       => $profile_post_id,
        'field_groups'  => (isset($atts['field_group']) ? array($atts['field_group']) : array()),
        'submit_value'  => 'Update Profile'
    );

    // Output buffering to capture the form
    ob_start();
    acf_form($options);
    $form = ob_get_clean();

    return $form;
}
add_shortcode('wpturbo_edit_profile_form', 'wpturbo_edit_profile_form_shortcode');

// Helper function to get the profile post ID associated with a user
function wpturbo_get_user_profile_post_id($user_id) {
    // Define the arguments for the WP_Query
    $args = array(
        'author'        => $user_id,
        'post_type'     => 'profile', // Replace with your actual profile post type
        'post_status'   => 'any',
        'posts_per_page'=> 1,
        'fields'        => 'ids'
    );

    // Perform the query
    $profile_posts = get_posts($args);

    // If we found a profile post, return its ID
    if (!empty($profile_posts)) {
        return $profile_posts[0];
    }

    // If no profile post found, return false
    return false;
}
				

This code includes everything needed to create an edit profile form for logged-in users. The shortcode [wpturbo_edit_profile_form] can be placed on any page or post. When a logged-in user visits that page, they will see a form to edit their profile, which is a custom post type named 'profile'. If you have a different post type for profiles or any specific field groups, you need to replace 'profile' with the actual post type you are using.

Please copy and paste this entire code into your wp-plugin.php file, and it should work without the need for any further modifications.

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