2017-04-04 61 views

回答

0

这是一个我可以帮助你的通用小部件模板。您只需填写缺失的部分(html,css和js文件)

<?php 
/* 
Plugin Name: General Widget 
Plugin URI: 
Description: 
Author: 
Version: 1 
Author URI: 
*/ 
// register widget 
add_action('widgets_init', create_function('', 'return register_widget("General_Widget");')); 

class General_Widget extends WP_Widget { 

// constructor 
    function General_Widget() { 
     // Give widget name here 
     parent::WP_Widget(false, $name = __('General Widget', 'wp_widget_plugin')); 

     // link the style sheet for the widget 
     add_action('wp_enqueue_scripts', array($this,'add_styles')); 
     // link javascript file 
     add_action('wp_enqueue_scripts', array($this,'add_scripts')); 
     // register the shortcode 
     // TODO: 
     add_shortcode('INSERT SHORTCODE HERE', array($this, 'shortcode')); 
    } 

    // function that enqueues the necessary javascript files 
    function add_scripts() {  
    // TODO:  
     // NOTE this is risky, might mess with your theme. 
     wp_deregister_script('jquery'); // we will use our own versions of jquery 
     wp_deregister_script('jquery-migrate'); 
     global $post; 
     // only register and add scripts for this widget if the widget is active or if the post has a shortcode 
     if(has_shortcode($post->post_content, 'INSERT SHORTCODE HERE') || is_active_widget(false, false, $this->id_base, true)) { 
      // register the different scripts 
      wp_register_script('jqueryMin', plugins_url('js/lib/jquery-2.2.4.min.js', __FILE__), array(), false, false); // https://developer.wordpress.org/reference/functions/wp_register_script/ 

      // add them to document 
      wp_enqueue_script('jqueryMin'); 
     } 
    } 

    // enqueues the stylesheet 
    function add_styles() { 
     // TODO: 
     global $post; 
     if(has_shortcode($post->post_content, 'INSERT SHORTCODE HERE') || is_active_widget(false, false, $this->id_base, true)) { 
      // register the different style sheets 
      wp_register_style('bootstrapMinStyle', plugins_url('css/lib/bootstrap.min.css', __FILE__), array(), false, "all"); // https://codex.wordpress.org/Function_Reference/wp_register_style 
      wp_register_style('commonStyle', plugins_url('css/common.css', __FILE__), array(), false, "all"); // https://codex.wordpress.org/Function_Reference/wp_register_style 
      $arrCoreStyles = array('bootstrapMinStyle', 'commonStyle'); 

      wp_register_style('quoteStyle', plugins_url('css/quote.css', __FILE__),$arrCoreStyles, false, "all"); // https://codex.wordpress.org/Function_Reference/wp_register_style 
      // add style sheet and all of its dependencies 
      wp_enqueue_style('quoteStyle'); 
     } 
    } 

// widget form creation. This handles the form that the wordpress admin sees when they add the widget to a widget area 
    function form($instance) { 
    } 

    // updates the data stored in the widget when wordpress admin adds widget 
    function update($new_instance, $old_instance) { 
    } 

    // gives the widget shortcode functionality 
    function shortcode($atts) { 
     $args = shortcode_atts(array(), $atts); 
     $this->add_to_args($args); 
     $this->display($args); 
    } 

// displays the widget to the client. 
    function widget($args, $instance) { 
     $this->add_to_args($args); 
     $this->display($args); 
    } 

// use this to add fields to args before we display 
    private function add_to_args(& $args) { 
     // TODO: 
     $args['customWidgetHTML'] = "html/NAME OF HTML TEMPLATE.html"; 
    } 

// this is an additional function, not inherited from WP_Widget. It is used for displaying html to 
// client. Is called by both widget() and shortcode() 
    private function display($args) { 
     extract($args); 
     // TODO: 
     wp_enqueue_script('quoteJS'); // we dont want this in the header so we just enqueue it here 
     echo $before_widget; 
     include(plugin_dir_path(__FILE__) . $customWidgetHTML); 
     echo $after_widget; 
    } 
} 
?> 
相关问题