Skip to content

Instantly share code, notes, and snippets.

@todiadiyatmo
Last active July 8, 2019 22:59
Show Gist options
  • Save todiadiyatmo/cbcc5c302efdd5eda2d4 to your computer and use it in GitHub Desktop.
Save todiadiyatmo/cbcc5c302efdd5eda2d4 to your computer and use it in GitHub Desktop.
WP Custom Post Type and Custom Capability Role
<?php
// Init new Role and add all capability to editor and admin
function custom_post_type_capability_admin()
{
// List the role that we want to give a full access
$roles = array('editor','administrator');
// List of the post type that will have custom capability
$postTypes = array('job','resume');
// Iterate all roles
foreach ($roles as $role) {
foreach ($postTypes as $postType) {
$addRole = get_role($role);
$addRole->add_cap("read" );
$addRole->add_cap("read_{$postType}" );
$addRole->add_cap("read_private_{$postType}s");
$addRole->add_cap("edit_{$postType}s");
$addRole->add_cap("edit_private_{$postType}s");
$addRole->add_cap("edit_published_{$postType}s");
$addRole->add_cap("edit_others_{$postType}s");
$addRole->add_cap("publish_{$postType}s");
$addRole->add_cap("delete_{$postType}s");
$addRole->add_cap("delete_private_{$postType}s");
$addRole->add_cap("delete_published_{$postType}s");
$addRole->add_cap("delete_others_{$postType}s");
}
}
}
add_action('after_setup_theme', 'custom_post_type_capability_admin');
function custom_post_type_capability_non_admin()
{
add_role( 'perusahaan', 'Perusahaan');
$perusahaan = get_role( 'perusahaan' );
$perusahaanCaps = array(
"read",
"read_job",
"read_private_jobs",
"edit_jobs",
"edit_published_jobs",
// "publish_jobs",
"delete_jobs",
"delete_published_jobs",
"upload_files",
"edit_files",
"assign_job_type",
"assign_job_city",
"assign_skills"
);
foreach ($perusahaanCaps as $cap) {
$perusahaan->add_cap($cap);
}
add_role( 'pelamar', 'Pelamar');
$pelamar = get_role( 'pelamar' );
$pelamarCaps = array(
"read",
"read_resume",
"read_private_resumes",
"edit_resumes",
"edit_published_resumes",
// "publish_resumes",
"delete_resumes",
"delete_published_resumes",
"upload_files",
"edit_files",
);
foreach ($pelamarCaps as $cap) {
$pelamar->add_cap($cap);
}
}
add_action('after_setup_theme', 'custom_post_type_capability_non_admin');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment