Created
October 29, 2020 15:45
-
-
Save chandrapatel/c276e7960d3335bd57535b7799aab32f to your computer and use it in GitHub Desktop.
Post Meta Viewer
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: Post Meta Viewer | |
* Description: Allows to view the post meta. | |
*/ | |
/** | |
* Add Post Meta Viewer submenu under Tools menu. | |
* | |
* @return void | |
*/ | |
function pmv_admin_init() { | |
add_submenu_page( 'options-general.php', 'Post Meta Viewer', 'Post Meta Viewer', 'manage_options', 'post-meta-viewer', 'pmv_page' ); | |
add_filter( 'page_row_actions', 'pmv_row_action', 10, 2 ); | |
add_filter( 'post_row_actions', 'pmv_row_action', 10, 2 ); | |
} | |
add_action( 'admin_menu', 'pmv_admin_init' ); | |
/** | |
* Add "View Meta" row action. | |
* | |
* @param array $actions An array of row action links. | |
* @param WP_Post $post The post object. | |
* | |
* @return array | |
*/ | |
function pmv_row_action( $actions, $post ) { | |
$actions['pmv'] = sprintf( '<a href="%s">View Meta</a>', esc_url( add_query_arg( 'post_id', $post->ID, menu_page_url( 'post-meta-viewer', false ) ) ) ); | |
return $actions; | |
} | |
/** | |
* Display the post meta of selected post ID. | |
* | |
* @return void | |
*/ | |
function pmv_page() { | |
$post_id = ''; | |
$post_meta = []; | |
if ( ! empty( $_REQUEST['post_id'] ) && is_numeric( $_REQUEST['post_id'] ) ) { | |
$post_id = $_REQUEST['post_id']; | |
$post_meta = get_post_meta( $_REQUEST['post_id'] ); | |
} | |
?> | |
<style> | |
.pmv pre { | |
margin-top: 8px; | |
} | |
</style> | |
<div class="wrap pmv"> | |
<h1><?php echo esc_html( get_admin_page_title() ); ?></h1> | |
<form action="" method="post"> | |
<table class="form-table" role="presentation"> | |
<tbody> | |
<th scope="row"><label for="pmv_post_id">Enter Post ID</label></th> | |
<td><input type="text" id="pmv_post_id" class="medium-text" name="post_id" required value="<?php echo esc_attr( $post_id ); ?>" /></td> | |
</tbody> | |
</table> | |
<p class="submit"> | |
<input type="submit" class="button button-primary" value="View" /> | |
</p> | |
</form> | |
<?php | |
if ( ! empty( $post_meta ) && is_array( $post_meta ) ) { | |
echo '<table class="form-table" role="presentation"><tbody>'; | |
foreach ( $post_meta as $meta_key => $meta_value ) { | |
$meta_value = is_array( $meta_value ) ? $meta_value [0] : $meta_value; | |
// Check if meta value is in JSON format. | |
if ( ! empty( json_decode( $meta_value ) ) ) { | |
$meta_value = json_decode( $meta_value, true ); | |
} else { | |
$meta_value = maybe_unserialize( $meta_value ); | |
} | |
echo '<tr>'; | |
printf( '<th scope="row"><label>%s</label></th>', esc_html( $meta_key ) ); | |
if ( is_array( $meta_value ) ) { | |
printf( '<td><pre>%s</pre></td>', esc_html( print_r( $meta_value, true ) ) ); | |
} else { | |
printf( '<td><pre>%s</pre></td>', esc_html( print_r( $meta_value, true ) ) ); | |
} | |
echo '</tr>'; | |
} | |
echo '</tbody></table>'; | |
} elseif ( ! empty( $post_id ) ) { | |
echo '<p>No post meta found.</p>'; | |
} | |
?> | |
</div> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment