WordPress.org

Ready to get started?Download WordPress

Forums

Writing Widget but remove_action not working (3 posts)

  1. atgheb
    Member
    Posted 4 years ago #

    Hi,
    I have a rss widget that I'm writing.
    I want to give the user the option to customize the link color, background color etc by entering it in the widget options.

    I dont want any inline css so this is how I tackled it.

    In the constructor I do:

    class CustomRSSFeedsAggregatorTicker extends WP_Widget{
    			var $bgColor = "#FFFFFF";
                            var $linkColor = "#0000FF";
    			var $descWordLimit = 15; //max number of words to allow in the description.
    			var $maxRssEntries = 10; //max number of feeds to get from each rss URI.
    
    			function CustomRSSFeedsAggregatorTicker(){
    				$widget_ops = array('classname' => 'custom_rss_feeds', 'description' => __( "Custom RSS Feeds Aggregator Widget") );
    
    				$this->WP_Widget('ticker', __('RSS Feeds Aggregator Ticker'), $widget_ops);
    
    				add_action('wp_head', array(&$this,'addStyle'), 1);
    				add_action('wp_footer', array(&$this,'addScript'), 1);
    
    			}

    In my addStyle function:

    function addStyle(){
    				$height = $this->height;
    				$width = $this->width;
    
    				echo '<link type="text/css" rel="stylesheet" href="' . get_bloginfo('wpurl') . '/wp-content/plugins/custom-rss-feeds-aggregator-ticker/css/rss-aggregator-ticker.php?version=1.0.1'
    				. '&bgcolor=' . $this->bgColor
    				. '&linkcolor=' .  $this->linkColor
    				. '" />' . "\n";
    			}

    Then when the widget options are updated, in the update() method I update this variabled bgColor and linkColor:

    $this->bgColor = $instance['bgcolor'];
    $this->linkColor = $instance['linkcolor'];
    remove_action('wp_head',array(&$this, 'addStyle'), 1);
    add_action('wp_head', array(&$this,'addStyle'), 1);

    The hope is that in the update method, i remove the old css include and replace it with a new one that include the new params...

    only problem is that the remove doesn't seem to happen, when I look at the page, I still see the old css include.

    Can anyone help?

  2. atgheb
    Member
    Posted 4 years ago #

    Anyone?
    I've noticed that it's probably not a case of remove_action not working, instead, seems that remove_action and add_action simply dont work in the update or widget function.

    Can anyone recommend how I can dynamically add something to the header from a widget? A simpler example would be if I wanted the title of the widget included as a comment in the header and I wanted that comment to be updated when someone update the title of the widget.

    Thanks in advance.

  3. atgheb
    Member
    Posted 4 years ago #

    Solved the problem... to make a long story short... dont use the 2.8 API, use the older API unless you need multiple instances.

    Regards,
    Ali
    --
    [sig moderated per forum rules]

Topic Closed

This topic has been closed to new replies.

About this Topic