{"id":1279,"date":"2024-01-25T17:02:18","date_gmt":"2024-01-25T16:02:18","guid":{"rendered":"https:\/\/blaminhor.com\/p\/wooordpress\/?p=1279"},"modified":"2024-01-25T19:02:21","modified_gmt":"2024-01-25T18:02:21","slug":"executer-du-php-si-statut-administrateur-wordpress","status":"publish","type":"post","link":"https:\/\/blaminhor.com\/p\/wooordpress\/fr\/executer-du-php-si-statut-administrateur-wordpress\/","title":{"rendered":"Ex\u00e9cuter du PHP seulement si l\u2019utilisateur est administrateur WordPress"},"content":{"rendered":"<div class=\"fusion-fullwidth fullwidth-box fusion-builder-row-1 fusion-flex-container has-pattern-background has-mask-background nonhundred-percent-fullwidth non-hundred-percent-height-scrolling\" style=\"--awb-border-radius-top-left:0px;--awb-border-radius-top-right:0px;--awb-border-radius-bottom-right:0px;--awb-border-radius-bottom-left:0px;--awb-flex-wrap:wrap;\" ><div class=\"fusion-builder-row fusion-row fusion-flex-align-items-flex-start fusion-flex-content-wrap\" style=\"max-width:1248px;margin-left: calc(-4% \/ 2 );margin-right: calc(-4% \/ 2 );\"><div class=\"fusion-layout-column fusion_builder_column fusion-builder-column-0 fusion_builder_column_1_1 1_1 fusion-flex-column\" style=\"--awb-bg-size:cover;--awb-width-large:100%;--awb-margin-top-large:0px;--awb-spacing-right-large:1.92%;--awb-margin-bottom-large:20px;--awb-spacing-left-large:1.92%;--awb-width-medium:100%;--awb-order-medium:0;--awb-spacing-right-medium:1.92%;--awb-spacing-left-medium:1.92%;--awb-width-small:100%;--awb-order-small:0;--awb-spacing-right-small:1.92%;--awb-spacing-left-small:1.92%;\"><div class=\"fusion-column-wrapper fusion-column-has-shadow fusion-flex-justify-content-flex-start fusion-content-layout-column\"><div class=\"fusion-text fusion-text-1\"><p><strong>Travailler sur un code PHP qui ne peut \u00eatre visible que pour l&rsquo;administrateur du site WordPress, qui est \u00e9galement le d\u00e9veloppeur, permet notamment de continuer \u00e0 travailler sur un site en production sans que les autres internautes ne puissent le voir. <\/strong>Le temps par exemple de bien v\u00e9rifier que tout fonctionne bien avant la <span style=\"text-decoration: underline;\">mise en production<\/span>, sans pour autant devoir d\u00e9velopper le code sur une <span style=\"text-decoration: underline;\">version de pr\u00e9production<\/span> en amont, ce qui n\u00e9cessiterait plus de manipulations.<\/p>\n<\/div><\/div><\/div><div class=\"fusion-layout-column fusion_builder_column fusion-builder-column-1 fusion_builder_column_1_1 1_1 fusion-flex-column block-flag block-flag-important\" style=\"--awb-bg-size:cover;--awb-width-large:100%;--awb-margin-top-large:0px;--awb-spacing-right-large:1.92%;--awb-margin-bottom-large:20px;--awb-spacing-left-large:1.92%;--awb-width-medium:100%;--awb-order-medium:0;--awb-spacing-right-medium:1.92%;--awb-spacing-left-medium:1.92%;--awb-width-small:100%;--awb-order-small:0;--awb-spacing-right-small:1.92%;--awb-spacing-left-small:1.92%;\" data-scroll-devices=\"small-visibility,medium-visibility,large-visibility\"><div class=\"fusion-column-wrapper fusion-column-has-shadow fusion-flex-justify-content-flex-start fusion-content-layout-column\"><div class=\"fusion-text fusion-text-2\"><p>Dans la majorit\u00e9 des cas, il est d\u00e9conseill\u00e9 de travailler en PHP sur un site <span style=\"text-decoration: underline;\">en production<\/span>, car m\u00eame si votre code ne sera ex\u00e9cut\u00e9 que si l&rsquo;utilisateur courant est administrateur (donc vous), une erreur de syntaxe dans votre PHP cr\u00e9era une erreur 500, et plantera donc quand m\u00eame le site\u00a0le temps que vous r\u00e9pariez votre erreur. C&rsquo;est n\u00e9anmoins une technique employ\u00e9e pour de petits ajustements sur des sites peu visit\u00e9s, ou du d\u00e9veloppement PHP dont l&rsquo;erreur de codage est peu probable.<\/p>\n<\/div><\/div><\/div><div class=\"fusion-layout-column fusion_builder_column fusion-builder-column-2 fusion_builder_column_1_1 1_1 fusion-flex-column block-flag block-flag-how\" style=\"--awb-bg-size:cover;--awb-width-large:100%;--awb-margin-top-large:0px;--awb-spacing-right-large:1.92%;--awb-margin-bottom-large:20px;--awb-spacing-left-large:1.92%;--awb-width-medium:100%;--awb-order-medium:0;--awb-spacing-right-medium:1.92%;--awb-spacing-left-medium:1.92%;--awb-width-small:100%;--awb-order-small:0;--awb-spacing-right-small:1.92%;--awb-spacing-left-small:1.92%;\" data-scroll-devices=\"small-visibility,medium-visibility,large-visibility\"><div class=\"fusion-column-wrapper fusion-column-has-shadow fusion-flex-justify-content-flex-start fusion-content-layout-column\"><div class=\"fusion-text fusion-text-3\"><p>Voici la simple fonction qui permet de v\u00e9rifier si l&rsquo;internaute qui charge la page courante a le statut WordPress d&rsquo;administrateur.<br \/>\nVous pouvez inclure cette fonction dans le <span style=\"text-decoration: underline;\">fichier functions.php situ\u00e9 \u00e0 la racine de votre th\u00e8me actif<\/span> pour que la fonction soit pr\u00e9sente partout sur votre site.<\/p>\n<\/div><style type=\"text\/css\" scopped=\"scopped\">.fusion-syntax-highlighter-1 > .CodeMirror, .fusion-syntax-highlighter-1 > .CodeMirror .CodeMirror-gutters {background-color:var(--awb-color1);}.fusion-syntax-highlighter-1 > .CodeMirror .CodeMirror-gutters { background-color: var(--awb-color2); }.fusion-syntax-highlighter-1 > .CodeMirror .CodeMirror-linenumber { color: var(--awb-color8); }<\/style><div class=\"fusion-syntax-highlighter-container fusion-syntax-highlighter-1 fusion-syntax-highlighter-theme-light\" style=\"opacity:0;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:14px;border-width:1px;border-style:solid;border-color:var(--awb-color3);\"><div class=\"syntax-highlighter-copy-code\"><span class=\"syntax-highlighter-copy-code-title\" data-id=\"fusion_syntax_highlighter_1\" style=\"font-size:14px;\">Copie dans le presse-papiers<\/span><\/div><label for=\"fusion_syntax_highlighter_1\" class=\"screen-reader-text\">Surligneur syntaxique<\/label><textarea class=\"fusion-syntax-highlighter-textarea\" id=\"fusion_syntax_highlighter_1\" data-readOnly=\"nocursor\" data-lineNumbers=\"1\" data-lineWrapping=\"\" data-theme=\"default\" data-mode=\"text\/x-php\">\/**\n\t* V\u00e9rifie si le r\u00f4le actuel est 'Administrateur'\n\t*\n\t* @return boolean\n**\/\nfunction is_adm()\n{\n\treturn current_user_can('manage_options');\n}<\/textarea><\/div><\/div><\/div><div class=\"fusion-layout-column fusion_builder_column fusion-builder-column-3 fusion_builder_column_1_1 1_1 fusion-flex-column block-flag block-flag-didyouknow\" style=\"--awb-bg-size:cover;--awb-width-large:100%;--awb-margin-top-large:0px;--awb-spacing-right-large:1.92%;--awb-margin-bottom-large:20px;--awb-spacing-left-large:1.92%;--awb-width-medium:100%;--awb-order-medium:0;--awb-spacing-right-medium:1.92%;--awb-spacing-left-medium:1.92%;--awb-width-small:100%;--awb-order-small:0;--awb-spacing-right-small:1.92%;--awb-spacing-left-small:1.92%;\" data-scroll-devices=\"small-visibility,medium-visibility,large-visibility\"><div class=\"fusion-column-wrapper fusion-column-has-shadow fusion-flex-justify-content-flex-start fusion-content-layout-column\"><div class=\"fusion-text fusion-text-4\"><p>Une fonction <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/is_admin\/\" target=\"_blank\" rel=\"noopener\">is_admin() existe au sein de WordPress<\/a>, mais ne v\u00e9rifie que si l&rsquo;on se trouve dans un environnement d&rsquo;administration (le tableau de bord par exemple), ce qui n&rsquo;est pas du tout ce que l&rsquo;on cherche. C&rsquo;est pour cette raison que nous cr\u00e9ons ici une fonction PHP avec un nom diff\u00e9rent de ce qu&rsquo;on aurait choisi intuitivement, <em>is_admin<\/em> \u00e9tant d\u00e9j\u00e0 pris.<\/p>\n<\/div><\/div><\/div><div class=\"fusion-layout-column fusion_builder_column fusion-builder-column-4 fusion_builder_column_1_1 1_1 fusion-flex-column block-flag block-flag-demo\" style=\"--awb-bg-size:cover;--awb-width-large:100%;--awb-margin-top-large:0px;--awb-spacing-right-large:1.92%;--awb-margin-bottom-large:20px;--awb-spacing-left-large:1.92%;--awb-width-medium:100%;--awb-order-medium:0;--awb-spacing-right-medium:1.92%;--awb-spacing-left-medium:1.92%;--awb-width-small:100%;--awb-order-small:0;--awb-spacing-right-small:1.92%;--awb-spacing-left-small:1.92%;\" data-scroll-devices=\"small-visibility,medium-visibility,large-visibility\"><div class=\"fusion-column-wrapper fusion-column-has-shadow fusion-flex-justify-content-flex-start fusion-content-layout-column\"><div class=\"fusion-text fusion-text-5\"><p>Vous pouvez ainsi utiliser la fonction \u00e0 l&rsquo;endroit o\u00f9 vous voulez cr\u00e9er votre code PHP \u00ab\u00a0cach\u00e9\u00a0\u00bb.<\/p>\n<\/div><style type=\"text\/css\" scopped=\"scopped\">.fusion-syntax-highlighter-2 > .CodeMirror, .fusion-syntax-highlighter-2 > .CodeMirror .CodeMirror-gutters {background-color:var(--awb-color1);}.fusion-syntax-highlighter-2 > .CodeMirror .CodeMirror-gutters { background-color: var(--awb-color2); }.fusion-syntax-highlighter-2 > .CodeMirror .CodeMirror-linenumber { color: var(--awb-color8); }<\/style><div class=\"fusion-syntax-highlighter-container fusion-syntax-highlighter-2 fusion-syntax-highlighter-theme-light\" style=\"opacity:0;margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;font-size:14px;border-width:1px;border-style:solid;border-color:var(--awb-color3);\"><div class=\"syntax-highlighter-copy-code\"><span class=\"syntax-highlighter-copy-code-title\" data-id=\"fusion_syntax_highlighter_2\" style=\"font-size:14px;\">Copie dans le presse-papiers<\/span><\/div><label for=\"fusion_syntax_highlighter_2\" class=\"screen-reader-text\">Surligneur syntaxique<\/label><textarea class=\"fusion-syntax-highlighter-textarea\" id=\"fusion_syntax_highlighter_2\" data-readOnly=\"nocursor\" data-lineNumbers=\"1\" data-lineWrapping=\"\" data-theme=\"default\" data-mode=\"text\/x-php\">if( is_adm() ) {\n  \n  \/**\n  Ici, le code ne sera ex\u00e9cut\u00e9 que pour l'administrateur\n  **\/\n  \n}<\/textarea><\/div><\/div><\/div><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Utilisez cette fonction PHP sur WordPress pour que votre code ne soit visible que par vous (l&rsquo;administrateur du site).<\/p>\n","protected":false},"author":1,"featured_media":1287,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_bbp_topic_count":0,"_bbp_reply_count":0,"_bbp_total_topic_count":0,"_bbp_total_reply_count":0,"_bbp_voice_count":0,"_bbp_anonymous_reply_count":0,"_bbp_topic_count_hidden":0,"_bbp_reply_count_hidden":0,"_bbp_forum_subforum_count":0,"footnotes":""},"categories":[8],"tags":[],"class_list":["post-1279","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress"],"_links":{"self":[{"href":"https:\/\/blaminhor.com\/p\/wooordpress\/wp-json\/wp\/v2\/posts\/1279","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blaminhor.com\/p\/wooordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blaminhor.com\/p\/wooordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blaminhor.com\/p\/wooordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blaminhor.com\/p\/wooordpress\/wp-json\/wp\/v2\/comments?post=1279"}],"version-history":[{"count":0,"href":"https:\/\/blaminhor.com\/p\/wooordpress\/wp-json\/wp\/v2\/posts\/1279\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blaminhor.com\/p\/wooordpress\/wp-json\/wp\/v2\/media\/1287"}],"wp:attachment":[{"href":"https:\/\/blaminhor.com\/p\/wooordpress\/wp-json\/wp\/v2\/media?parent=1279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blaminhor.com\/p\/wooordpress\/wp-json\/wp\/v2\/categories?post=1279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blaminhor.com\/p\/wooordpress\/wp-json\/wp\/v2\/tags?post=1279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}