Comment obtenir l'ID de la page d'édition actuelle dans l'admin?
-
-
Vouspouvez l'obtenir àpartir de l'URL `wp-admin/post.php? Post=14 & action=edit`You can get it from URL `wp-admin/post.php?post=14&action=edit`
- 0
- 2013-09-14
- Bindiya Patoliya
-
Je vois l'URL comme un dernier coup.J'espère qu'il y a unemanièreplus * élégante * defaire cela.I see the URL as a last shot. I hope there is more *elegant* way to do that.
- 4
- 2013-09-14
- Daniel
-
3 réponses
- votes
-
- 2013-09-14
Vouspouvez également utiliser
$post_id = $_GET['post'];
Ou vouspouvez utiliser un crochet (probablementmieux).
function id_WPSE_114111() { global $post; $id = $post->ID; // do something } add_action( 'admin_notices', 'id_WPSE_114111' );
Vous devrez ajouter un conditionnel car celafonctionnera surtoutes lespages d'administration,je recommande d'utiliser
get_current_screen();
Parexemple,pourne fonctionner que sur pages :
function id_WPSE_114111() { global $my_admin_page; $screen = get_current_screen(); if ( is_admin() && ($screen->id == 'page') ) { global $post; $id = $post->ID; var_dump($id); } } add_action( 'admin_notices', 'id_WPSE_114111' );
You can also use
$post_id = $_GET['post'];
Or you can use a hook (probably better).
function id_WPSE_114111() { global $post; $id = $post->ID; // do something } add_action( 'admin_notices', 'id_WPSE_114111' );
You will need to add a conditional since this will run on all admin pages, I recommend using
get_current_screen();
For example to run only on pages:
function id_WPSE_114111() { global $my_admin_page; $screen = get_current_screen(); if ( is_admin() && ($screen->id == 'page') ) { global $post; $id = $post->ID; var_dump($id); } } add_action( 'admin_notices', 'id_WPSE_114111' );
-
- 2013-09-14
Vouspouvez ajouter ce code dans lefichierfunctions.phpet il vous donnera uneméta-boîte au-dessus de laboîte desparamètres depublication lors de l'édition d'un article ou d'unepage.
<?php function cf_post_id() { global $post; // Get the data $id = $post->ID; // Echo out the field echo '<input type="text" name="_id" value="' . $id . '" class="widefat" disabled />'; } function ve_custom_meta_boxes() { add_meta_box('projects_refid', 'Post ID', 'cf_post_id', 'post', 'side', 'high'); add_meta_box('projects_refid', 'Page ID', 'cf_post_id', 'page', 'side', 'high'); } add_action('add_meta_boxes', 've_custom_meta_boxes'); ?>
You can add this code in functions.php file and it will give you a meta box above the publish settings box when editing a post or page.
<?php function cf_post_id() { global $post; // Get the data $id = $post->ID; // Echo out the field echo '<input type="text" name="_id" value="' . $id . '" class="widefat" disabled />'; } function ve_custom_meta_boxes() { add_meta_box('projects_refid', 'Post ID', 'cf_post_id', 'post', 'side', 'high'); add_meta_box('projects_refid', 'Page ID', 'cf_post_id', 'page', 'side', 'high'); } add_action('add_meta_boxes', 've_custom_meta_boxes'); ?>
-
- 2020-05-14
C'estprobablement lemoyen leplus sûr de vérifier quelest lemessage actuel sur lapage demodification (et d'ajout) dumessage dans wp admin.
function wpse114111_get_current_post_id(): ?WP_Post { global $post; if (empty($post) && array_key_exists('post', $_GET)) { $post = get_post($_GET['post']); } // Optional: get an empty post object from the post_type if (empty($post) && array_key_exists('post_type', $_GET)) { $object = new stdClass(); $object->post_type = $_GET['post_type']; return new WP_Post($object); } if (empty($post)) { return null; } return $post; }
This is probably the "most secure" way of checking what the current post is on the post edit (and add) page in wp admin.
function wpse114111_get_current_post_id(): ?WP_Post { global $post; if (empty($post) && array_key_exists('post', $_GET)) { $post = get_post($_GET['post']); } // Optional: get an empty post object from the post_type if (empty($post) && array_key_exists('post_type', $_GET)) { $object = new stdClass(); $object->post_type = $_GET['post_type']; return new WP_Post($object); } if (empty($post)) { return null; } return $post; }
Laplupart des solutions quej'aitrouvées sont destinées à une utilisationfrontale.Ceciestpour unplugin,donctoute l'activitéest dans leback-end.
Comment obtenir l'ID depage actuellement utilisé (modifié) dans l'administrateur?
Remarque je suis hors de laboucle.J'aijustebesoin d'obtenir l'ID de lapage (pas des articles) queje vois actuellement dans leback-end.