转自:http://summerbluet.com/225
在 wp-content\plugins 下创建 example-widget.php 代码如下 :
<?php
/**
* Plugin Name: Example Widget
* Plugin URI: http://example.com/widget
* Description: A widget that serves as an example for developing more advanced widgets.
* Version: 0.1
* Author: Justin Tadlock
* Author URI: http://justintadlock.com
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
/**
* Add function to widgets_init that'll load our widget.
* @since 0.1
*/
add_action( 'widgets_init', 'example_load_widgets' );
/**
* Register our widget.
* 'Example_Widget' is the widget class used below.
*
* @since 0.1
*/
function example_load_widgets() {
register_widget( 'Example_Widget' );
}
/**
* Example Widget class.
* This class handles everything that needs to be handled with the widget:
* the settings, form, display, and update. Nice!
*
* @since 0.1
*/
class Example_Widget extends WP_Widget {
/**
* Widget setup.
*/
function Example_Widget() {
/* Widget settings. */
$widget_ops = array( 'classname' => 'example', 'description' => __('An example widget that displays a person\'s name and sex.', 'example') );
/* Widget control settings. */
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'example-widget' );
/* Create the widget. */
$this->WP_Widget( 'example-widget', __('Example Widget', 'example'), $widget_ops, $control_ops );
}
/**
* How to display the widget on the screen.
*/
function widget( $args, $instance ) {
extract( $args );
/* Our variables from the widget settings. */
$title = apply_filters('widget_title', $instance['title'] );
$name = $instance['name'];
$sex = $instance['sex'];
$show_sex = isset( $instance['show_sex'] ) ? $instance['show_sex'] : false;
/* Before widget (defined by themes). */
echo $before_widget;
/* Display the widget title if one was input (before and after defined by themes). */
if ( $title )
echo $before_title . $title . $after_title;
/* Display name from widget settings if one was input. */
if ( $name )
printf( '<p>' . __('Hello. My name is %1$s.', 'example') . '</p>', $name );
/* If show sex was selected, display the user's sex. */
if ( $show_sex )
printf( '<p>' . __('I am a %1$s.', 'example.') . '</p>', $sex );
/* After widget (defined by themes). */
echo $after_widget;
}
/**
* Update the widget settings.
*/
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
/* Strip tags for title and name to remove HTML (important for text inputs). */
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['name'] = strip_tags( $new_instance['name'] );
/* No need to strip tags for sex and show_sex. */
$instance['sex'] = $new_instance['sex'];
$instance['show_sex'] = $new_instance['show_sex'];
return $instance;
}
/**
* Displays the widget settings controls on the widget panel.
* Make use of the get_field_id() and get_field_name() function
* when creating your form elements. This handles the confusing stuff.
*/
function form( $instance ) {
/* Set up some default widget settings. */
$defaults = array( 'title' => __('Example', 'example'), 'name' => __('John Doe', 'example'), 'sex' => 'male', 'show_sex' => true );
$instance = wp_parse_args( (array) $instance, $defaults ); ?>
<!-- Widget Title: Text Input -->
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e('Title:', 'hybrid'); ?></label>
<input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width:100%;" />
</p>
<!-- Your Name: Text Input -->
<p>
<label for="<?php echo $this->get_field_id( 'name' ); ?>"><?php _e('Your Name:', 'example'); ?></label>
<input id="<?php echo $this->get_field_id( 'name' ); ?>" name="<?php echo $this->get_field_name( 'name' ); ?>" value="<?php echo $instance['name']; ?>" style="width:100%;" />
</p>
<!-- Sex: Select Box -->
<p>
<label for="<?php echo $this->get_field_id( 'sex' ); ?>"><?php _e('Sex:', 'example'); ?></label>
<select id="<?php echo $this->get_field_id( 'sex' ); ?>" name="<?php echo $this->get_field_name( 'sex' ); ?>" class="widefat" style="width:100%;">
<option <?php if ( 'male' == $instance['format'] ) echo 'selected="selected"'; ?>>male</option>
<option <?php if ( 'female' == $instance['format'] ) echo 'selected="selected"'; ?>>female</option>
</select>
</p>
<!-- Show Sex? Checkbox -->
<p>
<input class="checkbox" type="checkbox" <?php checked( $instance['show_sex'], true ); ?> id="<?php echo $this->get_field_id( 'show_sex' ); ?>" name="<?php echo $this->get_field_name( 'show_sex' ); ?>" />
<label for="<?php echo $this->get_field_id( 'show_sex' ); ?>"><?php _e('Display sex publicly?', 'example'); ?></label>
</p>
<?php
}
}
?>
分享到:
相关推荐
这本书以清晰易懂的方式,提供了丰富的实例和实用技巧,涵盖了从基础到高级的WordPress插件开发技术。 一、WordPress插件开发基础 在WordPress插件开发中,基础是至关重要的。书中首先会讲解WordPress插件的基本...
### WordPress插件开发指南知识点概览 #### 一、引言 《WordPress插件开发指南》是一本由Brad Williams、Ozh Richard与Justin Tadlock合著的专业书籍,旨在为开发者提供全面深入的WordPress插件开发指导。本书分为...
《WordPress插件开发新手入门》是由PACKT出版的一本针对初学者的WordPress插件开发指南。本书涵盖了从基础到进阶的各种技术,旨在帮助读者掌握WordPress插件的创建与实现。 在JavaScript和WordPress的结合部分,书...
《WordPress插件开发全攻略》是一份详尽的指南,旨在帮助有兴趣学习如何为WordPress开发插件的用户。这份指南由Charles翻译并整理,内容基于原版《How to Write a WordPress Plugin》,通过一系列深入浅出的文章,为...
1. Wordpress插件简介.mp4 文件大小:124,806 KB 2. WordPress插件的创建.mp4 文件大小:84,728 KB 3. 认识钩子:动作(add action与do action) .mp4 文件大小:69,992 KB 4. 认识钩子:带参数的动作.mp4 文件...
WP-FlashTime Widget插件,有二十五中不同风格样式的Flash时钟挂件,博主可以根据自己所用主题风格,选择和主题搭配的Flash时钟,此款插件在WordPress.org官方下载的频率高达15万之多,可想而知它的受欢迎程度。...
在WordPress后台,我们需要下载插件,而后通过上传的方式启用这款插件。插件安装完成后进入后台文章编辑文本模式下可以看到有一个“插入微信隐藏标签”的按钮,选中需要隐藏的图文内容,然后点击该按钮则会自动为你...
"【WordPress插件】2022年最新版完整功能demo+插件v1.7.5.5 Cooked Pro – Recipes, Cooking & Community WordPress Plugin 煮熟的专业食谱,烹饪和社区WordPress插件" ---------- 泰森云每天更新发布最新WordPress...
众所周知WP-Cache和WP Super Cache这两款页面缓存插件可以加速网页的显示,今天介绍一款插件,可以缓存WP边栏(SideBar),同样起到加速页面显示的作用,wp widget cache这款插件就是对侧栏的widget进行缓存,提高侧栏...
"【WordPress插件】2022年最新版完整功能demo+插件v3.5.4 Dokan - MultiVendor Marketplaces Plugin For WordPress Dokan - WordPress的Multivendor MarketPlaces插件" ---------- 泰森云每天更新发布最新WordPress...
Plugin Name: Devlounge Plugin Series ...Version: v1.00 Author: <a href="http://www.ronalfy.com/">Ronald Huereca Description: A sample plugin for a <a href="http://www.devlounge.net">Devlounge</a> series....
"【WordPress插件】2022年最新版完整功能demo+插件v1.1.9 WP Content Pilot Pro - Best WordPress Autoblog & Affiliate Marketing Plugin WP Content Pilot Pro - Best WordPress AutoBlog&Affiliate Marketing ...
"【WordPress插件】2022年最新版完整功能demo+插件v2.7.15 SportsPress Pro - The only WordPress plugin for serious teams and athletes 体育投票专业 - 严肃的团队和运动员的唯一WordPress插件" ---------- 泰森...
"【WordPress插件】2022年最新版完整功能demo+插件v3.6.2 Tasty Recipes - A Powerful WordPress Recipe Plugin for Food Blogs 美味的食谱 - 用于食品博客的强大Wordpress食谱插件" ---------- 泰森云每天更新发布...
"wordpress短信插件_wordpress短信接口开发_wordpress短信发送设置"这个主题涵盖了WordPress环境中短信服务的核心方面,包括如何安装短信插件、如何进行接口开发以及如何配置短信发送设置。 首先,我们来看...
"【WordPress插件】2022年最新版完整功能demo+插件v1.1 Elementor Widgets Mega Pack - Addons for Elementor Page Builder WordPress Plugin Elementor Widgets Mega Pack - Elementor Page Builder WordPress插件...
织梦dedecms转wordpress插件,该插件可以把织梦数据完整的转到wordpress并保持源链接不变不影响SEO,该插件可以把织梦的,系统参数、栏目分类、栏目内容、栏目TDK、文章内容、文章自定义字段、友情链接、一起转入到...
首先需要去公众号里配置一下,进公众号,开发 – 基本配置,配置IP白名单以及开启服务器配置,服务器地址、令牌(自行设置,确保两边填写一致)在启用插件后的插件设置里有,消息加解密方式选明文。 确保公众号配置...
PHP实例开发源码—CoolCode 代码高亮插件 修改版 WordPress 插件.zip PHP实例开发源码—CoolCode 代码高亮插件 修改版 WordPress 插件.zip PHP实例开发源码—CoolCode 代码高亮插件 修改版 WordPress 插件.zip