如何使用多實例小部件選項創建 WordPress 插件以供側邊欄或頁腳使用 – 準備使用 WordPress 插件代碼
已發表: 2016-05-11WordPress 小部件多個實例/WordPress 自定義小部件
Crunchify 在過去幾年中創建了多個 WordPress 插件。 我們對插件 Facebook 成員的要求之一是擁有同一個小部件的multiple instance
,因為用戶可能希望 Facebook 頁面插件進入他們的Sidebar
和Footer
部分。
在過去幾年與眾多客戶合作時,我們創建了很多帶有多實例小部件選項的簡單插件。
在本教程中,我們將Sample ready-to-use WordPress Plugin
代碼,該代碼會將實時Facebook Page Plugin
小部件放入您博客的側邊欄和/或頁腳。 隨意將此代碼用於您的插件。
讓我們開始吧:
第1步
我通常在 Eclipse IDE 中進行所有 Java 和 WordPress 開發。 因此,按照教程將 PHP 開發工具包設置到 Eclipse 中。
在crunchify-plugin
文件夾下創建文件crunchify-plugin.php
並放入以下代碼。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
<?php /* * Plugin Name: Crunchify Plugin * Plugin URI: https://crunchify.com/ * Description: The Simplest Ready to use WordPress Plugin with Multiple Instance of the same Widgets option - by Crunchify * Version: 1.0 * Author: Crunchify * Author URI: https://crunchify.com * Text Domain: crunchify-plugin */ class crunchify_widget extends WP_Widget { // Create Multiple WordPress Widgets function __construct ( ) { parent : : __construct ( 'crunchify_widget' , __ ( 'Crunchify Plugin' , 'crunchify_plugin_domain' ) , array ( 'description' = > __ ( 'Sample WordPress Plugin by Crunchify' , 'crunchify_plugin_domain' ) ) ) ; } // This function creates nice Facebook Page Like box in Header or Footer public function widget ( $args , $instance ) { $crunchify_facebook_url = apply_filters ( 'crunchify_facebook_url' , $instance [ 'crunchify_facebook_url' ] ) ; $crunchify_facebook_hidecover = $instance [ 'crunchify_facebook_hidecover' ] ? 'true' : 'false' ; $crunchify_facebook_showface = $instance [ 'crunchify_facebook_showface' ] ? 'true' : 'false' ; $facebookpage = '<div><iframe src="//www.facebook.com/plugins/page.php?href=' . $crunchify_facebook_url . '&tabs=&width=300&height=220&small_header=false&adapt_container_width=true&hide_cover=' . $crunchify_facebook_hidecover . '&show_facepile=' . $crunchify_facebook_showface . '&show_border=false&header=false" scrolling="no" frameborder="0" allowTransparency="true"></iframe></div>' ; echo $before_widget ; echo $before_title . $after_title ; echo $facebookpage ; echo $after_widget ; } // Create Instance and Assign Values public function form ( $instance ) { if ( isset ( $instance [ 'crunchify_facebook_url' ] ) ) { $crunchify_facebook_url = $instance [ 'crunchify_facebook_url' ] ; } else { $crunchify_facebook_url = __ ( 'https://facebook.com/Crunchify' , 'crunchify_plugin_domain' ) ; } $instance [ 'crunchify_facebook_hidecover' ] = $instance [ 'crunchify_facebook_hidecover' ] ? 'true' : 'false' ; $instance [ 'crunchify_facebook_showface' ] = $instance [ 'crunchify_facebook_showface' ] ? 'true' : 'false' ; ?> < ! -- This is Crunchify Widget Form -- > < p > < label for = " <?php echo $this - > get_field_id ( 'crunchify_facebook_url' ) ; ?> " > <?php _e ( 'Facebook Page URL' ) ; ?> < / label > < input class = "widefat" id = " <?php echo $this - > get_field_id ( 'crunchify_facebook_url' ) ; ?> " name = " <?php echo $this - > get_field_name ( 'crunchify_facebook_url' ) ; ?> " type = "text" value = " <?php echo esc_attr ( $crunchify_facebook_url ) ; ?> " / > < br / > < br / > < label for = " <?php echo $this - > get_field_id ( 'crunchify_facebook_hidecover' ) ; ?> " > <?php _e ( 'Hide Cover?' ) ; ?> < / label > < input class = "checkbox" type = "checkbox" <?php checked ( $instance [ 'crunchify_facebook_hidecover' ] , 'true' ) ; ?> id = " <?php echo $this - > get_field_id ( 'crunchify_facebook_hidecover' ) ; ?> " name = " <?php echo $this - > get_field_name ( 'crunchify_facebook_hidecover' ) ; ?> " / > < br / > < br / > < label for = " <?php echo $this - > get_field_id ( 'crunchify_facebook_showface' ) ; ?> " > <?php _e ( 'Show Facepile?' ) ; ?> < / label > < input class = "checkbox" type = "checkbox" <?php checked ( $instance [ 'crunchify_facebook_showface' ] , 'true' ) ; ?> id = " <?php echo $this - > get_field_id ( 'crunchify_facebook_showface' ) ; ?> " name = " <?php echo $this - > get_field_name ( 'crunchify_facebook_showface' ) ; ?> " / > < / p > <?php } // Updating widget replacing old instances with new function update ( $new_instance , $old_instance ) { $instance = array ( ) ; $instance [ 'crunchify_facebook_url' ] = ( ! empty ( $new_instance [ 'crunchify_facebook_url' ] ) ) ? strip_tags ( $new_instance [ 'crunchify_facebook_url' ] ) : '' ; $instance [ 'crunchify_facebook_hidecover' ] = $new_instance [ 'crunchify_facebook_hidecover' ] ; $instance [ 'crunchify_facebook_showface' ] = $new_instance [ 'crunchify_facebook_showface' ] ; return $instance ; } } function crunchify_plugin ( ) { register_widget ( 'crunchify_widget' ) ; } // Initialize Plugin add_action ( 'widgets_init' , 'crunchify_plugin' ) ; ?> |

讓我們從上面的代碼中理解 4 個突出顯示的行:
- 函數 __construct()
- 使用名稱和描述初始化 WordPress 插件
- 公共功能小部件($args,$instance)
- 這是您的博客上顯示的內容 - 輸出小部件的內容
- 公共函數形式($instance)
- 這是您在
Appearance
->Widgets
中指定 WordPress Widget 選項的地方
- 這是您在
- 函數更新($new_instance,$old_instance)
- 當您單擊保存按鈕時 - 選項值將被保存
NOTE:
widgets_init
操作將調用您的函數crunchify_plugin
,該函數在內部registers
名稱為crunchify_widget
的小部件。
第2步
在同一個crunchify-plugin
文件夾下創建readme.txt
文件,內容如下。 隨時根據您的需要更新它。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
=== Crunchify Plugin === Contributors : Crunchify Tags : WordPress , WordPress Plugins , Crunchify , multiple widgets Requires at least : 4.3 Tested up to : 4.5.2 Stable tag : 1.0 The simplest ready to use WordPress Plugin with Multiple Instance of the same Widgets option - by Crunchify . com == Description == Sample WordPress Plugin which creates Multiple Widgets for your plugin . [ Feedback ] ( https : //crunchify.com/) | [Twitter](https://twitter.com/Crunchify) == Installation == 1. Unpack the ` download - package ` . 2. Upload the file to the ` / wp - content / plugins / ` directory . 3. Activate the plugin through the ` Plugins ` menu in WordPress . 4. Go to Appearance - > Widgets Section - > Select Plugin ` Crunchify Plugin ` . 5. Done and Ready . == Frequently Asked Questions == = FAQ1 = * Your Answer1 . = Where do I get latest updates on plugin ? = * On < a href = "http://twitter.com/Crunchify" target = "_blank" > Twitter < / a > and < a href = "http://www.facebook.com/Crunchify" target = "_blank" > Facebook < / a > . == Screenshots == 1. Screenshot1 details 2. Screenshot2 details == Changelog == = 1.0 = * Initial Working Version |
第三步
只是為了確保您的文件夾結構應如下所示:
將crunchify-plugin
文件夾放在plugins
文件夾下。
第 4 步如何啟用?
- 轉到您網站的
Admin Panel
- 點擊
Plugins
-
Activate
插件 Crunchify Plugin
第 5 步設置插件選項
- 點擊
Appearance -> Widgets
- 選擇 Widget
Crunchify Plugin
並將其放在Sidebar
或Footer
腳下 - 更新選項並點擊
save
Step-6 如何驗證?
我放置了兩個具有不同選項的小部件。
您應該會看到multiple instances of same Crunchify Plugin
它們會根據您的設置在側邊欄或頁腳中發出漂亮的Facebook Page Plugin
。