Support

Account

Home Forums Front-end Issues group field in frontend page

Solved

group field in frontend page

  • Hi, I have a custom post type called “clients” and a group of ACF fields associated with it.
    I would like to know how to do so that the connected user can modify the custom fields of that group in the frontend.

    The post is created when a user with the role “customer” is created.

    Field group key is: “group_60c25840f1e02”.

    Can this be done?

    Thank you

    PS: I don’t have the PRO version.

  • I would create a page, for example “My Account” or something like this. Then I would create a page template for this page. The page template would make sure that the user is logged in, if not it would redirect to the login page.

    The next step is to get the logged in user id and call acf_form() with the post ID of the post that that user is allowed to edit.

  • Thanks for your answer.

    I was thinking of doing it with a short code on a page in the frontend.

    So far I have something like this:

    
    function profile_user_frontend($atts = array() ) {
    	if (is_user_logged_in() && current_user_can('cliente')) {
    	// Get the current user
    		$current_user_id = get_current_user_id();
    		acf_form([
    			'field_groups' => [group_60c25840f1e02],
    			'post_title'    => false,
            		'post_content'  => false,
            		'submit_value'  => __('Update meta')
    		]);
             } else {
                    return site_url('/ingreso/');
    }
    add_shortcode('profile_user_frontend','profile_user_frontend_shortcode');
    
  • The problem with using a shortcode is that acf_form_head() must be called before any output is created. Using a shortcode means that you’ll need to call this function of every page.

    You also need to use the post id of the post that the user can edit, so you’ll need to get the post related to the user and set “post_id” in acf_form or ACF will save the values to whatever page is currently being displayed.

  • How can I do that?

    You also need to use the post id of the post that the user can edit, so you’ll need to get the post related to the user and set “post_id” in acf_form or ACF will save the values to whatever page is currently being displayed.

  • I do not know based on the information I have. How is the post created? What is it that causes the user and the post that is automatically created related to each other?

  • How is the post created?

    
    /*
     * Agregar entrada personalizada "clientes" cuando se registra un nuevo usuario
     */
    
    add_action( 'user_register', 'agregar_cp_clientes_usuario', 10, 1 );
    
    function agregar_cp_clientes_usuario( $user_id ) {
       // Get user info
        $user_info = get_userdata( $user_id );
        $user_roles = $user_info->roles;
    
        // New code added 
        $this_user_role = implode(', ', $user_roles );
    
        if ($this_user_role == 'cliente') {
    
            // Create a new post
            $user_post = array(
                'post_title'   => $user_info->user_nicename,
                'post_author'   => $user_info->ID,
                'post_status'  => 'publish', // <- here is to publish
                'post_type'    => 'clientes', // <- change to your cpt
            );
            // Insert the post into the database
            $post_id = wp_insert_post( $user_post );
        }
    }
    

    What is it that causes the user and the post that is automatically created related to each other?

    They are related by the author of the post.
    That is, the post is added automatically, when a new user registers.

  • Is the the only post that is created with with the post_author set to the user ID?

  • Is the the only post that is created with with the post_author set to the user ID?

    In the “customers” custom post type , yes.

  • 
    $current_user_id = get_current_user_id();
    $args = array(
      'post_type' => 'customers',
      'author' => $current_user_id
    );
    $posts = get_posts($args);
    if ($posts) {
      $post_id = $posts[0]->ID;
    }
    
  • Thank you very much for your answer.

    I had the problem that you were marking me with doing it through a short code, so I am doing it as a template.

    I never did it this way, it marks me an error and I don’t know what it is.

    In the loop I have the following:

    
    			<?php while ( have_posts() ) : the_post(); ?>
    
    	            <?php if (is_user_logged_in() && current_user_can('cliente')) : ?>
                		<?php $current_user_id = get_current_user_id(); ?>
                		<?php $args = array(
                		  'post_type' => 'clientes',
                		  'author' => $current_user_id
                		); ?>
                		<?php $posts = get_posts($args); ?>
                		<?php if ($posts) 
                		  $post_id = $posts[0]->ID;
                	     ?>
                    : ?>
    			<?php endwhile; ?>
    
  • 
    			<?php while ( have_posts() ) : the_post(); ?>
    
    	            <?php 
                	if (is_user_logged_in() && current_user_can('cliente')) {
                	// Get the current user
                		$current_user_id = get_current_user_id();
                		$args = array(
                		  'post_type' => 'clientes',
                		  'author' => $current_user_id
                		);
                		$posts = get_posts($args);
                		if ($posts) {
                		  $post_id = $posts[0]->ID;
                	       acf_form(array(
    		                	'post_id'  =>  $post_id,
    			                'submit_value' => __('Actualizar Mi perfil'),
    		                ));
                		}
                	}
                	?>
    			<?php endwhile; ?>
    
  • I solved it by adapting John’s code. Thank you.

    
    			<?php while ( have_posts() ) : the_post(); ?>
    
    	            <?php 
                	if (is_user_logged_in() && current_user_can('cliente')) {
                	// Get the current user
                		$current_user_id = get_current_user_id();
                		$args = array(
                		  'post_type' => 'clientes',
                		  'author' => $current_user_id
                		);
                		$posts = get_posts($args);
                		if ($posts) {
                		  $post_id = $posts[0]->ID;
                		}
    
         	       acf_form(array(
    	               	'post_id'  =>  $post_id,
    	                'submit_value' => __('Actualizar Mi perfil'),
    	           ));
                	}
                	?>
    			<?php endwhile; ?>
    
Viewing 13 posts - 1 through 13 (of 13 total)

You must be logged in to reply to this topic.