Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include Deactivate Listing in Job Actions #2860

Open
Scotm19 opened this issue Dec 3, 2024 · 2 comments
Open

Include Deactivate Listing in Job Actions #2860

Scotm19 opened this issue Dec 3, 2024 · 2 comments

Comments

@Scotm19
Copy link

Scotm19 commented Dec 3, 2024

Is your feature request related to a problem? Please describe

Employers often need to remove job listings for various reasons, but the only option they have is to Delete a job from their jobs dashboard.

Describe the solution you'd like

Include an action in the jobs dashboard menu that allows employers to deactivate a job listing (sending the listing to Draft status).

@Braehler
Copy link

Braehler commented Dec 6, 2024

@Scotm19
this is a long awaited function but still not included. I recently got this to work. Just put this into your functions.php

`<?php
/**

  • Function to add deactivate Button toJob Dashboard
  • Works with WP Job Manager 2.4.0 and WC Paid Listings 3.0.3
    */

// add Button
add_action('job_manager_my_job_actions', 'custom_add_deactivate_button', 10, 2);
function custom_add_deactivate_button($actions, $job) {
if ($job->post_status === 'publish') {
// Basie-URL Dashboards
$dashboard_url = get_permalink(get_option('job_manager_job_dashboard_page_id'));

    // URLwith correct parameters
    $actions['deactivate'] = array(
        'label' => __('Deactivate', 'wp-job-manager'),
        'url'   => add_query_arg(array(
            'action' => 'deactivate',
            'job_id' => $job->ID
        ), $dashboard_url)
    );
}
return $actions;

}

// prepare deactivate
add_action('wp', 'custom_process_job_deactivation');
function custom_process_job_deactivation() {
if (!isset($_GET['action']) || $_GET['action'] !== 'deactivate') {
return;
}

if (empty($_GET['job_id'])) {
    return;
}

$job_id = absint($_GET['job_id']);

// check permission
$job = get_post($job_id);
if (!$job || $job->post_author != get_current_user_id()) {
    wp_die(__('You do not have the permission to deactivate', 'wp-job-manager'));
}

// update Job Status
wp_update_post(array(
    'ID' => $job_id,
    'post_status' => 'expired'
));

// WC Paid Listings 
if (class_exists('WC_Paid_Listings')) {
    delete_post_meta($job_id, '_package_id');
    delete_post_meta($job_id, '_package_subscription_id');
    delete_post_meta($job_id, '_featured');
}

// clear Cache
clean_post_cache($job_id);

// redirect to job dashboard
wp_redirect(add_query_arg('msg', 'deactivated', get_permalink(get_option('job_manager_job_dashboard_page_id'))));
exit;

}

// success message
add_action('job_manager_job_dashboard_before', 'custom_show_deactivation_notice');
function custom_show_deactivation_notice() {
if (isset($GET['msg']) && $GET['msg'] === 'deactivated') {
echo '

' .
esc_html
('Success.', 'wp-job-manager') .
'
';
}
}`

@Ericmuc
Copy link

Ericmuc commented Jan 14, 2025

hey, great, thanks, I have adopted it and it works just fine, it should find its way into the core, very helpful.

Here the WPML adapted version, remains in the current language:

/**

  • Function to add deactivate Button to Job Dashboard

  • Works with WP Job Manager 2.4.0 and WC Paid Listings 3.0.3
    */
    // Add Deactivate Button
    add_action('job_manager_my_job_actions', 'custom_add_deactivate_button', 10, 2);
    function custom_add_deactivate_button($actions, $job) {
    if ($job->post_status === 'publish') {
    // Base URL Dashboard
    $dashboard_url = get_permalink(get_option('job_manager_job_dashboard_page_id'));

     // Add Button with correct parameters
     $actions['deactivate'] = array(
         'label' => __('Deactivate', 'wp-job-manager'),
         'url'   => add_query_arg(array(
             'action' => 'deactivate',
             'job_id' => $job->ID
         ), $dashboard_url)
     );
    

    }
    return $actions;
    }

// Prepare Deactivate
add_action('wp', 'custom_process_job_deactivation');
function custom_process_job_deactivation() {
if (!isset($_GET['action']) || $_GET['action'] !== 'deactivate') {
return;
}

if (empty($_GET['job_id'])) {
    return;
}

$job_id = absint($_GET['job_id']);

// Check Permission
$job = get_post($job_id);
if (!$job || $job->post_author != get_current_user_id()) {
    wp_die(__('You do not have the permission to deactivate', 'wp-job-manager'));
}

// Update Job Status
wp_update_post(array(
    'ID' => $job_id,
    'post_status' => 'expired'
));

// WC Paid Listings cleanup
if (class_exists('WC_Paid_Listings')) {
    delete_post_meta($job_id, '_package_id');
    delete_post_meta($job_id, '_package_subscription_id');
    delete_post_meta($job_id, '_featured');
}

// Clear Cache
clean_post_cache($job_id);

// Redirect to the job dashboard in the current language
$dashboard_url = get_permalink(get_option('job_manager_job_dashboard_page_id'));
if (function_exists('icl_get_current_language')) {
    $current_language = icl_get_current_language();
    $dashboard_url = apply_filters('wpml_permalink', $dashboard_url, $current_language);
}

wp_redirect(add_query_arg('msg', 'deactivated', $dashboard_url));
exit;

}

// Success Message
add_action('job_manager_job_dashboard_before', 'custom_show_deactivation_notice');
function custom_show_deactivation_notice() {
if (isset($GET['msg']) && $GET['msg'] === 'deactivated') {
echo '

' .
esc_html
('Job successfully deactivated.', 'wp-job-manager') .
'
';
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants