Hier eine Anleitung, wie man eine „Metabox“ mit „Textfeld“ in ein „Custom Post Type“ einfügt.
Als erstes definieren wir eine MetaBox.
function themename_add_post_meta_box_style() {
add_meta_box(
'themename_post_meta_box_style', // ID der MetaBox, womit sie in WP registriert wird
__('My Title'), // Titel der MetaBox
array( $this, 'themename_post_meta_box_style_callback'), // aufrufen der Funktion, womit der Inhalt unserer MetaBox definiert wird
'myPostType', // ID vom "Custom Post Type" in dem die MetaBox eingefügt werden soll
'normal', // Positon der MetaBox, normal = mitte, side = rechte Seite
'high' // Die Priorität innerhalb des Kontexts, in dem die Felder angezeigt werden sollen
);
}Code-Sprache: PHP (php)
Die MetaBox fügen wir dem „Custom Post Type“ unserer Wahl hinzu.
- add_meta_boxes = ID womit die MetaBox registriert wird
- themename_add_post_meta_box_style = die Funktion womit wir die MetaBox definiert haben
- 5 = um die Reihenfolge anzugeben, in der die einer bestimmten Aktion zugeordneten Funktionen ausgeführt werden. Niedrigere Zahlen entsprechen einer früheren Ausführung, und Funktionen mit derselben Priorität werden in der Reihenfolge ausgeführt, in der sie der Aktion hinzugefügt wurden.
add_action( 'add_meta_boxes', array( $this, 'themename_add_post_meta_box_style'), 5 );Code-Sprache: PHP (php)
Jetzt kümmern wir uns um den Inhalt unserer MetaBox, diese Funktion wird im ersten Schritt (definieren der MetaBox) aufgerufen
public function themename_post_meta_box_style_callback() {
global $post;
global $post;
$custom = get_post_custom($post->ID);
$function = isset($custom["function"][0])?$custom["function"][0]:'';
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table class="form-table">
<tr>
<th><label for="function">Function</label></th>
<td>
<input type="text" class="regular-text" name="function" value="<?php echo $function; ?>" id="function" /><br />
</td>
</tr>
<tr>
<th><label for="button"></label></th>
<td>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="submit" class="button" name="mybutton" value="Button" /><br />
<span class="description">Text deiner Wahl</span>
</form>
</td>
</tr>
</table>
</form>
<?php
}Code-Sprache: HTML, XML (xml)
Als letztes müssen wir uns noch drum kümmern, das die Daten aus dem Textfeld gespeichert werden.
function prefix_teammembers_save_post()
{
if(empty($_POST)) return; //why is prefix_teammembers_save_post triggered by add new?
global $post;
update_post_meta($post->ID, "function", $_POST["function"]);
}Code-Sprache: PHP (php)
Die Funktion wird mit folgendem Befehl geaddet.
ACHTUNG: Hierbei ist drauf zu achten, das die Variable „save_post_myPostType“ aus zwei teilen besteht, der erste Teil ist „save_post_“ dieser darf nicht verändert werden, der zweite Teil ist „myPostType“ das muss die ID vom “ Custom Post Type “ sein, wo die MetaBox hinzugefügt wurde
add_action( 'save_post_myPostType', array( $this, 'prefix_teammembers_save_post') );Code-Sprache: PHP (php)
Hier nochmal der ganze Code mit Plugin Main-Datei
main-metabox.php
<?php
/*
Plugin Name: My Custom User Field
Description:
Version: 0.0.1
Author: Inge
Author URI:
*/
/*
Damit unsere PHP-Datei nicht von außerhalb unserer WordPress-Instanz ausgeführt werden kann,
überprüfen wir nach dem Header-Kommentar ob die Konstante „ABSPATH“ gesetzt ist.
Die Konstante „ABSPATH“ wird in der „wp-config.php“ definiert und beinhaltet den absoluten Dateipfad zur WordPress Installation.
*/
defined( 'ABSPATH' ) or die( 'Are you ok?' );
// Die Datei mit den CustomFields einbinden
require_once 'myMetaBox.php';
// Eine Instanz der Main-Klasse erstellen, ist sozusagen der Start der Anwendung
$mainMetaBox= new MainMetaBox();
class MainMetaBox{
private $myMetaBox;
public function __construct() {
// Eine Instanz der Klasse "UserCustomFields" aus der Datei "myMetaBox.php" erstellen under
// mit "$this->myMetaBox" in der Variable "$myMetaBox" speichern
$this->myMetaBox= new MyMetaBox();
// Wird nur ausgeführt, wenn das Plugin aktiviert wird
register_activation_hook( __FILE__, array( $this, 'my_plugin_activate' ) );
// Wird nur ausgeführt, wenn das Plugin deaktiviert wird
register_deactivation_hook(__FILE__, array( $this, 'my_plugin_deactivation' ) );
// Wird immer ausgeführt
add_action( 'init', array( $this, 'initialisation' ) );
}
public function my_plugin_activate(){
}
public function my_plugin_deactivation(){
}
public function initialisation(){
// Die function aus der Datei "myMetaBox.php" ausführen und die Custom Fields ins User-Profil einfügen
$this->myMetaBox->add_customFields();
}
}
?>Code-Sprache: HTML, XML (xml)
myMetaBox.php
<?php
class MyMetaBox {
public function __construct() {
}
function themename_add_post_meta_box_style() {
add_meta_box(
'themename_post_meta_box_style', // ID der MetaBox, womit sie in WP registriert wird
__('My Title'), // Titel der MetaBox
array( $this, 'themename_post_meta_box_style_callback'), // aufrufen der Funktion, womit der Inhalt unserer MetaBox definiert wird
'myPostType', // ID vom "Custom Post Type" in dem die MetaBox eingefügt werden soll
'normal', // Positon der MetaBox, normal = mitte, side = rechte Seite
'high' // Die Priorität innerhalb des Kontexts, in dem die Felder angezeigt werden sollen
);
}
public function themename_post_meta_box_style_callback() {
global $post;
global $post;
$custom = get_post_custom($post->ID);
$function = isset($custom["function"][0])?$custom["function"][0]:'';
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table class="form-table">
<tr>
<th><label for="function">Function</label></th>
<td>
<input type="text" class="regular-text" name="function" value="<?php echo $function; ?>" id="function" /><br />
</td>
</tr>
<tr>
<th><label for="button"></label></th>
<td>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="submit" class="button" name="mybutton" value="Button" /><br />
<span class="description">Text deiner Wahl</span>
</form>
</td>
</tr>
</table>
</form>
<?php
}
function prefix_teammembers_save_post()
{
if(empty($_POST)) return; //why is prefix_teammembers_save_post triggered by add new?
global $post;
update_post_meta($post->ID, "function", $_POST["function"]);
}
public function add_metaBox() {
add_action( 'add_meta_boxes', array( $this, 'themename_add_post_meta_box_style'), 5 );
add_action( 'save_post_myPostType', array( $this, 'prefix_teammembers_save_post') );
}
}
?>Code-Sprache: HTML, XML (xml)