最近访客

为WordPress主题添加可定制小工具(英语版)

详情
评论
问答
赞助

您可以通过搜索或浏览分类列表来找到您期望下载的资源。随后点击资源介绍页右侧的下载链接按钮,依据提示信息进行操作即可。

大部分资源可积分免费下载,为了维持网站的运行小部分资源须付费才能下载。

本站提供的下载资源均为网络搜集,仅供个人学习和交流使用。对于版权问题,请用户自行判断并承担相应责任。

随着时间的推移,维持网站的运行需要支出高昂的服务器和带宽费用。为了能继续坚持免费做下去,如果觉得文章对您有用,希望您能动动发财的小手免费赞助狐狸,给狐狸加油打气!激励狐狸库继续创作下去!谢谢!

在互联网的浩瀚海洋中,我们的网站就像一座明亮的灯塔,为无数用户照亮前行的道路。它承载着知识的传递、梦想的交流,以及大家共同的回忆与期待。然而,运营和维护这样一个网站需要投入大量的心血与资金。

现在,您只需通过支付宝扫码领红包,就能轻松成为我们的赞助人。这不仅是一次简单的支持,更是您与网站共同成长的见证。操作简单,打开支付宝,扫描专属二维码,您的每一份心意都将化作网站发展的动力。

您的赞助,能让网站不断升级,提供更优质的服务,给大家带来更好的体验。让我们携手共进,让这座灯塔绽放更耀眼的光芒,照亮更多人的网络世界 。

下面是免费赞助的方法和流程

  1. 下面图一是本站的支付宝每日领红包二维码,使用支付宝扫描二维码后会自动显示红包金额,有任意小额金额红包。
  2. 点击“去使用”,再扫描第三张图的二维码进行付款即可。
  3. 上面扫到多少红包就输入多少金额,红包会自动抵扣金额,所以不需要额外费用。
  4. 如需赞助自定义金额请到【赞助狐狸】页面进行赞助。
  5. 这个扫码红包是每天都可以领的哦,支持同账号重复,每天花一分钟时间即可免费支持本站!
扫码红包 红包图 扫码支付

对主题开发者很有用处的技术文章,外语不好就不给大家翻译了,自行参阅。

对主题开发者很有用处的技术文章,外语不好就不给大家翻译了,自行参阅。

So you’re a designer and you create beautiful looking themes for WordPress. However you find it quite hard making your beautiful theme come to life when trying to integrate it with WordPress. Or maybe you’re getting the hang of this whole “coding” thing (you know HTML and CSS after all) but you need a helping hand to lead you in the right direction when it comes to the more complex PHP stuff in WordPress. Or maybe you’re simply looking for a comprehensive tutorial on a certain aspect of WordPress programming.

If you are any of these people then this series of tutorials is for you. In this series I will cover most of the popular programming topics that designers tend to struggle with when doing development work for WordPress. You might even find that it’s not as hard as you think.

Part 1 – How to Build Custom Widgets for Your Themes

One of the most common features of almost every theme that has ever existed is the ability to customize your sidebar with widgets.

“WordPress Widgets (WPW) is like a plugin, but designed to provide a simple way to arrange the various elements of your sidebar content (known as “widgets”) without having to change any code.”

WordPress Codex

Widgets provide WordPress users an easy way to customize content without having to code anything. Unfortunately that means that it is down to theme developers to do it. But don’t worry it might not be as bad as you think. Since v2.8 WordPress has come packaged with a Widget API which makes it really easy to create custom widgets for your themes, and this is what we will be using to create our example widget.

Using the Widgets API

To create a new widget using the Widgets API you simply need to create a class that extends the standard widget class. Don’t worry if you don’t know what that means, because all you have to do is open up your functions.php file and paste the following code:

class My_Widget extends WP_Widget
{
function My_Widget() {
// register the widget
}
function widget($args, $instance) {
// output the content of the widget
}
function form($instance) {
// output the admin options form
}
function update($new_instance, $old_instance) {
// processes the admin options form when saved
}
}
add_action(\'widgets_init\', create_function(\'\', \'return register_widget(\"My_Widget\");\'));

This is the template code for your widget. Obviously you can rename each instance of “My_Widget” above to something more appropriate. The important thing to notice here is the four functions which will hold all of the functionality of our widget. It’s easier to explain what these functions do by showing you an example of a widget and explaining how these four functions make it work. So that’s exactly what I’ll do.
Our Newsletter Signup Widget

For this example we are going to create a simple widget that lets users enter a title, some descriptive text and paste in their favourite newsletter signup form code (like MailChimp or CampaignMonitor for example). You will be able to change the title, description and code of the widget from the widget admin. So let’s get coding!

class Newsletter_Signup_Widget extends WP_Widget
{
// Register the widget
function Newsletter_Signup_Widget() {
// Set some widget options
$widget_options = array( \'description\' => \'Allow users to signup to your newsletter easily\', \'classname\' => \'newsletter-signup\' );
// Set some control options (width, height etc)
$control_options = array( \'width\' => 300 );
// Actually create the widget (widget id, widget name, options...)
$this->WP_Widget( \'newsletter-signup-widget\', \'Newsletter Signup\', $widget_options, $control_options );
}

First we create a new class that “extends WP_Widget” then we setup the widget in what is known as a constructor (a function with the same name as the class). Don’t worry about the technicalities here. Just accept that you need them and this is how they work.

In our constructor function first we set up some options in the $widget_options and $control_options variables. These are arrays of values that we pass into the WP_Widget() function to create our widget. The important values here are the description in $widget_options which will appear below your widget on the Widgets page, and the width (you can also specify height) in $control_options which will set the width of the widget admin box (when it is expanded).

Then we call the important WP_Widget() function. The four parameters that you need to pass in to this function are: a unique widget id (lowercase with no spaces), a widget title (which will be used on the widget admin), the $widget_options variable and finally the $control_options variable. Now we have setup our widget we can move on to the next function.

// Output the content of the widget
function widget($args, $instance) {
extract( $args ); // Don\'t worry about this
// Get our variables
$title = apply_filters( \'widget_title\', $instance[\'title\'] );
$text = $instance[\'text\'];
$code = $instance[\'code\'];
// This is defined when you register a sidebar
echo$before_widget;
// If our title isn\'t empty then show it
if ( $title ) {
echo$before_title . $title . $after_title;
}
// If our description isn\'t empty then show it
if ( $text ) {
echo \'<p>\'. $text .\'</p>\';
}
// If we have some code then output it
if ( $code ) {
echo$code;
}
// This is defined when you register a sidebar
echo$after_widget;
}

The widget() function is where we code the output that the users will see on your theme. Here we get the $title, $text and $code variables that are stored in the $instance array and simply echo them if they are not empty. If you need to do any HTML formatting of your content then here is the place to do it, but in our example we don’t need much.

Note that here we also echo four variables ($before_widget, $after_widget, $before_title, $after_title) that are defined when registering sidebars. Technically it is not essential to include these variables but if you leave them out you’re sidebar might become messed up and it certainly won’t be very flexible (especially if you are designing a theme that you want to distribute). So take my advice and just include them. Then we can move on.

// Output the admin options form
function form($instance) {
// These are our default values
$defaults = array( \'title\' => \'Example\', \'text\' => \'Subscribe to our newsletter using the form below.\', \'code\' => \'\' );
// This overwrites any default values with saved values
$instance = wp_parse_args( (array) $instance, $defaults );
?>
<p>
<label for=\"<?php echo $this->get_field_id(\'title\'); ?>\"><?php _e(\'Title:\'); ?></label>
<input id=\"<?php echo $this->get_field_id(\'title\'); ?>\" name=\"<?php echo $this->get_field_name(\'title\'); ?>\" value=\"<?php echo $instance[\'title\']; ?>\" type=\"text\" class=\"widefat\" />
</p>
<p>
<label for=\"<?php echo $this->get_field_id(\'text\'); ?>\"><?php _e(\'Description:\'); ?></label>
<input id=\"<?php echo $this->get_field_id(\'text\'); ?>\" name=\"<?php echo $this->get_field_name(\'text\'); ?>\" value=\"<?php echo $instance[\'text\']; ?>\" type=\"text\" class=\"widefat\" />
</p>
<p>
<label for=\"<?php echo $this->get_field_id(\'code\'); ?>\"><?php _e(\'Code:\'); ?></label>
<textarea id=\"<?php echo $this->get_field_id(\'code\'); ?>\" name=\"<?php echo $this->get_field_name(\'code\'); ?>\" rows=\"10\" class=\"widefat\"><?php echo $instance[\'code\']; ?></textarea>
</p>
<?php
}

The form() function is where we put the HTML that will appear in the admin form (when a widget is expanded). In our example we are going to need two text inputs (for the title & description) and a text area (for the code).

But before we create the HTML for the form we are going to set up some variables. $defaults is simply an array which holds our default values that will appear when you first add the widget to a sidebar in the widget admin. Don’t worry too much about the wp_parse_args() function. All this does is set up $instance with our array of values. If we have saved values then they will be used, otherwise it fall’s back to the $defaults.

Setting up the form itself is just basic HTML. We set the values of the HTML inputs using the $instance array which, again, is basic PHP. The thing to note is the WordPress helper functions that are used to make sure we get the input id’s and names right. Setting the id’s and names manually would be a nightmare (imagine loads of widgets made by different developers all with different id’s and names). So we just let WordPress do it for us and ask no questions. One more function to go.

// Processes the admin options form when saved
function update($new_instance, $old_instance) {
// Get the old values
$instance = $old_instance;
// Update with any new values (and sanitise input)
$instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
$instance[\'text\'] = strip_tags( $new_instance[\'text\'] );
$instance[\'code\'] = $new_instance[\'code\'];
return$instance;
}
}

The update() function is where you process the data when a user clicks “Save” on the widget admin. This function should return a variable with all of your settings (in this case the $instance variable). The code is fairly self explanatory here. Just note that the names of the array indexes (the bit in square brackets) matches the names of your inputs in the functions above.

Also remember that it is your responsibility to sanitise user input when you are saving data, hence the use of the strip_tags() function to strip any nasty HTML from our variables.

add_action(\'widgets_init\', create_function(\'\', \'return register_widget(\"Example_Widget\");\'));

Finally we need to “hook” our new widget into WordPress using the add_action() function. I’m not going to explain how WordPress actions work in this article, suffice to say you must include this line for your code to function properly.
Full Code Listing

class Newsletter_Signup_Widget extends WP_Widget
{
// Register the widget
function Newsletter_Signup_Widget() {
// Set some widget options
$widget_options = array( \'description\' => \'Allow users to signup to your newsletter easily\', \'classname\' => \'newsletter-signup\' );
// Set some control options (width, height etc)
$control_options = array( \'width\' => 300 );
// Actually create the widget (widget id, widget name, options...)
$this->WP_Widget( \'newsletter-signup-widget\', \'Newsletter Signup\', $widget_options, $control_options );
}
// Output the content of the widget
function widget($args, $instance) {
extract( $args ); // Don\'t worry about this
// Get our variables
$title = apply_filters( \'widget_title\', $instance[\'title\'] );
$text = $instance[\'text\'];
$code = $instance[\'code\'];
// This is defined when you register a sidebar
echo$before_widget;
// If our title isn\'t empty then show it
if ( $title ) {
echo$before_title . $title . $after_title;
}
// If our description isn\'t empty then show it
if ( $text ) {
echo \'<p>\'. $text .\'</p>\';
}
// If we have some code then output it
if ( $code ) {
echo$code;
}
// This is defined when you register a sidebar
echo$after_widget;
}
// Output the admin options form
function form($instance) {
// These are our default values
$defaults = array( \'title\' => \'Example\', \'text\' => \'Subscribe to our newsletter using the form below.\', \'code\' => \'\' );
// This overwrites any default values with saved values
$instance = wp_parse_args( (array) $instance, $defaults );
?>
<p>
<label for=\"<?php echo $this->get_field_id(\'title\'); ?>\"><?php _e(\'Title:\'); ?></label>
<input id=\"<?php echo $this->get_field_id(\'title\'); ?>\" name=\"<?php echo $this->get_field_name(\'title\'); ?>\" value=\"<?php echo $instance[\'title\']; ?>\" type=\"text\" class=\"widefat\" />
</p>
<p>
<label for=\"<?php echo $this->get_field_id(\'text\'); ?>\"><?php _e(\'Description:\'); ?></label>
<input id=\"<?php echo $this->get_field_id(\'text\'); ?>\" name=\"<?php echo $this->get_field_name(\'text\'); ?>\" value=\"<?php echo $instance[\'text\']; ?>\" type=\"text\" class=\"widefat\" />
</p>
<p>
<label for=\"<?php echo $this->get_field_id(\'code\'); ?>\"><?php _e(\'Code:\'); ?></label>
<textarea id=\"<?php echo $this->get_field_id(\'code\'); ?>\" name=\"<?php echo $this->get_field_name(\'code\'); ?>\" rows=\"10\" class=\"widefat\"><?php echo $instance[\'code\']; ?></textarea>
</p>
<?php
}
// Processes the admin options form when saved
function update($new_instance, $old_instance) {
// Get the old values
$instance = $old_instance;
// Update with any new values (and sanitise input)
$instance[\'title\'] = strip_tags( $new_instance[\'title\'] );
$instance[\'text\'] = strip_tags( $new_instance[\'text\'] );
$instance[\'code\'] = $new_instance[\'code\'];
return$instance;
}
}
add_action(\'widgets_init\', create_function(\'\', \'return register_widget(\"Newsletter_Signup_Widget\");\'));

Over to You

So now that you know how to create custom widgets for your themes in WordPress, let me know how you get on. I hope this article has been easy enough to follow but comprehensive enough to give you a good introduction to creating widgets for WordPress.


温馨提示:本文最后更新于2022/10/20 03:37:23。若文章内容或图片失效,请留言联系站长反馈!
© 版权声明
THE END
点赞0赞赏 分享
评论 抢沙发

请登录后发表评论

    请登录后查看评论内容