我找到了一种将JavaScript变量传递给PHP变量的简单方法。请注意,此方法在WordPress版本4.7.2中有效,并且仅在小部件上有效。我写了很多评论来解释每一行的作用。如果您有更好的解决方案,请与我们分享!
Solution:
创建一个隐藏的输入字段来存储要传递的javascript的值访问隐藏的输入字段并将其值赋给PHP变量
Demo Widget:
<我创建了一个演示小部件,根据你按“添加爱你”按钮的次数添加“爱你”一词。
请注意,为了更好地理解,我留下了显示的隐藏字段。
您可以更改type="text"
到type="hidden"
隐藏它。
本演示仅侧重于form
小部件的功能。
确保单击保存按钮,否则小部件不会保存隐藏输入的值。
Demo Widget ScreenShot:
Source code:
wp文本转发器。php<?php
/**
*Plugin Name: WP Text Reapter
**/
class wp_text_repeater extends WP_Widget {
/**
* Sets up the widgets name etc
*/
public function __construct() {
$widget_ops = array(
\'classname\' => \'wp_text_repeater\',
\'description\' => \'Widget that prints LOVE YOU repeatedly according to button press\',
);
parent::__construct( \'wp_text_repeater\', \'WP Text Repeater Widget\', $widget_ops );
}
/**
* Outputs the content of the widget
*
* @param array $args
* @param array $instance
*/
public function widget( $args, $instance ) {
// outputs the content of the widget
$wp_text_repeater_button = ! empty( $instance[\'wp_text_repeater_button\'] ) ? $instance[\'wp_text_repeater_button\'] : \'\';
$wp_text_repeater_appendee = ! empty( $instance[\'wp_text_repeater_appendee\'] ) ? $instance[\'wp_text_repeater_appendee\'] : \'\';
$wp_text_repeater_hidden = ! empty( $instance[\'wp_text_repeater_hidden\'] ) ? $instance[\'wp_text_repeater_hidden\'] : \'\';
}
/**
* Outputs the options form on admin
*
* @param array $instance The widget options
*/
public function form( $instance ) {
// outputs the options form on admin
$instance = wp_parse_args( (array) $instance, array( \'wp_text_repeater_button\' => \'\', \'wp_text_repeater_appendee\' => \'\', \'wp_text_repeater_hidden\' => \'\'));
$wp_text_repeater_button = $instance[\'wp_text_repeater_button\'];
$wp_text_repeater_appendee = $instance[\'wp_text_repeater_appendee\'];
$wp_text_repeater_hidden = $instance[\'wp_text_repeater_hidden\'];
$tempHidden = \'wp_text_repeater_hidden\';
?>
<!-- Hidden field that store number of time user press the button -->
<input
class="widefat"
id="<?php echo $this->get_field_id($tempHidden); ?>"
name="<?php echo $this->get_field_name($tempHidden); ?>"
type="text"
value="<?php echo esc_attr($$tempHidden);?>"/>
<?php
$max = 0; //Number of time user press the button
//if JavaScript front-end hidden input has value, assign $max to it.
//This If statement sync between the javascript and the php part.
if(strlen($$tempHidden) > 0){
$max = intval($$tempHidden);
}
$counter = 0;
while($counter < $max){ //loop according to how many time user press the button
?>
<p>LOVE YOU!</p>
<?php
$counter++;
}
$id_prefix = $this->get_field_id(\'\'); //get the widget prefix id.
?>
<!-- You can append all your content herev-->
<span id="<?php echo $this->get_field_id(\'wp_text_repeater_appendee\')?>"></span>
<!-- Add button that call jQery function to add "LOVE YOU" word -->
<input style="background-color: #08a538; color:white; height: 27px;"
class="button widefat"
type="button"
id="<?php echo $this->get_field_id(\'wp_text_repeater_button\'); ?>"
value="Add LOVE YOU"
onclick="text_repeater.addLove(\'<?php echo $this->id;?>\', \'<?php echo $id_prefix;?>\'); return false;"
/>
<script>
jQuery(document).ready(function($){
var preIndexID;
var numberOfLove = <?php echo $max; ?>; //grab value from the php in order to sync between the front and back end.
text_repeater = {
addLove :function(widget_id, widget_id_string){
preIndexID = widget_id_string; //get the correct pre-index of the widget.
numberOfLove++;
numberOfLove = numberOfLove.toString(); //convert int to string for the hidden input field.
$("#" + preIndexID + "wp_text_repeater_hidden").val(numberOfLove); //change the value of the hidden input field.
$("#" + preIndexID + "wp_text_repeater_appendee").append(\'<p>LOVE YOU!</p>\'); //live update the front-end with "LOVE YOU".
}
}
});
</script>
<?php
}
/**
* Processing widget options on save
*
* @param array $new_instance The new options
* @param array $old_instance The previous options
*/
public function update( $new_instance, $old_instance ) {
// processes widget options to be saved
$instance = $old_instance;
$instance[\'wp_text_repeater_button\'] = sanitize_text_field($new_instance[\'wp_text_repeater_button\']);
$instance[\'wp_text_repeater_appendee\'] = sanitize_text_field ($new_instance[\'wp_text_repeater_appendee\']);
$instance[\'wp_text_repeater_hidden\'] = sanitize_text_field( $new_instance[\'wp_text_repeater_hidden\'] );
return $instance;
}
}
// register wp_text_repeater widget
function register_wp_text_repeater_widget() {
register_widget( \'wp_text_repeater\' );
}
add_action( \'widgets_init\', \'register_wp_text_repeater_widget\' );