1

I need to create a page for my collaborators which will be divided by region in which they operate and by profession.

I would like to create a form that they will fill out themselves and after sending it, personal cards will be generated in the appropriate section. How can I do it without installing heavy plugins?

I've seen some methods around, but nothing efficient and correct.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

1 Answers1

-2

Create a Custom Post Type:

// Add this code to your theme's functions.php file or a custom plugin
 function create_collaborator_post_type() {
 $args = array(
    'public' => true,
    'label' => 'Collaborators',
    'supports' => array('title', 'editor', 'thumbnail'),
    // Add any additional fields you need for collaborators
    // such as 'region', 'profession', etc.
);
register_post_type('collaborator', $args);
 }
add_action('init', 'create_collaborator_post_type');

Create Page Templates: Create separate template files for each region and profession. For example, create page-region.php and page-profession.php in your theme folder.

Add Form to Each Template: In each template file, add a form to collect the collaborator information:

<!-- Add this code to page-region.php -->
<form method="post">
<label for="name">Name:</label>
<input type="text" name="name" id="name" required>

<!-- Add more fields for region-specific information -->

<input type="submit" value="Submit">
 </form>

<!-- Add this code to page-profession.php -->
<form method="post">
<label for="name">Name:</label>
<input type="text" name="name" id="name" required>

<!-- Add more fields for profession-specific information -->

 <input type="submit" value="Submit">
 </form>

Process Form Submission: Handle the form submission in each template file and create a new post for the collaborator:

 // Add this code to page-region.php and page-profession.php after the form HTML
 if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = $_POST['name'];
// Get other form fields

$post_data = array(
    'post_title' => $name,
    'post_type' => 'collaborator',
    // Set other post meta values based on form input
);

$post_id = wp_insert_post($post_data);
if ($post_id) {
    // Display a success message or redirect to a thank-you page
} else {
    // Display an error message
}
   }

Display Personal Cards: Query and display the personal cards in each template file:

// Add this code to page-region.php and page-profession.php after the form processing code
     $args = array(
    'post_type' => 'collaborator',
    // Add additional arguments to filter by region or profession
    );

  $query = new WP_Query($args);
  if ($query->have_posts()) {
   while ($query->have_posts()) {
    $query->the_post();
    
    // Display the collaborator information
    the_title('<h2>', '</h2>');
    the_content();
    // Display additional fields
    
    // Add appropriate HTML and styling for the personal cards
}

wp_reset_postdata();
 } else {
// Display a message when no collaborators are found
 }

Remember to customize the code according to your specific requirements, such as adding additional fields, styling, and customizing the loop to display the collaborator details as desired.

Please note that this code serves as a starting point and may require further adjustments based on your specific needs and theme structure.

Mr coder
  • 4
  • 2
  • 1
    Most or all of your 13 answers (and one NAA) appear likely to have been entirely or partially written by AI (e.g., ChatGPT). Please be aware that [posting of AI-generated content is banned here](//meta.stackoverflow.com/q/421831). If you used an AI tool to assist with any answer, I would encourage you to delete it. Thanks! – NotTheDr01ds Jun 14 '23 at 14:18
  • 1
    **Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly. The moderation team can use your help to identify quality issues. – NotTheDr01ds Jun 14 '23 at 14:18