Home Forums Plugin Support WP User Frontend Pro Users can post in the name of a company

This topic is: resolved

This topic contains 22 replies, has 2 voices, and was last updated by  noomia 6 years, 1 month ago.

Viewing 8 posts - 16 through 23 (of 23 total)
  • Author
    Posts
  • #4275

    noomia
    Participant
    Post count: 115

    Hi Tarek !

    I choose to do it with custom field. It's a hidden one so I create it with action hook in WPUF.

    So in Front-end, I have my hidden field in the source page. But I don't have it in the wp-admin in the WPUF form meta box. How can I have it there ?

    #4285

    Tareq Hasan
    Keymaster
    Post count: 2831

    Are you sure it doesn't adds in the admin area? It should actually.

    #4307

    noomia
    Participant
    Post count: 115

    Hi Tarek !

    Yes I am. In fact I have the field in the default custom field box of WordPress, but I want it in the WPUF box…

    What I do :
    In my WPUF form, I have an action hook field with this action hook ‘which_companies_hook'.
    If we are in a front-end page, I generate a hidden field which contains the company ID.
    If we are in the admin, the hidden field is replaced by a select dropdown which contains all the company's name (related to the right ID), so I can change the company as an admin.

    Here is my code :

    function hidden_field_which_company( $form_id, $post_id, $form_settings ) {
        global $current_user;
        $post = get_post($post_id);
        if(get_post_meta( $post_id,'company_id', true )) {
    	    $postParent = get_post_meta( $post_id,'company_id', true );
        }
        
        if (is_admin()) { //If we are in the wp-admin, dropdown list
        
    	    // generate your post dropdown
    	    $args= array( 
    	        'post_type' => 'companies',
    	        'showposts' => -1,
    	        'post_status' => 'publish',
    	        'order' => 'ASC',
    	        'orderby' => 'title'
    	    );
    	    $my_companies_query = new WP_Query($args); 
    	    
    	    ?>	
    	    <div class="wpuf-fields">
    	
    	        <select id="whichCompany" name="whichCompany" data-type="select">
    	        	<option value="">None</option>
    	            <?php while ($my_companies_query->have_posts()) : $my_companies_query->the_post(); ?>
    		            <option value="<?php echo (get_the_ID()); ?>" <?php if(get_the_ID() == $postParent) { ?>selected="selected" <?php } ?>><?php the_title(); ?></option>
    		        <?php endwhile;?>
    	        </select>
    	        <span class="wpuf-help"></span>
    	    </div>
    	
    		<?php
    		wp_reset_postdata(); 
    	} else { //If we are not in the front-end, hidden field
    		
    	    if($_GET['companyId']) { //On est sur une page de création
    	    	$post = get_post($_GET['companyId']);
    	    	if ( $current_user->ID == $post->post_author ){ ?>
    	    		<div class="wpuf-fields">
    			        <input type="hidden" id="whichCompany" name="whichCompany" value="<?php echo($_GET['companyId']); ?>" />
    			    </div>
    	    	<?php } else {
    		    	header('Location: '.home_url());
    		    	exit();
    	    	}
    	    } else {
    	    	if($postParent) { //On est sur une page d'édition
    				?>
    				<div class="wpuf-fields">
    					<input type="hidden" id="whichCompany" name="whichCompany" value="<?php echo($postParent); ?>" />
    				</div>
    				<?php
    	    	} else {
    				header('Location: '.home_url());
    				exit();   
    			}
    		} 
    	}   
    }
    add_action( 'which_companies_hook', 'hidden_field_which_company', 10, 3 );
    
    function wpufe_insert_postParent( $post_id ) {
    	    if ( isset( $_POST['whichCompany'] ) ) {
    	       	$my_post = array();
    	       	$my_post['ID'] = $post_id;
    	       	$newpost_id=wp_update_post( $my_post );
    	       	if($newpost_id!=0) {
    		       	$whichCompany=$_POST['whichCompany'];
    		       	update_post_meta ($newpost_id, 'company_id', $whichCompany);
    	       	}
    	    }
    }
    add_action( 'wpuf_add_post_after_insert', 'wpufe_insert_postParent' );
    add_action( 'wpuf_edit_post_after_update', 'wpufe_insert_postParent' );

    I have the hidden field in the Front-end, but I have nothing (hidden or dropdown) in the wp-admin (in the WPUF custom field box).

    How can I have the dropdown in the admin ?

    Thanks !

    #4458

    noomia
    Participant
    Post count: 115

    I can't find how to deal with it… Any help would be appreciated.

    #4474

    Tareq Hasan
    Keymaster
    Post count: 2831

    Oh, now I get it. In the admin panel, only custom fields are displayed in the “WPUF Custom Fields” meta box, you can't see the action hooks. In that case, you've add your own meta box.

    #4534

    noomia
    Participant
    Post count: 115

    This is what I was afraid of… No way to add it to the existing WPUF metabox ? It seems that I need to create mine 😉 !

    #4538

    Tareq Hasan
    Keymaster
    Post count: 2831

    Yeah, you need to create yours.

    #4541

    noomia
    Participant
    Post count: 115

    Thank you for you help. Resolved !

Viewing 8 posts - 16 through 23 (of 23 total)

The topic ‘Users can post in the name of a company’ is closed to new replies.