Reputation: 1154
Im trying to make my own widget. But i found out that this is not so easy. I'm trying to set default values but i have no idea how to do this.
Yes, i have Googled a lot. I'm almost trying for 1 week to get my own options page and custom widgets, but im not succeeding.
so back to my question, this is my code now:
class Superdeal_Widget extends WP_Widget {
public function __construct()
{
parent::__construct(
'Superdeal-widget',
'Superdeal Widget',
array(
'description' => 'Superdeal widget'
),
array (
'width' => 400,
'height' => 350
)
);
}
public function widget( $args, $instance )
{
// basic output just for this example
echo '<p><a href="'.$instance['url'].'">'.$instance['titel'].' '.$instance['superdeal'].'</a></p>';
}
public function form( $instance )
{
// removed the for loop, you can create new instances of the widget instead
?>
<p>
<label for="<?php echo $this->get_field_id('titel'); ?>">Titel</label><br />
<input type="text" name="<?php echo $this->get_field_name('titel'); ?>" id="<?php echo $this->get_field_id('titel'); ?>-title" value="<?php echo $instance['titel']; ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id('url'); ?>">Url</label><br />
<input type="text" name="<?php echo $this->get_field_name('url'); ?>" id="<?php echo $this->get_field_id('url'); ?>-url" value="<?php echo $instance['url']; ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id('superdeal'); ?>">Superdeal tekst</label><br />
<input type="text" name="<?php echo $this->get_field_name('superdeal'); ?>" id="<?php echo $this->get_field_id('superdeal'); ?>-title" value="<?php echo $instance['superdeal']; ?>" class="widefat" />
</p>
<?php
}
}
// end class
// init the widget
add_action( 'widgets_init', create_function('', 'return register_widget("Superdeal_Widget");') );
PS: if you know a good tutorial which shows how to make you own options page / declare properties / makeing custom widgets, i would love to hear from you. Because all the tutorials i have followd are old, obscure or way too complicated.
Thank you guys!
Upvotes: 0
Views: 2826
Reputation: 2136
This should help you → widget tutorial
Note on this line from form()
function:
$instance = wp_parse_args( (array) $instance, array( 'title' => 'DEFAULT_VALUE_HERE' ) );
You can add several instances like:
array( 'title' => 'DEFAULT_VALUE_HERE', 'name' => 'DEFAULT_VALUE_HERE')
Hope this would help you going.
Upvotes: 5