只需查询引用add_filter()
作用
您可以在WordPress小部件类中使用此函数吗。
Example
我有一个带有动态创建代码的小部件,我想将其传递给插件中创建的另一个函数。这能实现吗?
<?php
/**
* Display the actual Widget
*
* @param Array $args
* @param Array $instance
*/
public function widget($args, $instance){
extract($args);
/* All the widget code. Hidden to save space */
/* Generate unique id if shortcode - used for styling */
$sc_id = mt_rand(1, 100000);
$wcss = \'\';
/* Here is the actual output code that I want to pass to another function I created */
$wcss .= ".awp_archive_widget_sc".$sc_id." { border:".$awp_archives_border_width.\' \'.$awp_archives_border_style.\' \'.$awp_archives_border_color." !important }\\n";
$wcss .= $awp_archives_border_radius == \'round\' ? \'.awp_archive_widget_sc\'.$sc_id.\' { -moz-border-radius:5px; -webkit-border-radius:5px; border-radius:5px }\' : \'\'.\'\'." \\n";
$wcss .= ".awp_archive_widget_sc".$sc_id." li a, .awp_archive_widget_sc".$sc_id." li { font-size:".$awp_archives_font_size." !important }\\n";
$wcss .= ".awp_archive_widget_sc".$sc_id." li a { color:".$awp_archives_link_std." !important }\\n";
$wcss .= ".awp_archive_widget_sc".$sc_id." li a:hover { color:".$awp_archives_link_hover." !important }\\n";
$out = \'\';
/* Rest of widget code - hidden to save space */
echo $out;
}
?>
标记的代码
$wcss
是我要传递给另一个函数的实际代码。
我想我可以在插件构造函数中做类似的事情。
add_filter(\'advanced_widget_pack_css\',array(&$this,\'$this->widget\'));
任何帮助都将不胜感激。
只是不知道如何在widget类中实现这一点。
Additonal Code
该插件允许通过WordPress AJAX创建自定义css文件来定制css样式。
以下是我如何生成自定义AJAX CSS样式表:
Generate WordPress AJAX CSS file
/**
* Enqueue the widgets stylesheet
*/
public function add_awp_css(){
wp_enqueue_style($this->hook, plugins_url(\'advanced-widget-pack/css/advanced-widget-pack.css\' , dirname(__FILE__)), array(), \'\', \'screen\');
wp_enqueue_style(
$this->hook.\'-custom\',
add_query_arg(
array(
\'action\' => \'advanced_widget_pack_gen_css\',
/* Include this to ensure changes don\'t get cached by the browser */
\'layout\' => substr(md5(serialize($this->hook)), 0, 8)
),
site_url()
),
array($this->hook),
$this->version
);
}
这非常有效。
现在,上述排队调用的函数是:
The callback function for the WordPress AJAX CSS file
/**
* Echo the CSS for the custom widgets style
*/
public function advanced_widget_pack_css() {
if(empty($_GET[\'action\']) || $_GET[\'action\'] != \'advanced_widget_pack_gen_css\') return;
if(!isset($_GET[\'ver\'])) return;
$css_text = \'\';
/* ============================= */
/* Archives Widget */
/* ============================= */
if(awp_option(\'archives_style_border\') == \'true\'){
$css_text .= ".awp_archive_widget { border:".awp_option(\'archives_style_border_width\').\' \'.awp_option(\'archives_style_border_style\').\' \'.awp_option(\'archives_style_border_color\')." !important }\\n";
$css_text .= awp_option(\'archives_style_border_radius\') == \'round\' ? \'.awp_archive_widget { -moz-border-radius:5px; -webkit-border-radius:5px; border-radius:5px }\' : \'\'.\'\'." \\n";
}
$css_text .= ".awp_archives_colour { color:".awp_option(\'awp_archives_dropdown_inst_colour\')." !important }\\n";
/**
* Filter the unprocessed CSS array
*/
$css_text = apply_filters(\'advanced_widget_pack_css\', $css_text);
header("Content-type: text/css");
echo $css_text;
exit();
}
上述功能也可以正常工作。它输出一个CSS文件。