Hello world!

Welcome to WordPress. This is your first post. Edit or delete it, then start writing!

‘Worker Posts’,
‘singular_name’ => ‘Worker Post’,
‘add_new_item’ => ‘Add Worker Post’,
‘edit_item’ => ‘Edit Worker Post’,
‘new_item’ => ‘New Worker Post’,
‘view_item’ => ‘View Worker Post’,
‘search_items’ => ‘Search Worker Posts’,
);

$args = array(
‘labels’ => $labels,
‘public’ => true,
‘has_archive’ => true,
‘rewrite’ => array(‘slug’ => ‘worker-posts’),
‘supports’ => array(‘title’,’editor’,’author’),
‘show_in_rest’ => true,
);

register_post_type(‘worker_post’, $args);
}
add_action(‘init’,’wpp_register_worker_post_type’);

/* ————————-
2) Shortcode: Worker Submission Form
————————- */
function wpp_worker_form_shortcode() {
if (isset($_POST[‘wpp_submit’]) && wp_verify_nonce($_POST[‘wpp_nonce’] ?? ”, ‘wpp_submit_nonce’)) {
$title = sanitize_text_field($_POST[‘work_title’] ?? ”);
$description = wp_kses_post($_POST[‘work_description’] ?? ”);
$location = sanitize_text_field($_POST[‘work_location’] ?? ”);
$contact_email = sanitize_email($_POST[‘work_contact_email’] ?? ”);

// Basic validation
$errors = array();
if (empty($title)) $errors[] = ‘Please enter a work title.’;
if (empty($contact_email) || !is_email($contact_email)) $errors[] = ‘Please enter a valid contact email.’;

// Handle file upload
$resume_url = ”;
if (!empty($_FILES[‘worker_resume’][‘name’])) {
require_once(ABSPATH . ‘wp-admin/includes/file.php’);
$file = $_FILES[‘worker_resume’];
// Allowed mime types
$allowed = array(‘pdf’,’doc’,’docx’,’jpg’,’jpeg’,’png’);
$filetype = wp_check_filetype( $file[‘name’] );
$ext = strtolower(pathinfo($file[‘name’], PATHINFO_EXTENSION));
if (!in_array($ext, $allowed)) {
$errors[] = ‘Resume must be PDF, DOC, DOCX, JPG or PNG.’;
} else {
// Use wp_handle_upload
$overrides = array(‘test_form’ => false);
$movefile = wp_handle_upload($file, $overrides);
if (isset($movefile[‘error’])) {
$errors[] = ‘File upload error: ‘ . esc_html($movefile[‘error’]);
} else {
// Insert into Media Library
$filetype = wp_check_filetype( basename( $movefile[‘file’] ), null );
$attachment = array(
‘guid’ => $movefile[‘url’],
‘post_mime_type’ => $filetype[‘type’],
‘post_title’ => preg_replace(‘/\.[^.]+$/’, ”, basename( $movefile[‘file’] )),
‘post_content’ => ”,
‘post_status’ => ‘inherit’
);
$attach_id = wp_insert_attachment( $attachment, $movefile[‘file’] );
require_once(ABSPATH . ‘wp-admin/includes/image.php’);
$attach_data = wp_generate_attachment_metadata( $attach_id, $movefile[‘file’] );
wp_update_attachment_metadata( $attach_id, $attach_data );
$resume_url = wp_get_attachment_url($attach_id);
}
}
}

if (empty($errors)) {
// Insert Worker Post (pending by default)
$post_id = wp_insert_post(array(
‘post_title’ => $title,
‘post_content’ => $description,
‘post_type’ => ‘worker_post’,
‘post_status’ => ‘pending’, // change to ‘publish’ to auto publish
));

if ($post_id && !is_wp_error($post_id)) {
update_post_meta($post_id, ‘work_location’, $location);
update_post_meta($post_id, ‘work_contact_email’, $contact_email);
if ($resume_url) update_post_meta($post_id, ‘worker_resume’, esc_url_raw($resume_url));
// Optional: send notification to admin
$admin_email = get_option(‘admin_email’);
$subject = ‘New Worker Post Submitted’;
$message = “A new worker post was submitted:\n\nTitle: $title\nView in admin: ” . admin_url(“post.php?post={$post_id}&action=edit”);
wp_mail($admin_email, $subject, $message);

$success_message = ‘Your work request has been submitted. It will be reviewed shortly.’;
} else {
$errors[] = ‘Could not save your post. Try again later.’;
}
}
}

// Form HTML
ob_start();
if (!empty($errors)) {
echo ‘

    ‘;
    foreach ($errors as $err) echo ‘

  • ‘ . esc_html($err) . ‘
  • ‘;
    echo ‘

‘;
}
if (!empty($success_message)) {
echo ‘

‘ . esc_html($success_message) . ‘

‘;
}

?>

10,
), $atts, ‘worker_listings’ );

// Handle apply form submission
if (isset($_POST[‘wpp_apply’]) && wp_verify_nonce($_POST[‘wpp_apply_nonce’] ?? ”, ‘wpp_apply_nonce’)) {
$apply_post_id = intval($_POST[‘apply_post_id’] ?? 0);
$employer_name = sanitize_text_field($_POST[’employer_name’] ?? ”);
$employer_email = sanitize_email($_POST[’employer_email’] ?? ”);
$employer_message = sanitize_textarea_field($_POST[’employer_message’] ?? ”);

$worker_email = get_post_meta($apply_post_id, ‘work_contact_email’, true);
if ($worker_email && is_email($worker_email)) {
$subject = “Application for your work: ” . get_the_title($apply_post_id);
$body = “Hello,\n\nYou have a new application for your post on the site.\n\nApplicant: $employer_name\nEmail: $employer_email\n\nMessage:\n$employer_message\n\nView post: ” . get_permalink($apply_post_id);
wp_mail($worker_email, $subject, $body);
$apply_notice = ‘Your message has been sent to the worker.’;
} else {
$apply_notice = ‘Worker contact email not available. Admin will be notified.’;
// Send to admin as fallback
wp_mail(get_option(‘admin_email’), ‘Application fallback’, $body);
}
}

// Query published worker posts
$q = new WP_Query(array(
‘post_type’ => ‘worker_post’,
‘post_status’ => ‘publish’,
‘posts_per_page’ => intval($atts[‘posts_per_page’]),
));

ob_start();
if (!empty($apply_notice)) echo ‘

‘ . esc_html($apply_notice) . ‘

‘;

if ($q->have_posts()) {
echo ‘

‘;
while ($q->have_posts()) {
$q->the_post();
$pid = get_the_ID();
$title = get_the_title();
$desc = get_the_excerpt() ?: wp_trim_words(get_the_content(), 30);
$location = get_post_meta($pid,’work_location’,true);
$resume = get_post_meta($pid,’worker_resume’,true);
echo ‘

‘;
echo ‘

‘ . esc_html($title) . ‘

‘;
if ($location) echo ‘

Location: ‘ . esc_html($location) . ‘

‘;
echo ‘

‘ . wp_kses_post(wpautop($desc)) . ‘

‘;
if ($resume) echo ‘

Download Resume

‘;

// Apply form
echo ‘

‘;
echo ‘‘;
echo ‘

‘; // apply-form
echo ‘

‘; // wpp-apply

echo ‘

‘; // listing
}
echo ‘

‘; // listings
wp_reset_postdata();
} else {
echo ‘

No worker posts found.

‘;
}

return ob_get_clean();
}
add_shortcode(‘worker_listings’,’wpp_worker_listings_shortcode’);

/* ————————-
4) Tiny CSS in admin header
————————- */
function wpp_enqueue_styles() {
// Minimal inline styles if desired
wp_register_style(‘wpp-inline’, false);
wp_enqueue_style(‘wpp-inline’);
$custom_css = “
.wpp-worker-form input, .wpp-worker-form textarea { padding:8px; border:1px solid #ccc; border-radius:3px; }
.wpp-listing h3 { margin:0 0 6px; }
“;
wp_add_inline_style(‘wpp-inline’, $custom_css);
}
add_action(‘wp_enqueue_scripts’,’wpp_enqueue_styles’);

<?php
/*
Plugin Name: Worker Posts & Listings
Description: Allow workers to post work requests (with resume upload) and employers to view listings and apply.
Version: 1.0
Author: Your Name
*/

if (!defined('ABSPATH')) exit;

/* -------------------------
   1) Register Custom Post Type
   ------------------------- */
function wpp_register_worker_post_type() {
    $labels = array(
        'name' => 'Worker Posts',
        'singular_name' => 'Worker Post',
        'add_new_item' => 'Add Worker Post',
        'edit_item' => 'Edit Worker Post',
        'new_item' => 'New Worker Post',
        'view_item' => 'View Worker Post',
        'search_items' => 'Search Worker Posts',
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'worker-posts'),
        'supports' => array('title','editor','author'),
        'show_in_rest' => true,
    );

    register_post_type('worker_post', $args);
}
add_action('init','wpp_register_worker_post_type');

/* -------------------------
   2) Shortcode: Worker Submission Form
   ------------------------- */
function wpp_worker_form_shortcode() {
    if (isset($_POST['wpp_submit']) && wp_verify_nonce($_POST['wpp_nonce'] ?? '', 'wpp_submit_nonce')) {
        $title = sanitize_text_field($_POST['work_title'] ?? '');
        $description = wp_kses_post($_POST['work_description'] ?? '');
        $location = sanitize_text_field($_POST['work_location'] ?? '');
        $contact_email = sanitize_email($_POST['work_contact_email'] ?? '');

        // Basic validation
        $errors = array();
        if (empty($title)) $errors[] = 'Please enter a work title.';
        if (empty($contact_email) || !is_email($contact_email)) $errors[] = 'Please enter a valid contact email.';

        // Handle file upload
        $resume_url = '';
        if (!empty($_FILES['worker_resume']['name'])) {
            require_once(ABSPATH . 'wp-admin/includes/file.php');
            $file = $_FILES['worker_resume'];
            // Allowed mime types
            $allowed = array('pdf','doc','docx','jpg','jpeg','png');
            $filetype = wp_check_filetype( $file['name'] );
            $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
            if (!in_array($ext, $allowed)) {
                $errors[] = 'Resume must be PDF, DOC, DOCX, JPG or PNG.';
            } else {
                // Use wp_handle_upload
                $overrides = array('test_form' => false);
                $movefile = wp_handle_upload($file, $overrides);
                if (isset($movefile['error'])) {
                    $errors[] = 'File upload error: ' . esc_html($movefile['error']);
                } else {
                    // Insert into Media Library
                    $filetype = wp_check_filetype( basename( $movefile['file'] ), null );
                    $attachment = array(
                        'guid' => $movefile['url'],
                        'post_mime_type' => $filetype['type'],
                        'post_title' => preg_replace('/\.[^.]+$/', '', basename( $movefile['file'] )),
                        'post_content' => '',
                        'post_status' => 'inherit'
                    );
                    $attach_id = wp_insert_attachment( $attachment, $movefile['file'] );
                    require_once(ABSPATH . 'wp-admin/includes/image.php');
                    $attach_data = wp_generate_attachment_metadata( $attach_id, $movefile['file'] );
                    wp_update_attachment_metadata( $attach_id, $attach_data );
                    $resume_url = wp_get_attachment_url($attach_id);
                }
            }
        }

        if (empty($errors)) {
            // Insert Worker Post (pending by default)
            $post_id = wp_insert_post(array(
                'post_title' => $title,
                'post_content' => $description,
                'post_type' => 'worker_post',
                'post_status' => 'pending', // change to 'publish' to auto publish
            ));

            if ($post_id && !is_wp_error($post_id)) {
                update_post_meta($post_id, 'work_location', $location);
                update_post_meta($post_id, 'work_contact_email', $contact_email);
                if ($resume_url) update_post_meta($post_id, 'worker_resume', esc_url_raw($resume_url));
                // Optional: send notification to admin
                $admin_email = get_option('admin_email');
                $subject = 'New Worker Post Submitted';
                $message = "A new worker post was submitted:\n\nTitle: $title\nView in admin: " . admin_url("post.php?post={$post_id}&action=edit");
                wp_mail($admin_email, $subject, $message);

                $success_message = 'Your work request has been submitted. It will be reviewed shortly.';
            } else {
                $errors[] = 'Could not save your post. Try again later.';
            }
        }
    }

    // Form HTML
    ob_start();
    if (!empty($errors)) {
        echo '<div class="wpp-errors" style="color:red;"><ul>';
        foreach ($errors as $err) echo '<li>' . esc_html($err) . '</li>';
        echo '</ul></div>';
    }
    if (!empty($success_message)) {
        echo '<div class="wpp-success" style="color:green;">' . esc_html($success_message) . '</div>';
    }

    ?>
    <form method="post" enctype="multipart/form-data" class="wpp-worker-form">
      <?php wp_nonce_field('wpp_submit_nonce','wpp_nonce'); ?>
      <p><label>Work Title (what you need) <br>
      <input type="text" name="work_title" required style="width:100%" value="<?php echo esc_attr($_POST['work_title'] ?? ''); ?>"></label></p>

      <p><label>Work Details <br>
      <textarea name="work_description" rows="6" style="width:100%"><?php echo esc_textarea($_POST['work_description'] ?? ''); ?></textarea></label></p>

      <p><label>Location <br>
      <input type="text" name="work_location" style="width:100%" value="<?php echo esc_attr($_POST['work_location'] ?? ''); ?>"></label></p>

      <p><label>Contact Email <br>
      <input type="email" name="work_contact_email" required style="width:100%" value="<?php echo esc_attr($_POST['work_contact_email'] ?? ''); ?>"></label></p>

      <p><label>Upload Resume (PDF, DOC, JPG) <br>
      <input type="file" name="worker_resume" accept=".pdf,.doc,.docx,.jpg,.jpeg,.png"></label></p>

      <p><input type="submit" name="wpp_submit" value="Post Work Request"></p>
    </form>
    <?php

    return ob_get_clean();
}
add_shortcode('worker_post_form','wpp_worker_form_shortcode');

/* -------------------------
   3) Shortcode: Worker Listings with Apply Form
   ------------------------- */
function wpp_worker_listings_shortcode($atts) {
    $atts = shortcode_atts( array(
        'posts_per_page' => 10,
    ), $atts, 'worker_listings' );

    // Handle apply form submission
    if (isset($_POST['wpp_apply']) && wp_verify_nonce($_POST['wpp_apply_nonce'] ?? '', 'wpp_apply_nonce')) {
        $apply_post_id = intval($_POST['apply_post_id'] ?? 0);
        $employer_name = sanitize_text_field($_POST['employer_name'] ?? '');
        $employer_email = sanitize_email($_POST['employer_email'] ?? '');
        $employer_message = sanitize_textarea_field($_POST['employer_message'] ?? '');

        $worker_email = get_post_meta($apply_post_id, 'work_contact_email', true);
        if ($worker_email && is_email($worker_email)) {
            $subject = "Application for your work: " . get_the_title($apply_post_id);
            $body = "Hello,\n\nYou have a new application for your post on the site.\n\nApplicant: $employer_name\nEmail: $employer_email\n\nMessage:\n$employer_message\n\nView post: " . get_permalink($apply_post_id);
            wp_mail($worker_email, $subject, $body);
            $apply_notice = 'Your message has been sent to the worker.';
        } else {
            $apply_notice = 'Worker contact email not available. Admin will be notified.';
            // Send to admin as fallback
            wp_mail(get_option('admin_email'), 'Application fallback', $body);
        }
    }

    // Query published worker posts
    $q = new WP_Query(array(
        'post_type' => 'worker_post',
        'post_status' => 'publish',
        'posts_per_page' => intval($atts['posts_per_page']),
    ));

    ob_start();
    if (!empty($apply_notice)) echo '<div class="wpp-apply-notice" style="color:green;">' . esc_html($apply_notice) . '</div>';

    if ($q->have_posts()) {
        echo '<div class="wpp-listings">';
        while ($q->have_posts()) {
            $q->the_post();
            $pid = get_the_ID();
            $title = get_the_title();
            $desc = get_the_excerpt() ?: wp_trim_words(get_the_content(), 30);
            $location = get_post_meta($pid,'work_location',true);
            $resume = get_post_meta($pid,'worker_resume',true);
            echo '<div class="wpp-listing" style="border:1px solid #ddd;padding:12px;margin-bottom:12px;">';
            echo '<h3>' . esc_html($title) . '</h3>';
            if ($location) echo '<p><strong>Location:</strong> ' . esc_html($location) . '</p>';
            echo '<p>' . wp_kses_post(wpautop($desc)) . '</p>';
            if ($resume) echo '<p><a href="'.esc_url($resume).'" target="_blank" rel="noopener">Download Resume</a></p>';

            // Apply form
            echo '<div class="wpp-apply">';
            echo '<button class="wpp-toggle-apply" onclick="document.getElementById(\'apply-form-'. $pid .'\').style.display = (document.getElementById(\'apply-form-'. $pid .'\').style.display==\'block\'? \'none\': \'block\')">Apply / Contact Worker</button>';
            echo '<div id="apply-form-'. $pid .'" style="display:none;margin-top:8px;">';
            echo '<form method="post">';
            wp_nonce_field('wpp_apply_nonce','wpp_apply_nonce');
            echo '<input type="hidden" name="apply_post_id" value="'. esc_attr($pid) .'">';
            echo '<p><input type="text" name="employer_name" placeholder="Your Name" style="width:100%" required></p>';
            echo '<p><input type="email" name="employer_email" placeholder="Your Email" style="width:100%" required></p>';
            echo '<p><textarea name="employer_message" placeholder="Message to worker" rows="4" style="width:100%" required></textarea></p>';
            echo '<p><input type="submit" name="wpp_apply" value="Send Message"></p>';
            echo '</form>';
            echo '</div>'; // apply-form
            echo '</div>'; // wpp-apply

            echo '</div>'; // listing
        }
        echo '</div>'; // listings
        wp_reset_postdata();
    } else {
        echo '<p>No worker posts found.</p>';
    }

    return ob_get_clean();
}
add_shortcode('worker_listings','wpp_worker_listings_shortcode');

/* -------------------------
   4) Tiny CSS in admin header
   ------------------------- */
function wpp_enqueue_styles() {
    // Minimal inline styles if desired
    wp_register_style('wpp-inline', false);
    wp_enqueue_style('wpp-inline');
    $custom_css = "
    .wpp-worker-form input, .wpp-worker-form textarea { padding:8px; border:1px solid #ccc; border-radius:3px; }
    .wpp-listing h3 { margin:0 0 6px; }
    ";
    wp_add_inline_style('wpp-inline', $custom_css);
}
add_action('wp_enqueue_scripts','wpp_enqueue_styles');

1 thought on “Hello world!”

Leave a Comment

Your email address will not be published. Required fields are marked *