This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revision | |||
programmieren:wordpress:klassische_metabox_fuer_posts_erstellen [2024/07/22 09:45] – jgehrke | programmieren:wordpress:klassische_metabox_fuer_posts_erstellen [2024/08/10 10:38] (current) – jgehrke | ||
---|---|---|---|
Line 53: | Line 53: | ||
} | } | ||
</ | </ | ||
+ | |||
+ | ===== 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. | 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. | ||
Line 99: | Line 101: | ||
</ | </ | ||
+ | |||
+ | ===== 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: | ||
+ | '' | ||
+ | |||
+ | <code php> | ||
+ | <? | ||
+ | |||
+ | | ||
+ | |||
+ | add_action( ' | ||
+ | add_action( ' | ||
+ | |||
+ | final class AdditionalPostInfos | ||
+ | { | ||
+ | private static $ID = ' | ||
+ | private static $title | ||
+ | private static $render_callback = [' | ||
+ | private static $show_on_screens = [ ' | ||
+ | private static $position | ||
+ | private static $priority | ||
+ | |||
+ | private static $entry_meta_key | ||
+ | |||
+ | public static function add_metabox() | ||
+ | { | ||
+ | add_meta_box( self::$ID, self:: | ||
+ | } | ||
+ | |||
+ | public static function render_metabox() | ||
+ | { | ||
+ | $value_description = self:: | ||
+ | |||
+ | echo <<< | ||
+ | <div class=" | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | </ | ||
+ | < | ||
+ | </ | ||
+ | |||
+ | < | ||
+ | .mb_additional_post_infos label{ | ||
+ | width : 100%; | ||
+ | } | ||
+ | .mb_additional_post_infos input{ | ||
+ | width : 100%; | ||
+ | } | ||
+ | .mb_additional_post_infos textarea{ | ||
+ | margin | ||
+ | width | ||
+ | border | ||
+ | outline | ||
+ | border-radius | ||
+ | border-left | ||
+ | background-color : rgb(245, | ||
+ | color | ||
+ | resize | ||
+ | transition | ||
+ | box-shadow | ||
+ | } | ||
+ | .mb_additional_post_infos textarea: | ||
+ | border-left | ||
+ | background-color : rgb(250, | ||
+ | } | ||
+ | .mb_additional_post_infos textarea: | ||
+ | border | ||
+ | outline | ||
+ | border-left | ||
+ | color | ||
+ | background-color : rgb(255, | ||
+ | box-shadow | ||
+ | } | ||
+ | |||
+ | .mb_additional_post_infos p em { | ||
+ | margin | ||
+ | font-size | ||
+ | line-height : 12px; | ||
+ | } | ||
+ | </ | ||
+ | HTML; | ||
+ | |||
+ | } | ||
+ | |||
+ | public static function save( $post_id ) | ||
+ | { | ||
+ | // Nichts machen bei Auto-Save | ||
+ | if( defined( ' | ||
+ | |||
+ | // Nichts tun, wenn gar kein Meta mit geschickt wurde | ||
+ | if( !isset( $_POST[' | ||
+ | |||
+ | update_post_meta( $post_id, ' | ||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | public static function get_description(): | ||
+ | { | ||
+ | global $post; | ||
+ | $post_meta_description = get_post_meta( $post-> | ||
+ | |||
+ | return $post_meta_description; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ |