Created
June 26, 2016 21:24
-
-
Save chwnam/3d21cf363857ce2166d0c4225b866306 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Read Only Custom Post | |
* Description: Sample code of read-only custom post | |
* Author: changwoo | |
* Author URI: mailto://ep6tri@hotmail.com | |
*/ | |
add_action( 'init', 'rocp_register_post_type' ); | |
function rocp_register_post_type() { | |
$args = array( | |
'labels' => array( | |
'name' => 'RO Posts', | |
'signular_name' => 'RO Post', | |
'edit_item' => 'RO Post Details', | |
), | |
'capabilities' => array( | |
'create_posts' => 'do_not_allow', | |
// 'delete_posts' => 'do_not_allow', | |
// 'delete_private_posts' => 'do_not_allow', | |
// 'delete_published_posts' => 'do_not_allow', | |
// 'delete_others_posts' => 'do_not_allow', | |
), | |
'map_meta_cap' => TRUE, | |
'public' => TRUE, | |
'show_ui' => TRUE, | |
'supports' => FALSE, | |
'register_meta_box_cb' => 'rocp_customize_meta_boxes', | |
); | |
$rocp = register_post_type( 'rocp', $args ); | |
if ( $rocp->hierarchical ) { | |
add_filter( 'page_row_actions', 'rocp_row_actions', 10, 2 ); | |
} else { | |
add_filter( 'post_row_actions', 'rocp_row_actions', 10, 2 ); | |
} | |
} | |
function rocp_customize_meta_boxes() { | |
// disable slug metabox | |
remove_meta_box( 'slugdiv', 'rocp', 'normal' ); | |
// disable post update metabox | |
remove_meta_box( 'submitdiv', 'rocp', 'side' ); | |
} | |
function rocp_row_actions( $actions, $post ) { | |
if ( $post->post_type == 'rocp' ) { | |
if ( $actions['edit'] ) { | |
$actions['edit'] = sprintf( | |
'<a href="%s" aria-label="%s">%s</a>', | |
get_edit_post_link( $post->ID ), | |
/* translators: %s: post title */ | |
esc_attr( sprintf( __( 'Detail “%s”', 'rocp' ), $post->post_title ) ), | |
__( 'Detail', 'rocp' ) | |
); | |
} | |
if ( $actions['inline hide-if-no-js'] ) { | |
unset( $actions['inline hide-if-no-js'] ); | |
} | |
} | |
return $actions; | |
} | |
add_action( 'edit_form_after_editor', 'rocp_display_detail' ); | |
function rocp_display_detail( $post ) { | |
$rocp_meta_01 = get_post_meta( $post->ID, 'rocp_meta_01', TRUE ); | |
$rocp_meta_02 = get_post_meta( $post->ID, 'rocp_meta_02', TRUE ); | |
echo '<table class="widefat">'; | |
echo '<thead><tr><th>Name</th><th>Value</th></tr></thead>'; | |
echo '<tbody>'; | |
echo '<tr><th>ROCP Meta 01</th><td>' . esc_html( $rocp_meta_01 ) . '</td></tr>'; | |
echo '<tr><th>ROCP Meta 02</th><td>' . esc_html( $rocp_meta_02 ) . '</td></tr>'; | |
echo '</tbody>'; | |
echo '</table>'; | |
echo '<p><a href="' . esc_url( admin_url( 'edit.php?post_type=rocp' ) ) . '"><button type="button" class="button button-primary">Go back</button></a></p>'; | |
} | |
add_filter( 'screen_options_show_screen', 'rocp_disable_screen_option', 10, 2 ); | |
function rocp_disable_screen_option( $show_screen, $wp_screen ) { | |
if ( $wp_screen->id == 'rocp' ) { | |
$show_screen = FALSE; | |
} | |
return $show_screen; | |
} | |
add_filter( 'screen_layout_columns', 'rocp_screen_layout_columns', 10, 2 ); | |
function rocp_screen_layout_columns( $columns, $screen_id ) { | |
if ( $screen_id == 'rocp' ) { | |
$columns['rocp'] = 1; | |
} | |
return $columns; | |
} | |
add_filter( 'get_user_option_screen_layout_rocp', 'rocp_user_option_screen_layout' ); | |
function rocp_user_option_screen_layout( $result ) { | |
return 1; | |
} | |
add_filter( 'bulk_actions-edit-rocp', 'rocp_remove_bulk_edit' ); | |
function rocp_remove_bulk_edit( $actions ) { | |
if ( isset( $actions['edit'] ) ) { | |
unset( $actions['edit'] ); | |
} | |
// if ( isset( $actions['trash'] ) ) { | |
// unset( $actions['trash'] ); | |
// } | |
return $actions; | |
} | |
register_activation_hook( __FILE__, 'rocp_activation' ); | |
function rocp_activation() { | |
rocp_register_post_type(); | |
for ( $i = 1; $i <= 5; ++ $i ) { | |
wp_insert_post( | |
array( | |
'post_title' => sprintf( 'Read Only Custom Post Sample #%02d', $i ), | |
'post_status' => 'publish', | |
'post_type' => 'rocp', | |
'comment_status' => 'closed', | |
'ping_status' => 'closed', | |
'post_name' => sprintf( 'rocp-%02d', $i ), | |
'meta_input' => array( | |
'rocp_meta_01' => "meta_01_$i", | |
'rocp_meta_02' => "meta_02_$i", | |
), | |
) | |
); | |
} | |
flush_rewrite_rules(); | |
} | |
register_deactivation_hook( __FILE__, 'rocp_clear' ); | |
function rocp_clear() { | |
$query = new WP_Query( | |
array( | |
'post_type' => 'rocp', | |
'nopaging' => TRUE, | |
'fields' => 'ids', | |
) | |
); | |
$posts = $query->get_posts(); | |
foreach ( $posts as $post ) { | |
wp_delete_post( $post, TRUE ); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
자세한 설명은 goo.gl/H5qQEM에서.