2

Just have a query reference the add_filter() function.

Can you use this function inside a WordPress widget class.

Example

I have a widget with dynamic created code that I want to pass to another function created within the plugin. Can this be achieved?

<?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;
}
?>

The code marked $wcss is the actual code that I want to pass to another function.

I thought I could do something like this from within the plugin constructor.

add_filter('advanced_widget_pack_css',array(&$this,'$this->widget'));

Any help would be greatly appreciated.

Just not sure how to achieve this from within the widget class.

Additonal Code

The plugin allows for custom css styles through creating a custom CSS file through WordPress AJAX.

Here is how I generate the custom AJAX CSS stylesheet:

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
    );  
}

This works greats.

Now the function that the above enqueue calls is this:

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();
}

This above function also works fine. It outputs a CSS file.

  • I think you are misunderstanding how filters work. See: wordpress.stackexchange.com/a/103644/21376 – s_ha_dum Sep 24 '13 at 7:28
  • @s_ha_dum Do you know what would be the best way to pass the variable $wcss and it's contents to the function named 'advanced_widget_pack_css'? – Jason Sep 25 '13 at 1:40
  • I have been thinking about it. Your biggest problem is that the widget method runs after the <head> of the document. If not for that this would not be hard. – s_ha_dum Sep 25 '13 at 2:07
  • @s_ha_dum My whole goal here is to simply pass the dynamically generated CSS code from the widget class to another function that I created. I will append the above code snippet to show a WP AJAX function that I created that creates a dynamic CSS file. I could simply echo the $wcss variable code out to the page and it will display properly, however the plugin has 15 widgets and this would end up with very messy code. Therefore I would like to append any 'extra' generated CSS code onto the original AJAX generated CSS file. – Jason Sep 25 '13 at 2:18
0
<?php
/**
 * Display the actual Widget
 *
 * @param Array $args
 * @param Array $instance
 */
public function widget($args, $instance){
    …
    $wcss = apply_filters( 'my-filter-name', $wcss );
    …
}
?>

To create your own filter hook just use the function "apply_filters" then as you mentioned add a function in your constructor i.e.

function __constructor()
{
    …
    add_filter( 'my-filter-name', array( &$this, 'my_function_name' );
    …
}

Be sure your function returns something

function my_function_name( $css )
{
    // Do whatever you want
    return $css;
}
  • Thanks for the help. Should the my_function_name() function still be contained within the widget class? I tried by adding the function within the widget class and passing the $wcss data to it. $this->my_function_name($wcss);. It doesn't seem to pass the data though. – Jason Sep 25 '13 at 1:31
  • Depends on the 'add_filter( 'my-filter-name', array( &$this, 'my_function_name' );' part. As I used $this it is delivered with the class. But you could also hook into it from your function.php file. Double check if your function is actually called. – Horttcore Sep 27 '13 at 8:58
  • I have the function setup within the widget, however it doesn't seem to pass any PHP code. If I try and pass a static variable such as return "test" it passes that. It doesn't pass the code from higher up in the widget though. Thanks for your help, I appreciate it. – Jason Sep 28 '13 at 0:36
0

First do apply_filters( 'your-filter-name', $wcss, $this ) into widget function. I put $this as third parameter as you might other widget data for your function.

Then do

add_filter( 'your-filter-name', 'your-functin-name', 10, 2 );
function your-functin-name( $css, $data ) {
    // do your stuff
}

where ever you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.