<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>浮山狼de博客 &#187; widget</title>
	<atom:link href="https://www.fushanlang.com/tag/widget/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.fushanlang.com</link>
	<description>next station - 下一站，活在当下，且行且思</description>
	<lastBuildDate>Sat, 29 Nov 2014 15:14:11 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=4.2.5</generator>
	<item>
		<title>wordpress创建widget</title>
		<link>https://www.fushanlang.com/wordpress%e5%88%9b%e5%bb%bawidget-185/</link>
		<comments>https://www.fushanlang.com/wordpress%e5%88%9b%e5%bb%bawidget-185/#comments</comments>
		<pubDate>Mon, 09 Aug 2010 16:12:23 +0000</pubDate>
		<dc:creator><![CDATA[fushanlang]]></dc:creator>
				<category><![CDATA[wordpress]]></category>
		<category><![CDATA[widget]]></category>

		<guid isPermaLink="false">http://www.fushanlang.com/blog/?p=185</guid>
		<description><![CDATA[<p>我在WordPress-o-Sphere网站上看到一些Wordpress 2.8创建Widget指南，但是我并认为它们中任何一个真正涉及了实际应用。我想通过使用WordPress 2.8的Widget类来告诉你如何创建一个实际应用的Widget。</p> <p>这个教程，我会从建立Widget、设置表单，显示在网站一步步来告诉你。结尾附有可供下载的实例。当然，你也通过把它应用到你的主题中去。如果你不想阅读教程，可以直接到教程结尾下载实例。</p> 建立什么样的Widget实例？ <p>这个教程，我们将建立一个极其简单的实例，但是你会从中看到如何给予用户更多的Widget控制权的高级技术。</p> <p>实例Widget将显示一个人的姓名和性别。控件将允许输入Widget标题（文本框）、用户名称（文本框），可以选择用户的性别（选择框）和是否允许公开显示性别信息（复选框）。</p> <p>以下是Widget的外观：</p> <p>我知道，这个优点简单，但你可以把这些工具应用到更复杂的Widget中。</p> 在恰当的时候载入Widget <p>我门要做的第一件事情是必须在需要的时候载入Widget。Wordpress提供的widgets_init操作钩子（Action Hook）使我们能够做到这一点。操作钩子（Action Hook）在Wordpress默认Widget注册后被触发。</p> <p>function Example_Widget() { /* Widget settings. */ $widget_ops = array( ‘classname’ =&#62; ‘example’, ‘description’ =&#62; ‘An example widget that displays a person\’s name and sex.’ ); /* Widget control settings. */ $control_ops = array( ‘width’ =&#62; 300, ‘height’ =&#62; 350, ‘id_base’ <span style="color:#777"> . . . &#8594; Read More: <a href="https://www.fushanlang.com/wordpress%e5%88%9b%e5%bb%bawidget-185/">wordpress创建widget</a></span>]]></description>
				<content:encoded><![CDATA[<p>我在WordPress-o-Sphere网站上看到一些Wordpress 2.8创建Widget指南，但是我并认为它们中任何一个真正涉及了实际应用。我想通过使用WordPress 2.8的Widget类来告诉你如何创建一个实际应用的Widget。</p>
<p>这个教程，我会从建立Widget、设置表单，显示在网站一步步来告诉你。结尾附有可供下载的实例。当然，你也通过把它应用到你的主题中去。如果你不想阅读教程，可以直接到教程结尾下载实例。<span id="more-185"></span></p>
<h3>建立什么样的Widget实例？</h3>
<p>这个教程，我们将建立一个极其简单的实例，但是你会从中看到如何给予用户更多的Widget控制权的高级技术。</p>
<p>实例Widget将显示一个人的姓名和性别。控件将允许输入Widget标题（文本框）、用户名称（文本框），可以选择用户的性别（选择框）和是否允许公开显示性别信息（复选框）。</p>
<p>以下是Widget的外观：</p>
<p>我知道，这个优点简单，但你可以把这些工具应用到更复杂的Widget中。</p>
<h3>在恰当的时候载入Widget</h3>
<p>我门要做的第一件事情是必须在需要的时候载入Widget。Wordpress提供的widgets_init操作钩子（Action Hook）使我们能够做到这一点。操作钩子（Action Hook）在Wordpress默认Widget注册后被触发。</p>
<blockquote><p>function Example_Widget() {<br />
/* Widget settings. */<br />
$widget_ops = array( ‘classname’ =&gt; ‘example’, ‘description’ =&gt; ‘An example widget that displays a person\’s name and sex.’ );<br />
/* Widget control settings. */<br />
$control_ops = array( ‘width’ =&gt; 300, ‘height’ =&gt; 350, ‘id_base’ =&gt; ‘example-widget’ );</p>
<p>/* Create the widget. */<br />
$this-&gt;WP_Widget( ‘example-widget’, ‘Example Widget’, $widget_ops, $control_ops );<br />
}</p></blockquote>
<p>如果你要创建一个以上的Widget，请在example_load_widgets()函数内为每个Widget使用register_widget()函数。</p>
<h3>建立我们的Widget</h3>
<p>过去，我们建立Widget是一件很费解的事情。在2.8里，我们只要扩展原有的WP_Widget类。因此，第一步是建立一个新的类（Class）。</p>
<blockquote><p>class Example_Widget extends WP_Widget {</p></blockquote>
<p>然后，我门将要添加第一个函数。这个函数将使我们的Widget不同于默认的，且允许我们设置它。</p>
<p>请注意，类名称和函数名称是相同的。例子中均为 Example_Widget。</p>
<blockquote><p>function Example_Widget() {</p>
<p>/* Widget settings. */</p>
<p>$widget_ops = array( ‘classname’ =&gt; ‘example’, ‘description’ =&gt; ‘An example widget that displays a person\’s name and sex.’ );</p>
<p>/* Widget control settings. */</p>
<p>$control_ops = array( ‘width’ =&gt; 300, ‘height’ =&gt; 350, ‘id_base’ =&gt; ‘example-widget’ );</p>
<p>/* Create the widget. */</p>
<p>$this-&gt;WP_Widget( ‘example-widget’, ‘Example Widget’, $widget_ops, $control_ops );</p>
<p>}</p></blockquote>
<h3>显示Widget</h3>
<p>Example_Widget类的下一个函数负责显示Widget。此代码可能使你优点困惑，因为我们并不知道发生了什么（我们还没有添加控制功能）。这这里我们将取得用户设置并根据设置显示。</p>
<p>还有一点也很重要，确保$before_widget，$after_widget，$before_title，$after_title可以用。这些是由主题实现的，我们不应该硬编码。Widget如何显示是由主题负责的。</p>
<blockquote><p>function widget( $args, $instance ) {<br />
extract( $args );</p>
<p>/* User-selected settings. */<br />
$title = apply_filters(’widget_title’, $instance[&#8216;title&#8217;] );<br />
$name = $instance[&#8216;name&#8217;];<br />
$sex = $instance[&#8216;sex&#8217;];<br />
$show_sex = isset( $instance[&#8216;show_sex&#8217;] ) ? $instance[&#8216;show_sex&#8217;] : false;</p>
<p>/* Before widget (defined by themes). */<br />
echo $before_widget;</p>
<p>/* Title of widget (before and after defined by themes). */<br />
if ( $title )<br />
echo $before_title . $title . $after_title;</p>
<p>/* Display name from widget settings. */<br />
if ( $name )<br />
echo ‘&lt;p&gt;Hello. My name is’ . $name . ‘.&lt;/p&gt;’;</p>
<p>/* Show sex. */<br />
if ( $show_sex )<br />
echo ‘&lt;p&gt;I am a ‘ . $sex . ‘.&lt;/p&gt;’;</p>
<p>/* After widget (defined by themes). */<br />
echo $after_widget;<br />
}</p></blockquote>
<h3>确保Widget更新并保存</h3>
<p>这一步，我们将设置每个Widget并保存。这是一个简单的程序，我们只是更新用户的设置。</p>
<p>如果你在表格中使用的是一个像文本输入的功能，而不让用户使用XHTML，那么你应该使用下面的strip_tags()函数。</p>
<blockquote><p>function update( $new_instance, $old_instance ) {<br />
$instance = $old_instance;</p>
<p>/* Strip tags (if needed) and update the widget settings. */<br />
$instance[&#8216;title&#8217;] = strip_tags( $new_instance[&#8216;title&#8217;] );<br />
$instance[&#8216;name&#8217;] = strip_tags( $new_instance[&#8216;name&#8217;] );<br />
$instance[&#8216;sex&#8217;] = $new_instance[&#8216;sex&#8217;];<br />
$instance[&#8216;show_sex&#8217;] = $new_instance[&#8216;show_sex&#8217;];</p>
<p>return $instance;<br />
}</p></blockquote>
<h3>创建Widget设置面版</h3>
<p>WordPress 2.8新的Widget类能够轻易创建Widget控制面版。get_field_id()和get_field_name()函数负责大部分肮脏的工作，使我们更专注于像建立Widget这样更重要的事情。请特别注意这个函数的使用，因为它能使得你的更简单地开发。</p>
<p>首先，我们可能想要设置一些默认值。通过建立默认值，我们可以控制在用户没有设置时该显示什么。</p>
<blockquote><p>function form( $instance ) {</p>
<p>/* Set up some default widget settings. */<br />
$defaults = array( ‘title’ =&gt; ‘Example’, ‘name’ =&gt; ‘John Doe’, ’sex’ =&gt; ‘male’, ’show_sex’ =&gt; true );<br />
$instance = wp_parse_args( (array) $instance, $defaults ); ?&gt;</p></blockquote>
<p>表格的前两部分是文本输入框，Widget标题和用户性别。</p>
<blockquote><p>&lt;p&gt;<br />
&lt;label for=”&lt;?php echo $this-&gt;get_field_id( ‘title’ ); ?&gt;”&gt;Title:&lt;/label&gt;<br />
&lt;input id=”&lt;?php echo $this-&gt;get_field_id( ‘title’ ); ?&gt;” name=”&lt;?php echo $this-&gt;get_field_name( ‘title’ ); ?&gt;” value=”&lt;?php echo $instance[&#8216;title&#8217;]; ?&gt;” style=”width:100%;” /&gt;<br />
&lt;/p&gt;</p>
<p>&lt;p&gt;<br />
&lt;label for=”&lt;?php echo $this-&gt;get_field_id( ‘name’ ); ?&gt;”&gt;Your Name:&lt;/label&gt;<br />
&lt;input id=”&lt;?php echo $this-&gt;get_field_id( ‘name’ ); ?&gt;” name=”&lt;?php echo $this-&gt;get_field_name( ‘name’ ); ?&gt;” value=”&lt;?php echo $instance[&#8216;name&#8217;]; ?&gt;” style=”width:100%;” /&gt;<br />
&lt;/p&gt;</p></blockquote>
<p>表单第二步是这一单选框，允许用户选择性别。</p>
<blockquote><p>&lt;p&gt;<br />
&lt;label for=”&lt;?php echo $this-&gt;get_field_id( ’sex’ ); ?&gt;”&gt;Sex:&lt;/label&gt;<br />
&lt;select id=”&lt;?php echo $this-&gt;get_field_id( ’sex’ ); ?&gt;” name=”&lt;?php echo $this-&gt;get_field_name( ’sex’ ); ?&gt;” class=”widefat” style=”width:100%;”&gt;<br />
&lt;option &lt;?php if ( ‘male’ == $instance[&#8216;format&#8217;] ) echo ’selected=”selected”‘; ?&gt;&gt;male&lt;/option&gt;<br />
&lt;option &lt;?php if ( ‘female’ == $instance[&#8216;format&#8217;] ) echo ’selected=”selected”‘; ?&gt;&gt;female&lt;/option&gt;<br />
&lt;/select&gt;<br />
&lt;/p&gt;</p></blockquote>
<p>表单的最后一项是一个可供用户选择是否显示性别信息的复选框。</p>
<blockquote><p>&lt;p&gt;<br />
&lt;input class=”checkbox” type=”checkbox” &lt;?php checked( $instance[&#8216;show_sex&#8217;], true ); ?&gt; id=”&lt;?php echo $this-&gt;get_field_id( ’show_sex’ ); ?&gt;” name=”&lt;?php echo $this-&gt;get_field_name( ’show_sex’ ); ?&gt;” /&gt;<br />
&lt;label for=”&lt;?php echo $this-&gt;get_field_id( ’show_sex’ ); ?&gt;”&gt;Display sex publicly?&lt;/label&gt;<br />
&lt;/p&gt;</p></blockquote>
<p>唯一还没有完成的是关闭表单函数并完成剩下的Widget代码。</p>
<blockquote><p>&lt;?php<br />
}<br />
}<br />
?&gt;</p></blockquote>
<h3>创建属于你自己的Widget</h3>
<p>如果你在WordPress 2.8以前曾经和企图创建一个Widget，那么你可以看看现在是多么的简单。为了帮助人们学习新的系统，我已经把这一教程的代码放到一起，并在里面详细注释了代码的功能。</p>
]]></content:encoded>
			<wfw:commentRss>https://www.fushanlang.com/wordpress%e5%88%9b%e5%bb%bawidget-185/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
