有时,一个例子能说出一千多个单词。这里有一个小插件可以指导您:
<?php
! defined( \'ABSPATH\' ) AND exit;
/**
* Plugin Name: (#65981) »kaiser« Meta Box example
* Plugin URI: http://goo.gl/ls6Q6
* Description: Example showing how to add a meta box with callback args
* Author: Franz Josef Kaiser
* Author URI: http://unserkaiser.com
*/
if ( ! class_exists( \'WPSE65981_MetaBox_Example\' ) )
{
add_action( \'load-post.php\', array( \'WPSE65981_MetaBox_Example\', \'init\' ) );
add_action( \'load-post-new.php\', array( \'WPSE65981_MetaBox_Example\', \'init\' ) );
/**
* The Class
*/
class WPSE65981_MetaBox_Example
{
protected static $instance;
public static function init()
{
null === self :: $instance AND self :: $instance = new self;
return self :: $instance;
}
public function __construct()
{
add_action( \'add_meta_boxes\', array( $this, \'add_meta_box\' ) );
}
/**
* Adds the meta box container
*
* @return void
*/
public function add_meta_box()
{
add_meta_box(
// ID
\'wpse65981_meta_box_example\'
// Title/"hndle"-bar
,__( \'WPSE Meta Box Headline\', \'example_textdomain\' )
// Callback
,array( $this, \'render_meta_box_content\' )
// post_type
,\'post\'
// context
,\'advanced\'
// priority
,\'high\'
// callback_args
,array( \'Hello\', "{$GLOBALS[\'current_user\']->display_name}!" )
);
}
/**
* Render Meta Box content (Callback)
*
* @return void
*/
public function render_meta_box_content( $post, $callback_args )
{
// Argument dump: Uncomment for insights
# var_dump( $post );
# var_dump( $callback_args );
$args = array_map( \'esc_html\', $callback_args[\'args\'] );
return print "<h2>".implode( \' \', $args )."</h2>";
}
} // END Class WPSE65981_MetaBox_Example
} // endif;
如果你想知道,我是如何识别那些用于静态
init
要加载的函数,然后
take a look at my »current admin info« plugin here.
![enter image description here](https://i.stack.imgur.com/TThCV.png)