我遇到了我自定义的WordPress窗口小部件的问题。出于某种奇怪的原因,表单函数会运行两次,因此会生成两个Redactor textareas,但只有在将该小部件添加到侧栏时才会发生。不是当我添加完成后点击保存按钮。Wordpress窗体小部件窗体函数运行两次
我该怎么做,我的表单函数只能运行一次,或者至少让JavaScript运行一次。
全部部件代码
我的表单功能
function form($instance)
{
$instance = wp_parse_args((array) $instance, array('box1' => '', 'box2' => '', 'box3' => '', 'box4' => ''));
$box1 = $instance['box1'];
$box2 = $instance['box2'];
$box3 = $instance['box3'];
$box4 = $instance['box4'];
$boxqty = $instance['boxqty'];
$uniqueID = rand();
echo "Unique ID: ".$uniqueID;
?>
<p>
<label for="<?php echo $this->get_field_id('boxqty'); ?>">Number of blocks: (1-4)
<input class="widefat" id="<?php echo $this->get_field_id('boxqty'); ?>" name="<?php echo $this->get_field_name('boxqty'); ?>" type="text" value="<?php echo attribute_escape($boxqty); ?>" />
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('box1'); ?>">Box 1:
<textarea class="widefat redactor_content_<?php echo $uniqueID; ?>" id="box1" name="<?php echo $this->get_field_name('box1'); ?>" style="width: 600px;"><?php echo $box1; ?></textarea>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('box2'); ?>">Box 2:
<textarea class="widefat redactor_content_<?php echo $uniqueID; ?>" id="<?php echo $this->get_field_id('box2'); ?>" name="<?php echo $this->get_field_name('box2'); ?>" style="width: 600px;"><?php echo $box2; ?></textarea>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('box3'); ?>">Box 3:
<textarea class="widefat redactor_content_<?php echo $uniqueID; ?>" id="<?php echo $this->get_field_id('box3'); ?>" name="<?php echo $this->get_field_name('box3'); ?>" style="width: 600px;"><?php echo $box3; ?></textarea>
</label>
</p>
<p>
<label for="<?php echo $this->get_field_id('box4'); ?>">Box 4:
<textarea class="widefat redactor_content_<?php echo $uniqueID; ?>" id="<?php echo $this->get_field_id('box4'); ?>" name="<?php echo $this->get_field_name('box4'); ?>" style="width: 600px;"><?php echo $box4; ?></textarea>
</label>
</p>
<!--Activate Redactor-->
<script type="text/javascript">
jQuery('.redactor_content_<?php echo $uniqueID; ?>').redactor({
fixed: true
});
console.log('Unique ID: <?php echo $uniqueID; ?>');
</script>
<?php
}
“......或者至少让JavaScript运行一次” - 所以,问题是Javascript运行两次,而不是'form'函数本身? – 2013-03-13 13:34:13
我有一些php代码testet的表单函数确实运行两次。 – dasmikko 2013-03-13 13:36:17