WordPress中使主题支持小工具以及添加插件启用函数
<p><strong>让主题支持小工具</strong><br>WordPress 的小工具(widget)是一大特色,它让用户自由拖动组合内容,而且任何插件和主题都可以添加一个额外的小工具,增加扩展性。</p>
<p>
默认情况下,一个主题并不会支持小工具,需要主题开发者启用小工具功能并把小工具在相应的前台位置调用出来,这样用户才能在后台直接拖动生成侧边栏。</p>
<p>
本文就来教你如何激活小工具功能,并且添加一个侧边栏,最后在前台显示出来。</p>
<p>
注册侧边栏</p>
<p>
默认的,后台外观下是没有 “小工具” 这个菜单按钮的,如果想要让他出现,就至少需要注册一个侧边栏,否则即使显示出来,也没有用。</p>
<p>
注册一个侧边栏需要使用 register_sidebar() 函数,用法比较简单,只有一个属性,填上需要的信息就行了。</p>
<div>
<div>
<div id="highlighter_887136">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<div>
4</div>
<div>
5</div>
<div>
6</div>
<div>
7</div>
<div>
8</div>
<div>
9</div>
</td>
<td>
<div>
<div>
<code>register_sidebar( </code><code>array</code><code>(</code>
</div>
<div>
<code> </code><code>'name'</code> <code>=> __( </code><code>'默认侧边栏'</code><code>, </code><code>'Bing'</code> <code>),</code><code>//侧边的名字</code>
</div>
<div>
<code> </code><code>'id'</code> <code>=> </code><code>'widget_default'</code><code>,</code><code>//侧边栏的 ID,注册多个侧边栏的时候不要重复</code>
</div>
<div>
<code> </code><code>'description'</code> <code>=> __( </code><code>'侧边栏的描述'</code><code>, </code><code>'Bing'</code> <code>),</code><code>//侧边栏的描述,会在后台显示</code>
</div>
<div>
<code> </code><code>'before_widget'</code> <code>=> </code><code>'<div>'</code><code>,</code><code>//侧边栏里的小工具的开头代码,可以在里边使用 %2$s 来调用小工具的 ID,实现给每个小工具添加不同的样式</code>
</div>
<div>
<code> </code><code>'after_widget'</code> <code>=> </code><code>'</div>'</code><code>,</code><code>//侧边栏里的小工具的结尾代码</code>
</div>
<div>
<code> </code><code>'before_title'</code> <code>=> </code><code>'<h3>'</code><code>,</code><code>//侧边栏里的小工具的标题的开头代码</code>
</div>
<div>
<code> </code><code>'after_title'</code> <code>=> </code><code>'</h3>'</code><code>//侧边栏里的小工具的标题的结尾代码</code>
</div>
<div>
<code>) );</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
运用上边的例子代码,就能创建出一个侧边栏,外观下也显示 “小工具” 按钮了。</p>
<p>
<img style="max-width:100%!important;height:auto!important;"title="WordPress中使主题支持小工具以及添加插件启用函数" alt="WordPress中使主题支持小工具以及添加插件启用函数" src="https://zhuji.jb51.net/uploads/img/202305/5310845ad71b49645c192f5ebde0790e.jpg"></p>
<p>
在这个侧边栏里,用户就可以自由的添加小工具了。复制代码,还可以创建更多的侧边栏。</p>
<p>
调用侧边栏</p>
<p>
光注册是没有意义的,用户添加之后,还要把小工具显示在前台才可以,这就用到了 dynamic_sidebar() 函数。</p>
<p>
一般情况下,我们需要先判断一下小工具区域内是否添加了小工具,如果添加了,则显示小工具,否则提示用户添加小工具。判断侧边栏里是否被添加了小工具需要使用 is_active_sidebar() 函数。</p>
<div>
<div>
<div id="highlighter_53365">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<div>
4</div>
<div>
5</div>
<div>
6</div>
<div>
7</div>
<div>
8</div>
<div>
9</div>
</td>
<td>
<div>
<div>
<code><?php</code>
</div>
<div>
<code>if</code><code>( is_active_sidebar( </code><code>'widget_default'</code> <code>) ){</code>
</div>
<div>
<code> </code><code>echo</code> <code>'<aside id="sidebar">'</code><code>;</code>
</div>
<div>
<code> </code><code>dynamic_sidebar( </code><code>'widget_default'</code> <code>);</code>
</div>
<div>
<code> </code><code>echo</code> <code>'</aside>'</code><code>;</code>
</div>
<div>
<code>}</code><code>else</code><code>{</code>
</div>
<div>
<code> </code><code>echo</code> <code>'<p>请设置小工具</p>'</code><code>;</code>
</div>
<div>
<code>}</code>
</div>
<div>
<code>?></code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
非常简单吧,调用好后,前台的小工具就会按照注册时给出的格式一个一个的输出了。</p>
<p>
<strong>添加插件启用函数</strong><br>
register_activation_hook() 可以让你添加一个函数,这个函数会在指定插件启用时执行,一般用于插件开发。</p>
<p>
用法</p>
<div>
<div>
<div id="highlighter_883166">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
</td>
<td>
<div>
<div>
<code>register_activation_hook( </code><code>$file</code><code>, </code><code>$function</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
参数</p>
<p>
$file</p>
<p>
(字符串)(必须)需要在启用时执行函数的插件的主文件路径,如果是当前插件,直接写 __FILE__ 即可。</p>
<p>
$function</p>
<p>
(回调函数)(必须)执行的函数。</p>
<p>
例子</p>
<div>
<div>
<div id="highlighter_638811">
<div>
<span>?</span>
</div>
<table border="0" cellpadding="0" cellspacing="0"><tbody><tr>
<td>
<div>
1</div>
<div>
2</div>
<div>
3</div>
<div>
4</div>
<div>
5</div>
</td>
<td>
<div>
<div>
<code>function</code> <code>Bing_myplugin_activate(){</code>
</div>
<div>
<code> </code><code>global</code> <code>$wpdb</code><code>;</code>
</div>
<div>
<code> </code><code>$wpdb</code><code>->query( </code><code>''</code> <code>);</code><code>//创建一些数据表</code>
</div>
<div>
<code>}</code>
</div>
<div>
<code>register_activation_hook( </code><code>__FILE__</code><code>, </code><code>'Bing_myplugin_activate'</code> <code>);</code>
</div>
</div>
</td>
</tr></tbody></table>
</div>
</div>
<div id="codetool">
<div>
<textarea></textarea>
</div>
</div>
</div>
<p>
其它</p>
<p>
此函数位于:wp-includes/plugin.php</p>
頁:
[1]