Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Widgets Duplicate WordPress



I have created a custom widget to add adverts onto my WordPress website, which works fine.

I am now looking to dynamically duplicate this code so that you can choose the widget more than once, any ideas how to do this?

I can copy and paste this and add a number at the end but this gives me a lot of code as I want this widget 5-8 times, so is there a simple way of having it be used more than once?

My code so far is:

function textadvertWidget()
$settings = get_option("widget_textadvertwidget");

$adverttext1 = $settings['adverttext1'];
$adverttexturl1 = $settings['adverttexturl1'];
$adverttext2 = $settings['adverttext2'];
$adverttexturl2 = $settings['adverttexturl2'];
$adverttext3 = $settings['adverttext3'];
$adverttexturl3 = $settings['adverttexturl3'];




<?php if ($adverttext1=="" && $adverttexturl1=="") { } else { ?>
<li><a href="<?php echo $adverttexturl1; ?>" target="_blank"><?php echo $adverttext1; ?></a></li>
<?php } ?>
<?php if ($adverttext2=="" && $adverttexturl2=="") { } else { ?>
<li><a href="<?php echo $adverttexturl2; ?>" target="_blank"><?php echo $adverttext2; ?></a></li>
<?php } ?>
<?php if ($adverttext3=="" && $adverttexturl3=="") { } else { ?>
<li><a href="<?php echo $adverttexturl3; ?>" target="_blank"><?php echo $adverttext3; ?></a></li>
<?php } ?>


function textadvertWidgetAdmin() {

$settings = get_option("widget_textadvertwidget");

// check if anything's been sent
if (isset($_POST['update_textadvert'])) {
$settings['adverttext1'] = strip_tags(stripslashes($_POST['adverttext1_admin']));
$settings['adverttexturl1'] = strip_tags(stripslashes($_POST['adverttexturl1_admin']));
$settings['adverttext2'] = strip_tags(stripslashes($_POST['adverttext2_admin']));
$settings['adverttexturl2'] = strip_tags(stripslashes($_POST['adverttexturl2_admin']));
$settings['adverttext3'] = strip_tags(stripslashes($_POST['adverttext3_admin']));
$settings['adverttexturl3'] = strip_tags(stripslashes($_POST['adverttexturl3_admin']));


echo '<p>
<label for="adverttext1_admin">Text Advert 1:
<input id="adverttext1_admin" name="adverttext1_admin" type="text" class="widefat" value="'.$settings['adverttext1'].'" /></label></p>';
echo '<p>
<label for="adverttexturl1_admin">Text Advert URL 1:
<input id="adverttexturl1_admin" name="adverttexturl1_admin" type="text" class="widefat" value="'.$settings['adverttexturl1'].'" /></label></p>';
echo '<input type="hidden" id="update_textadvert" name="update_textadvert" value="1" />';
echo '<p>
<label for="adverttext2_admin">Text Advert 2:
<input id="adverttext2_admin" name="adverttext2_admin" type="text" class="widefat" value="'.$settings['adverttext2'].'" /></label></p>';
echo '<p>
<label for="adverttexturl2_admin">Text Advert URL 2:
<input id="adverttexturl2_admin" name="adverttexturl2_admin" type="text" class="widefat" value="'.$settings['adverttexturl2'].'" /></label></p>';
echo '<p>
<label for="adverttext3_admin">Text Advert 3:
<input id="adverttext3_admin" name="adverttext3_admin" type="text" class="widefat" value="'.$settings['adverttext3'].'" /></label></p>';
echo '<p>
<label for="adverttexturl3_admin">Text Advert URL 3:
<input id="adverttexturl3_admin" name="adverttexturl3_admin" type="text" class="widefat" value="'.$settings['adverttexturl3'].'" /></label></p>';


register_sidebar_widget('Clicky Media - Text Advert', 'textadvertWidget');
register_widget_control('Clicky Media - Text Advert', 'textadvertWidgetAdmin', 400, 200);

Any ideas?

Answers (3)


Ram Kumar answers:

hi craigfarrall,i recommend some wonderful plugins for advertise management .

<strong><strong></strong></strong>[[LINK href=""]]wp-125[[/LINK]]

[[LINK href=""]]Buy Sell Ads[[/LINK]]

[[LINK href=""]]Wp insert[[/LINK]

[[LINK href=""]]Adsense[[/LINK]]

[[LINK href=""]]Ad Rotate[[/LINK]]

these are the top level plugins.i hope you'll get solution:)


Ivaylo Draganov answers:

The code that you are using to register your widget is obsolete. You should create the widget by extending the base widget class in WP's core. And that way it would be possible to use the widget infinite number of times. Check this tutorial and sample code to get you started:
[[LINK href=""]][[/LINK]]

And you can also save the settings inside the widget itself without using the wp_options table.

craigfarrall comments:

I did read up that it was obsolete but it still seemed to work so I just kind of left it.

I have looked at the link provided to create the widget an infinite number of times but I can't see how to do that, do you have any sample code of how to do this?

Ivaylo Draganov comments:

You can download the complete widget code from that tutorial:
[[LINK href=""]][[/LINK]]

It is extensively documented and should give you a good starting point if you are familiar with WP's PHP.

I'm looking at your current code, trying to understand what's the goal of the widget. Why are there three advert fields in your current code?

craigfarrall comments:

I have three fields because I want there to be three adverts on the sidebar, so obviously for one advert I would like the advert URL and also the advert link.

I will have a look at that code provided, but if anyone can amend my code slightly to be duplicated that would be great.


Ivaylo Draganov comments:

Hm, your ad code doesn't seem complicated - just plain links. So why don't you just use regular text widgets? You can paste the HTML code inside the text widget.


moviezman answers:

Hi, don't reinvent the wheel :)

In the widget section, choose Text widget and paste your ad code .
For example:
<script type="text/javascript"><!--
and place it where you want.

You can repeat the operation .

You can use also ad plugins that are well done.