Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédenteProchaine révision | Révision précédente | ||
| webdev:wordpress:custom_posts [17/05/2017 17:49] – [Ajouter un WYSIWYG] dolo | webdev:wordpress:custom_posts [13/08/2020 00:23] (Version actuelle) – dolo | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | ====== Customs posts, etc ====== | ||
| + | ⇒ **Ces infos sont basées sur le plugin [[https:// | ||
| + | |||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | * [[https:// | ||
| + | |||
| + | * https:// | ||
| + | |||
| + | * [[https:// | ||
| + | |||
| + | ==== Récupérer la valeur d'un champ meta : ==== | ||
| + | [[https:// | ||
| + | |||
| + | ou | ||
| + | |||
| + | $post-> | ||
| + | ==== Ajouter le meta box ==== | ||
| + | < | ||
| + | public function addMetaBox() | ||
| + | { | ||
| + | add_meta_box( | ||
| + | ' | ||
| + | 'En bref', | ||
| + | array($this, | ||
| + | null, | ||
| + | ' | ||
| + | ' | ||
| + | ); | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== Enlever le main content field ==== | ||
| + | [[https:// | ||
| + | |||
| + | < | ||
| + | $args = array( | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | //' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ); | ||
| + | register_post_type( ' | ||
| + | </ | ||
| + | |||
| + | ==== Ajouter une custom taxonomy ==== | ||
| + | Classe plugin : | ||
| + | < | ||
| + | public function custom_taxonomy() | ||
| + | { | ||
| + | $labels = array( | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ); | ||
| + | $args = array( | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ); | ||
| + | register_taxonomy( ' | ||
| + | } | ||
| + | |||
| + | public function hook() | ||
| + | { | ||
| + | parent:: | ||
| + | add_action( ' | ||
| + | } | ||
| + | </ | ||
| + | Pour appeller cette taxonomy : [[https:// | ||
| + | |||
| + | ==== Ajouter un WYSIWYG ==== | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <? | ||
| + | htmlspecialchars_decode( $this-> | ||
| + | " | ||
| + | array( | ||
| + | ' | ||
| + | ' | ||
| + | ); | ||
| + | ?> | ||
| + | </ | ||
| + | </ | ||
| + | Sauvegarde du champ : | ||
| + | < | ||
| + | public function saveMetaData( $post_id ) | ||
| + | { | ||
| + | // ... | ||
| + | if ( isset( $_POST[' | ||
| + | { | ||
| + | update_post_meta( $post_id, ' | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | Afficher correctement le champ : | ||
| + | < | ||
| + | echo htmlspecialchars_decode($post-> | ||
| + | </ | ||
| + | |||
| + | ==== Ajouter un champs texte classique ==== | ||
| + | < | ||
| + | <p> | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | if( isset( $_POST[' | ||
| + | { | ||
| + | update_post_meta( $post_id, ' | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | if( !empty($post-> | ||
| + | { | ||
| + | echo '< | ||
| + | } | ||
| + | </ | ||
| + | ==== Ajouter un champs image ==== | ||
| + | === Image unique === | ||
| + | Code du champ simple : | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <? | ||
| + | if( !empty($this-> | ||
| + | { | ||
| + | $img = wp_get_attachment_image_src( $this-> | ||
| + | echo '< | ||
| + | } ?> | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | </ | ||
| + | Sauvegarde du champ : | ||
| + | < | ||
| + | public function saveMetaData( $post_id ) | ||
| + | { | ||
| + | // ... | ||
| + | if ( isset( $_POST[' | ||
| + | { | ||
| + | update_post_meta( $post_id, ' | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Afficher ce champ : | ||
| + | < | ||
| + | $img = wp_get_attachment_image_src( get_post_meta( get_the_ID(), | ||
| + | echo '< | ||
| + | </ | ||
| + | === Multi images === | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | < | ||
| + | <p class=" | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | |||
| + | <? | ||
| + | $serialized_images = $this-> | ||
| + | $images = unserialize($serialized_images); | ||
| + | if(!empty( $images ) ) | ||
| + | { | ||
| + | foreach ($images as $key => $image) { | ||
| + | ?> | ||
| + | <p class=" | ||
| + | < | ||
| + | < | ||
| + | </ | ||
| + | <? | ||
| + | } | ||
| + | } | ||
| + | echo '</ | ||
| + | </ | ||
| + | Sauvegarde : | ||
| + | < | ||
| + | public function saveMetaData( $post_id ) | ||
| + | { | ||
| + | // ... | ||
| + | if( isset( $_POST[' | ||
| + | { | ||
| + | foreach ($_POST[' | ||
| + | if($image == '' | ||
| + | unset($_POST[' | ||
| + | } | ||
| + | } | ||
| + | $_POST[' | ||
| + | update_post_meta( $post_id, ' | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | Afficher ce code : | ||
| + | < | ||
| + | $images = unserialize(get_post_meta( get_the_ID(), | ||
| + | if(!empty( $images )) | ||
| + | { | ||
| + | echo ' style=" | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | --------- | ||
| + | JS à ajouter à ce code : | ||
| + | < | ||
| + | jQuery(function ($) { | ||
| + | //ADD MEDIA UPLOADER/ | ||
| + | function upload_image(e, | ||
| + | { | ||
| + | // e.preventDefault(); | ||
| + | var image = wp.media({ | ||
| + | title: ' | ||
| + | // mutiple: true if you want to upload multiple files at once | ||
| + | multiple: | ||
| + | }).open() | ||
| + | .on(' | ||
| + | // This will return the selected image from the Media Uploader, the result is an object | ||
| + | var uploaded_image = image.state().get(' | ||
| + | // We convert uploaded_image to a JSON object to make accessing it easier | ||
| + | // Output to the console uploaded_image | ||
| + | var image_url = uploaded_image.toJSON().url; | ||
| + | |||
| + | // Let's assign the url value to the input field | ||
| + | if( text_input !== null ) | ||
| + | { | ||
| + | $(text_input).val(image_url); | ||
| + | } | ||
| + | |||
| + | if( id_input !== null ) | ||
| + | { | ||
| + | $(id_input).val( uploaded_image.toJSON().id ); | ||
| + | } | ||
| + | // window.aa_name_img_input[$(text_input).parent().index(' | ||
| + | // console.log(window.aa_name_img_input); | ||
| + | }); | ||
| + | }; | ||
| + | |||
| + | function delete_image(e, | ||
| + | del.parent().children(' | ||
| + | del.parent().slideToggle(); | ||
| + | } | ||
| + | |||
| + | // protect user role ie fix :poop: | ||
| + | $(' | ||
| + | $(' | ||
| + | |||
| + | p = '<p class=" | ||
| + | // window.aa_name_img_input = []; | ||
| + | $(' | ||
| + | var model = $(' | ||
| + | $(' | ||
| + | { | ||
| + | e.preventDefault(); | ||
| + | $(this).parent().append(p); | ||
| + | $(this).parent().children(' | ||
| + | var upload = $(this).parent().children(' | ||
| + | var text_input = upload.prev(' | ||
| + | var del = $(this).parent().children(' | ||
| + | upload.on(' | ||
| + | upload_image(e, | ||
| + | }); | ||
| + | del.on(' | ||
| + | delete_image(e, | ||
| + | }); | ||
| + | |||
| + | }); | ||
| + | |||
| + | $(' | ||
| + | { | ||
| + | delete_image(e, | ||
| + | }); | ||
| + | |||
| + | $(' | ||
| + | { | ||
| + | text_input = $(this).prev(' | ||
| + | upload_image(e, | ||
| + | }); | ||
| + | |||
| + | $(' | ||
| + | { | ||
| + | text_input = $(this).prev(' | ||
| + | upload_image(e, | ||
| + | }); | ||
| + | }); | ||
| + | |||
| + | </ | ||
| + | |||
| + | ==== Afficher les champs customs sous conditions ==== | ||
| + | === Limiter à la page d' | ||
| + | On peut mettre les conditions directement dans le hook **add_meta_boxes** si on fait remonter ses paramètres : | ||
| + | < | ||
| + | add_action( ' | ||
| + | |||
| + | public function addMetaBox($post_type, | ||
| + | { | ||
| + | // On affiche la meta box que si la page est en homepage | ||
| + | if($post-> | ||
| + | { | ||
| + | add_meta_box( | ||
| + | ' | ||
| + | 'En bref', | ||
| + | array($this, | ||
| + | ' | ||
| + | ' | ||
| + | ' | ||
| + | ); | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Afficher seulement pour un champ en particulier : | ||
| + | < | ||
| + | public function champHtml( $post ) | ||
| + | { | ||
| + | // On affiche ces champs que si la page est en homepage | ||
| + | if($post-> | ||
| + | { | ||
| + | // ... | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Afficher suivant le modèle de page : | ||
| + | < | ||
| + | <?php | ||
| + | if( get_page_template_slug( $post-> | ||
| + | { | ||
| + | ?> | ||
| + | < | ||
| + | < | ||
| + | <? | ||
| + | htmlspecialchars_decode( $this-> | ||
| + | " | ||
| + | array( | ||
| + | ' | ||
| + | ' | ||
| + | ); | ||
| + | ?> | ||
| + | </ | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | ==== Remove the slug from the custom post ==== | ||
| + | [[https:// | ||
| + | |||
| + | <Code linenums> | ||
| + | public function parse_request( $query ) | ||
| + | { | ||
| + | if ( ! $query-> | ||
| + | { | ||
| + | return; | ||
| + | } | ||
| + | |||
| + | if ( ! empty( $query-> | ||
| + | { | ||
| + | $query-> | ||
| + | } | ||
| + | } | ||
| + | |||
| + | public function hook() | ||
| + | { | ||
| + | parent:: | ||
| + | add_action( ' | ||
| + | } | ||
| + | |||
| + | |||
| + | //////////////////////////////////////////////////////////// | ||
| + | |||
| + | |||
| + | register_post_type( ' | ||
| + | //other custom posttype definitions | ||
| + | ' | ||
| + | ' | ||
| + | // ' | ||
| + | ), | ||
| + | ) ); | ||
| + | </ | ||
| + | |||
| + | __**Then re-register the permalink option to make it effective**__. | ||
| + | |||
| + | |||
| + | [[https:// | ||