Php Wordpress小部件表单函数运行两次

Php Wordpress小部件表单函数运行两次,php,html,wordpress,Php,Html,Wordpress,我正在制作的自定义Wordpress小部件有问题。出于某种奇怪的原因,表单函数运行了两次,因此生成了两个Redactor文本区域,但只有在我将小部件添加到侧栏时才会发生。当我在添加后单击“保存”按钮时,情况并非如此 我如何做到这一点?我的表单函数只运行一次,或者至少让JavaScript运行一次 完整小部件代码 我的表单功能 function form($instance) { $instance = wp_parse_args((array) $instance, array( 'b

我正在制作的自定义Wordpress小部件有问题。出于某种奇怪的原因,表单函数运行了两次,因此生成了两个Redactor文本区域,但只有在我将小部件添加到侧栏时才会发生。当我在添加后单击“保存”按钮时,情况并非如此

我如何做到这一点?我的表单函数只运行一次,或者至少让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
}
函数形式($instance)
{
$instance=wp_parse_参数((数组)$instance,数组('box1'=>'','box2'=>'','box3'=>'','box4'=>'');
$box1=$instance['box1'];
$box2=$instance['box2'];
$box3=$instance['box3'];
$box4=$instance['box4'];
$boxqty=$instance['boxqty'];
$uniqueID=rand();
回显“唯一ID:”.$uniqueID;
?>        


我测试了一些东西,似乎正在发生的事情是,整个小部件都加载在左侧(小部件表单内容隐藏的地方)和右侧的“活动”小部件部分。未测试,但您应该能够仅使用以下内容来定位“活动”部分:

jQuery('.widgets-holder-wrap .redactor_content_<?php echo $uniqueID; ?>').redactor({
    fixed: true
});
jQuery('.widgets holder wrap.redactor\u content').redactor({
修正:正确
});

如果我有完整的小部件,我可能会发现其他东西。

“…或者至少让JavaScript运行一次”--所以,问题是JavaScript运行两次,而不是
表单
函数本身?我用一些php代码测试了表单函数确实运行了两次。