Support » Plugins » How Do I Pass Widget Data To __construct?

  • Hello,

    I read in order to use add_filter() in my widget I must place it in __construct(). How do I pass widget data that I can use inside my function inside of __construct()? Sorry, fairly new to objects.

    Example is as follows.

    class MyWidget extends WP_Widget {
    	function __construct() {
    		parent::WP_Widget ( /* Base ID */'mywidget', /* Name */'My Widget', array( 'description' => 'This is going to be awesome!' ) );
    		function myContent($content) {
    			$page = get_page_by_title('My Page');
    			if ( is_page($page->ID) ) ?>
    				<p>Markup goes <?php echo $instant['widgetoption']; ?></p>
    			return $content;
    		add_filter('the_content', 'myContent');
    	function widget($args, $instant) {

    I want to be able to echo $instant[‘widgetoption’] in __construct() so that my add_filter works. I understand using get_option(‘widget_’) works, but with multiwidget they are nested arrays. So I’d have to do like

    $widget_options = get_options('widget_mywidget');
    echo $widget_options[5]['widgetoption'];

    Again, fairly new to objects, what should I do? 🙁

  • The topic ‘How Do I Pass Widget Data To __construct?’ is closed to new replies.