Cum sa iti faci propiul widget pentru wordpress.(Tutorial)

In acest tutorial o sa invatam impreuna cum se face propiul widget pentru tema/site-ul care ruleaza pe wordpress.Sa nu credeti ca eu sunt vreun doctor docent in php nici pe departe dar putem sa invatam impreuna asta e si ideea.

Atunci cand vrei sa adaugi reclama pe blogul personal cum e in cazul meu in sidebar sau oriunde permite tema widgeturi in mod normal se face drag end drop in widgets a textului/html si il introduci acolo unde vrei.Complectarea se face neaparat prin introducerea unor coduri html obligatorii.

LINKUL POZEI TALENumele dorit

Dar pentru a face lucrurile mult mai simple si sa nu mai stai mereu sa scrii coduri html ar fi mai bine daca am avea un widget propiu pentru reclame si sa il folosim oride cate ori avem nevoie.

Conform codexului sunt cateva coduri care trebuiesc construite.Creati un fisier cu numele widget-add-260.php.Asadar iata codurile pentru widgetul nostru.

[codesyntax lang=”php” container=”div”]

/***********************************************************************************************/
/* Widget pentru reclama 260x120 */
/***********************************************************************************************/</code>

class Ad_260_Widget extends WP_Widget {

public function __construct() {
parent::__construct(
'adaptive_ad_260_w',
'Custom Widget: 260x120 Ad »',
array('description' =&gt; __('Displays a single 260x120 ad block', 'adaptive-framework'))
);
}

public function form($instance) {
$defaults = array(
'title' =&gt; __('Ad', 'adaptive-framework'),
'ad_img' =&gt; IMAGES . '',
'ad_link' =&gt; 'http://www.example.com'
);

$instance = wp_parse_args((array) $instance, $defaults);

?&gt;
<!-- The Title -->

<label for="&lt;?php echo $this-&gt;get_field_id('title') ?&gt;"><!--?php _e('Title:', 'adaptive-framework'); ?--></label>
<input class="widefat" id="&lt;?php echo $this-&gt;get_field_id('title'); ?&gt;" type="text" name="&lt;?php echo $this-&gt;get_field_name('title'); ?&gt;" value="&lt;?php echo esc_attr($instance['title']); ?&gt;" />

<!-- Ad Image -->

<label for="&lt;?php echo $this-&gt;get_field_id('ad_img') ?&gt;"><!--?php _e('Ad image URL:', 'adaptive-framework'); ?--></label>
<input class="widefat" id="&lt;?php echo $this-&gt;get_field_id('ad_img'); ?&gt;" type="text" name="&lt;?php echo $this-&gt;get_field_name('ad_img'); ?&gt;" value="&lt;?php echo $instance['ad_img']; ?&gt;" />

<!-- Ad Link -->

<label for="&lt;?php echo $this-&gt;get_field_id('ad_link') ?&gt;"><!--?php _e('Ad link:', 'adaptive-framework'); ?--></label>
<input class="widefat" id="&lt;?php echo $this-&gt;get_field_id('ad_link'); ?&gt;" type="text" name="&lt;?php echo $this-&gt;get_field_name('ad_link'); ?&gt;" value="&lt;?php echo $instance['ad_link']; ?&gt;" />

<!--?php <br ?--> }

public function update($new_instance, $old_instance) {
$instance = $old_instance;

// The Title
$instance['title'] = strip_tags($new_instance['title']);

// The Ad
$instance['ad_img'] = $new_instance['ad_img'];
$instance['ad_link'] = $new_instance['ad_link'];

return $instance;
}

public function widget($args, $instance) {
extract($args);

// Get the title and prepare it for display
$title = apply_filters('widget_title', $instance['title']);

// Get the ad
$ad_img = $instance['ad_img'];
$ad_link = $instance['ad_link'];

echo $before_widget;

if ($title) {
echo $before_title . $title . $after_title;
}

echo '
<ul class="ad-260">';</ul>
if ($ad_img) : ?&gt;
<ul>
	<li><figure class="ad-block"><a href="&lt;?php echo $ad_link; ?&gt;"><img alt="Ad 260" src="&lt;?php echo $ad_img; ?&gt;" /></a></figure></li>
</ul>
<!--?php endif; </p-->

echo '

';
echo $after_widget;
}
}

register_widget('Ad_260_Widget');

[/codesyntax]

Introducetil in tema voastra via ftp.

Acum el ca sa fie si functionabil trebuie chemat in functions.php al temei introduceti acest cod

[codesyntax lang=”php” container=”div”]

require_once('widget-ad-260.php');

[/codesyntax]

Si iata ca avem primul widget creeat pentru adaugare de reclame mult mai simplu prin adaugarea widgetului oriunde doriti prin drag en drop.

Daca e ceva ce nu va iese sunt aici va stau la dispozitie sa va ajut.

un comentariu la “Cum sa iti faci propiul widget pentru wordpress.(Tutorial)

Lasă un răspuns

Adresa ta de email nu va fi publicată. Câmpurile obligatorii sunt marcate cu *

Alte articole Populare