Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
programmieren:wordpress:klassische_metabox_fuer_posts_erstellen [2023/06/14 19:24] – created jgehrkeprogrammieren:wordpress:klassische_metabox_fuer_posts_erstellen [2024/08/10 10:38] (current) jgehrke
Line 4: Line 4:
  
 <code php> <code php>
-/* +<?php 
- METABOX EINKLINGEN +/*  Metabox init */ 
- */ +new MetaboxPost();
-new MetaboxPostMarketo();+
  
 /** /**
- SEITEN EIGENSCHAFTEN+ METABOX KLASSE
  */  */
-class MetaboxPostMarketo+class MetaboxPost
 { {
  
- function __construct( $plugin_url, $plugin_path )+ function __construct( )
  {  {
- 
- $this->plugin_url  = $plugin_url; 
- $this->plugin_path = $plugin_path; 
- 
  add_action( 'add_meta_boxes', [ $this, 'add_meta_box' ] );  add_action( 'add_meta_boxes', [ $this, 'add_meta_box' ] );
  add_action( 'save_post', [ $this, 'save' ] );  add_action( 'save_post', [ $this, 'save' ] );
Line 27: Line 22:
  public function add_metabox()  public function add_metabox()
  {  {
- $mb_id            = 'moewe_mb_post_marketo'; + $mb_id            = 'plugin_metbox_name'; 
- $mb_title         = 'Marketo Verbindung';+ $mb_title         = 'Meine Metabox';
  $mb_callback      = [ $this, 'render_metabox' ];  $mb_callback      = [ $this, 'render_metabox' ];
  $mb_screens       = ['post','event'];  $mb_screens       = ['post','event'];
Line 37: Line 32:
  }  }
  
- public function register_scripts_styles(){ 
- // @see setup/admin_css_js.php 
- } 
  
  public function render_metabox()  public function render_metabox()
  {  {
- // @see setup/admin_css_js.php + echo "<p>HTML Ausgabe der Metabox</p>";
- //wp_enqueue_script( 'alpine-js' ); +
- echo "<p>Test</p>";+
  }  }
  
Line 60: Line 50:
  // Update von einem Metafeld  // Update von einem Metafeld
  //update_post_meta( $post_id, '_my_meta_value_key', $mydata );  //update_post_meta( $post_id, '_my_meta_value_key', $mydata );
 + }
 +}
 +</code>
 +
 +===== Alternative als statische Klasse =====
 +
 +Das hier ist eine alternative Version mit einer Klasse aus Static-Methods. Der Vorteil ist, dass diese nicht initialisiert werden muss, und wirklich nur aufgerufen wird, wenn Metaboxes von Wordpress gerendert werden. 
 +
 +<code php>
 +<?php 
 +
 +namespace wp_admin\wp_metabox;
 +
 +add_action( 'add_meta_boxes', ['wp_admin\wp_metabox\MyMetaBox', 'add_metabox'] );
 +add_action( 'save_post', ['wp_admin\wp_metabox\MyMetaBox', 'save'] );
 +
 +final class MyMetaBox
 +{
 + private static $ID              = 'my_metabox_id';
 + private static $title           = 'My Metabox Title';
 + private static $render_callback = ['wp_admin\wp_metabox\MyMetaBox', 'render_metabox'];
 + private static $show_on_screens = [ 'post' ];
 + private static $position        = 'side';
 + private static $priority        = 'default';
 +
 + public static function add_metabox()
 + {
 + add_meta_box( self::$ID, self::$title, self::$render_callback, self::$show_on_screens, self::$position, self::$priority );
 + }
 +
 + public static function render_metabox()
 + {
 + echo "<h1>Mein lustiges HTML</h1>";
 + }
 +
 + public static function save()
 + {
 + // Nichts machen bei Auto-Save
 + if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
 + return $post_id;
 + }
 + 
 + // Sanitizen von Eingaben
 + //$mydata = sanitize_text_field( $_POST['myplugin_new_field'] );
 + 
 + // Update von einem Metafeld
 + //update_post_meta( $post_id, '_my_meta_value_key', $mydata );
 + }
 +}
 +
 +</code>
 +
 +===== Erweitertes Beispiel mit statischer Klasse =====
 +
 +In diesem Beispiel ist alles etwas konkreter. Es zeigt wie Daten aus einer Textarea gespeichert und geladen werden. 
 +
 +Mit dieser Variante, kann man auf die gespeicherten Daten der Metabox auch direkt über die statische Methode zurück greifen, via: 
 +''$metabox_value_description = AdditionalPostInfos::get_description()''
 +
 +<code php>
 +<?php 
 + 
 + namespace theme\functions\theme_setup\metaboxes;
 + 
 +add_action( 'add_meta_boxes', ['theme\functions\theme_setup\metaboxes\AdditionalPostInfos', 'add_metabox'] );
 +add_action( 'save_post', ['theme\functions\theme_setup\metaboxes\AdditionalPostInfos', 'save'] );
 + 
 +final class AdditionalPostInfos
 +{
 + private static $ID              = 'mb_additional_post_infos';
 + private static $title           = 'Additional Post Infos';
 + private static $render_callback = ['theme\functions\theme_setup\metaboxes\AdditionalPostInfos', 'render_metabox'];
 + private static $show_on_screens = [ 'post' ];
 + private static $position        = 'side';
 + private static $priority        = 'high';
 + 
 + private static $entry_meta_key  = 'additional_post_infos';
 +
 + public static function add_metabox()
 + {
 + add_meta_box( self::$ID, self::$title, self::$render_callback, self::$show_on_screens, self::$position, self::$priority );
 + }
 + 
 + public static function render_metabox()
 + {
 + $value_description = self::get_description();
 +
 + echo <<<HTML
 + <div class="mb_additional_post_infos">
 + <label>
 + <strong>Descriptive Copy</strong>
 + <textarea type="text" name="additional_post_infos[description]" placeholder="add text here..." rows="3">{$value_description}</textarea>
 + </label>
 + <p><em>This will be displayed above the title on the blog post page.</em></p>
 + </div>
 +
 + <style>
 + .mb_additional_post_infos label{
 + width : 100%;
 + }
 + .mb_additional_post_infos input{
 + width : 100%;
 + }
 + .mb_additional_post_infos textarea{
 + margin           : 2px 0px 0px 0px;
 + width            : 100%;
 + border           : none;
 + outline          : none;
 + border-radius    : 0px;
 + border-left      : 2px solid rgb(0,124,186,0.0);
 + background-color : rgb(245,245,245,1);
 + color            : rgb(90,90,90);
 + resize           : none;
 + transition       : border 250ms, background-color 250ms, color 250ms;
 + box-shadow       : none;
 + }
 + .mb_additional_post_infos textarea:hover{
 + border-left      : 2px solid rgb(0,124,186,0.5);
 + background-color : rgb(250,250,250,1);
 + }
 + .mb_additional_post_infos textarea:focus{
 + border           : none;
 + outline          : none;
 + border-left      : 2px solid rgb(0,124,186,1.0);
 + color            : rgb(0,0,0);
 + background-color : rgb(255,255,255,1);
 + box-shadow       : none;
 + }
 +
 + .mb_additional_post_infos p em {
 + margin      : 4px 0px 0px 0px;
 + font-size   : 10px;
 + line-height : 12px;
 + }
 + </style>
 + HTML;
 +
 + }
 + 
 + public static function save( $post_id )
 + {
 + // Nichts machen bei Auto-Save
 + if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
 +
 + // Nichts tun, wenn gar kein Meta mit geschickt wurde
 + if( !isset( $_POST['additional_post_infos'] ) ) return;
 +
 + update_post_meta( $post_id, 'additional_post_infos', $_POST['additional_post_infos'] );
 + }
 +
 +
 +
 + public static function get_description():string
 + {
 + global $post;
 + $post_meta_description = get_post_meta( $post->ID, 'additional_post_infos',true )['description'] ?? '';
 +
 + return $post_meta_description;
  }  }
 } }
 </code> </code>
  

Page Tools