Adding submenu

/*Adding submenu to custom post; Here the purpose is to reorder the jobs*/
<?php
function rejauldu_add_submenu_page() {
 add_submenu_page(
 'edit.php?post_type=rejauldu',
 'Reorder Jobs',
 'Reorder Jobs',
 'manage_options',
 'reorder_jobs',
 'reorder_admin_jobs_callback'
 );
}

add_action('admin_menu', 'rejauldu_add_submenu_page');

function reorder_admin_jobs_callback() {
 $args = array(
 'post_type' => 'rejauldu',
 'orderby' => 'menu_order',
 'order' => 'ASC',
 'post_status' => 'publish',
 'no_found_rows' => true,
 'update_post_term_cache' => false,
 'post_per_post' => 50
 //'category_name' => 'cat1'
 );
 $job_listing = new WP_Query($args);
?>
 <div id="job-sort" class="wrap">
 <div id="icon-job-admin" class="icon32"></div>
 <h2><?php _e('Sort Job Positions', 'rejauldu');?> <img src="<?php echo esc_url(admin_url().'/images/loading.gif');?>" id="loading-animation" /></h2>
 <?php if($job_listing->have_posts()): ?>
 <p><?php _e('<strong>Note:</strong> this only affects the jobs listed using the shortcode functions', 'rejauldu');?></p>
 <ul id="custom-type-list">
 <?php while($job_listing->have_posts()):$job_listing->the_post(); ?>
 <li id="<?php the_id(); ?>"><?php the_title(); ?></li>
 <?php endwhile; ?>
 </ul>
 <?php else: ?>
 <p><?php _e('You have no jobs to sort', 'rejauldu');?></p>
 <?php endif; ?>
 </div>
 
<?php
}
function rejauldu_save_reorder() {
 if(!check_ajax_referer('wp-job-order', 'security')) { /* wp-job-order name was given in wp_localize_script function's nonce field, security is the parameter in js file*/
 return wp_send_json_error('Invalid Nonce.');
 }
 if(!current_user_can('manage_options')) {
 return wp_send_json_error('You are not allowed to do this.');
 }
 $order = $_POST['order'];
 $counter = 0;
 foreach($order as $post_id) {
 $post = array(
 'ID' => (int)$post_id,
 'menu_order' => $counter
 );
 wp_update_post($post);
 $counter ++;
 }
 return wp_send_json_success('Post saved.');
}
add_action('wp_ajax_save_sort', 'rejauldu_save_reorder');

Labels: , ,

© copyright-2020 Rejaul