Wordpress 插件笔记

  1. 添加管理员配置菜单
<?php
//添加事件,
add_action('admin_menu', 'display_copyright_page');

function display_copyright_page() {
  $tm_iconpath = get_option('siteurl').'/wp-content/plugins/'.basename(dirname(__FILE__)).'/images/twlogo.gif';
//添加顶级的菜单。
  $admin_page = add_menu_page('Copyright', 'Copyright', 'manage_options', 'copyright', 'cuestor_page', $tm_iconpath);
//为该菜单添加子菜单
  add_submenu_page('copyright', 'submenu', 'Test Sublevel', 'manage_options', 'copyright-sub', 'mt_sublevel_page');
  //条件加载样式,只有在本插件才可以加载这个颜色
  add_action( 'admin_print_styles-' . $admin_page, 'style');

}
function mt_sublevel_page() {
 echo '<div class="wrap"><h2>Sub Menu</h2></div>';
}
function style() {
  wp_register_style( 'copyright-admin',  get_option('siteurl').'/wp-content/plugins/'.basename(dirname(__FILE__)).'/css/copyright.css' );
  wp_enqueue_style( 'copyright-admin' ); 

}
function cuestor_page() {
  echo '<p class="copyright">hello cuestom</p>';
}

add_submenu_page 添加子菜单的时候,也可以为现有的功能模块添加子菜单,如果为自定义的菜单添加子菜单第一个参数必须和menu 的ID一致,自己的menuId 必须唯一。

如果没有比较添加顶级菜单,可以通过

add_options_page('版权设置页面', '版权设置菜单', 'administrator', 'display_copyright', 'display_copyright_html_page');

在现有的配置项内内添加,配置文件。

2.如果写widget

widget是wordpress的小物件,可以通过写widget的形式把插件中的数据展现在出来,如果是wp3.0以上的版本在后台的widget页面可以控制你要在哪里显示widget.

那如果写widget呢?

第一步:add_action(“widgets_init”,”function_name”)绑定事件

第二步:register_widget();注册你的widget.

下面看我写的代码

<?php
class MyWidget extends WP_Widget {
    //
	function __construct() {
		$widget_ops = array(
			'classname' => 'jamzhou',
			'description' => "Display a user's favorite movie",
			'updated' => false,
		);
		//$control_ops = array( 'width' => 450, 'height' => 250, 'id_base' => 'abcd' ); /* Widget control settings. */
		$this->WP_Widget('MyWidget', 'MyWidget');

	}
	//创建小工具的设置菜单
	function form($instance) {
   		$defaults = array('title' =>'My Info', 'movie' => '');
		$instance = wp_parse_args((array) $instance, $defaults);
		$title = $instance['title'];
		$movie = $instance['movie'];
		$song = $instance['song'];
		?>
            <p>Title: <input class="widefat" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
            <p> Favorite Movie: <input class="widefat" name="<?php echo $this->get_field_name('movie'); ?> " type="text" value="<?php echo esc_attr( $movie ); ?> " /> </p>
            <p> Favorite Song: <textarea class="widefat" name="<?php echo $this->get_field_name('song'); ?> " />
                <?php echo esc_attr( $song ); ?> </textarea> </p>
            <?php		
	
	}
	//update widget configuration
	function update($new_instance, $old_instance) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['movie'] = strip_tags($new_instance['movie']);
		$instance['song'] = strip_tags($new_instance['song']);
		return $instance;

	}
	//display widget
	function widget($args, $instance) {
		extract($args);
		echo $before_widget;
		$title = apply_filters('widget_title', $instance['title']);
		$movie = empty($instance['movie']) ? '&nbsp;' : $instance['movie'];
		$song = empty($instance['song']) ? '&nbsp;'  : $instance['song'];

		if ( !empty($title)) {
			echo $before_title . $title . $after_title;
		}
		echo '<p> Fav Movie:'  . $movie . '</p>';
		echo '<p> Fav Song:' . $song . '</p>';
		echo $after_widget;

	}
}


add_action('widgets_init', 'MyWidgetLoad');
function MyWidgetLoad() {
	register_widget('MyWidget');
}

如果你要写widget你必须继承自WP_Widget类。

$this->WP_Widget('MyWidget', $options,$controls); 有三个参数,第一个为widget的类名,第二和第三个参数都可以传数组或者是字符串。

options的值为数组array(‘classname’=>,description=>””)等。

widget 中的before_widget 和after_widget 默认是<div></div>也可以在$control中定义

before_widget

(string) the text or HTML befor the widget.
Default: <div class="widget {widget's classname}">
after_widget
(string) the text or HTML after the widget.
Default: </div>
before_title
(string) the text or HTML before the widget's title.
Default: <h2 class="widgettitle">
after_title
(string) the text or HTML after the widget's title.
Default: </h2>

在写这个的时候碰到一个问题,提交表单的时候在update方法里面得到到表单传过来的参数,找了好久原来是form方法显示表单的时候

input元素的name属性多了个空格导致的了,如<input name="title"> 写成了<input name=” title”> 悲剧了。

posted @ 2012-11-28 13:05  phpzxh  阅读(669)  评论(0编辑  收藏  举报