【问题标题】:Wordpress widget form function runs twiceWordpress 小部件表单功能运行两次
【发布时间】:2013-03-01 09:27:09
【问题描述】:

我正在制作的自定义 Wordpress 小部件出现问题。由于某种奇怪的原因,表单函数运行了两次,因此产生了两个 Redactor 文本区域,但它仅在我将小部件添加到侧边栏时才会发生。添加后点击保存按钮时不会。

我怎样才能让我的表单函数只运行一次,或者至少让 JavaScript 运行一次。

完整的小部件代码

http://pastebin.com/BHDJSxWX

我的表单功能

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 函数本身?
  • 我有一些php代码的testet,表单函数确实运行了两次。

标签: php html wordpress


【解决方案1】:

我测试了一些东西,似乎正在发生的事情是,整个小部件既加载在左侧,小部件表单内容被隐藏,又加载在右侧的“活动”小部件部分。未经测试,但您应该只能通过以下方式定位“活动”部分:

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

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

【讨论】:

    猜你喜欢
    • 2021-11-22
    • 2019-06-29
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    相关资源
    最近更新 更多