作成中のカスタム Wordpress ウィジェットに問題があります。何らかの奇妙な理由で、フォーム関数が 2 回実行されるため、2 つの Redactor テキストエリアが作成されますが、ウィジェットをサイドバーに追加した場合にのみ発生します。追加後に保存ボタンをクリックしたときではありません。
フォーム関数を 1 回だけ実行するか、少なくとも JavaScript を 1 回だけ実行するにはどうすればよいですか。
完全なウィジェット コード
マイフォーム機能
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
}