我无法让我的wordpress小部件正确发布表单。我在这个网站上听取了建议并研究了这个问题(查看帖子底部的相关链接),看看问题出在哪里。第一个问题是,我不能使用“名称”字段。我需要确保发布的名称是唯一的。如果不是的话,Wordpress会带我到一个非常神秘的“未找到的网站”。如何获得Wordpress的前端部件get/post示例工作在前端和后端?
来弄清这一点,关键是要启动这个例子:
https://www.w3schools.com/php/php_forms.asp
我简直复制并粘贴这个例子到小工具功能,当然取出的HTML头部和尾部的业务。它没有工作,因为它发布到已经存在的“名称”。另外,Wordpress的文件引用不是从插件目录开始的,而是从wordpress目录开始的。所以它最终在提交之后运行的action.php文件的错误位置查找。在发布帖子时仔细查看URL地址栏,以查看该动作是否确实到达正确的位置。最后,我调整了地址,并且所有的工作都像在这个例子中一样。但是,它离开了主要的Wordpress页面。所以我被卡住了。
我放回“”作为操作页面。我尝试了GET方法;这对我并不起作用(我仍然不确定为什么,但我应该调整它并检查出来,因为GET对于此应用程序更可取;它允许使用书签);它结束了(感谢G-D)“post”方法的工作。 (刚才,我将所有出现的“post”改为“get”,将所有“POST”实例改为“GET”,并且按预期工作。我在GET时遇到了URL正在形成的问题,然后我不能访问它。这个问题是我是嵌套PHP,我删除了所有的嵌套的PHP。沿途,我更新了这是不正确设置初始滑块值。
最后它的作品!
启动代码:
public function widget($args, $instance) {
/* include "thiswidget.php"; */ /*disable for now*/
/* Include the widget($args, $instance) function body */
/* include "businesspartnerships.php"; */ /*Original works fine with id update */
static $recommended_ex_no = 3;
static $example_run = 4;
$example_max = 4;
$example_min = 0;
$this_widg_id = $this->id; /* Get the widget ID to be the unique ID Key */
$default_slider_value = 3; /*Default slider value if nothing new has been posted */
if ($example_run > 0) { /*Provide a slider to select examples */
/*See http://foundation.zurb.com/sites/docs/v/5.5.3/components/range_slider.html */
/*See http://www.html5tutorial.info/html5-range.php */
/*See https://www.w3schools.com/php/php_forms.asp */
?><p> Recommended View Example Number: <?php echo $recommended_ex_no ?></p>
<!-- See: http://stackoverflow.com/questions/11788005/how-to-get-fetch-html5-range-sliders-value-in-php -->
<form action="" method="post">
<input type="range" min="<?php echo $example_min;?>" max="<?php echo $example_max;?>" step="1" value="<?php echo $set_slider_value ?>" id="<?php echo $this_widg_id; ?>" name="<?php echo $this_widg_id; ?>" onchange='document.getElementById("text_<?php echo $this_widg_id; ?>").value = "Slider Value = " + document.getElementById("<?php echo $this_widg_id; ?>").value;'/>
<br><center><input type="text" style="text-align: center" name="text_<?php echo $this_widg_id; ?>" id="text_<?php echo $this_widg_id; ?>" value="Slider Value = <?php echo $set_slider_value ?>" disabled /></center>
<br />
<center><input style="text-align: center" type="submit" value="Submit" /></center>
</form>
<?php
$this_widg_id = esc_attr($this->id) ; /* Get the widget ID to be the unique ID Key */
echo "The key is : "; echo esc_attr($this->id); echo "<br><br>";
if(isset($_POST[esc_attr($this->id)])){
echo '<br> isset below get <br>';
echo "<center>The example selected is: ".$_POST[esc_attr($this->id)]." </center>";
// Your Slider value is here, do what you want with it. Mail/Print anything..
$example_run = $_POST[esc_attr($this->id)];
$set_slider_value = $_POST[esc_attr($this->id)]; /*If the value has been posted, then show it.*/
} else {
/* $set_slider_value = $default_slider_value; */ /* Slider value is initially set to default. */
}
}
/* Try https://www.w3schools.com/php/php_forms.asp example here, since form update was not working before */
?>
<!-- <form action="wp-content/plugins/carousel_edit_url_for_newspro/welcome_get.php" method="post"> -->
<form action="" method="post">
Name: <input type="text" name="thisexname"><br>
<!-- E-mail: <input type="text" name="email"><br> -->
<input type="submit">
</form>
<?php
if (isset($_POST[esc_attr("thisexname")])){
echo 'Welcome '; echo $_POST["thisexname"]; echo "<br>";
}
?>
<?php
/*Setup example runs; an example run of 0 does not run! */
if ($example_run > 0) {
echo '<p>Example content being viewed currently is now example number '.$example_run.':';
}
...
中间代码(完整的Widget功能):
public function widget($args, $instance) {
/* include "thiswidget.php"; */ /*disable for now*/
/* Include the widget($args, $instance) function body */
/* include "businesspartnerships.php"; */ /*Original works fine with id update */
static $recommended_ex_no = 3;
static $example_run = 4;
$example_max = 4;
$example_min = 0;
$this_widg_id = $this->id; /* Get the widget ID to be the unique ID Key */
$default_slider_value = 3; /*Default slider value if nothing new has been posted */
if ($example_run > 0) { /*Provide a slider to select examples */
/*See http://foundation.zurb.com/sites/docs/v/5.5.3/components/range_slider.html */
/*See http://www.html5tutorial.info/html5-range.php */
/*See https://www.w3schools.com/php/php_forms.asp */
?><p> Recommended View Example Number: <?php echo $recommended_ex_no ?></p>
<!-- See: http://stackoverflow.com/questions/11788005/how-to-get-fetch-html5-range-sliders-value-in-php -->
<form action="" method="get">
<input type="range" min="<?php echo $example_min;?>" max="<?php echo $example_max;?>" step="1" value="<?php echo $set_slider_value ?>" id="<?php echo $this_widg_id; ?>" name="<?php echo $this_widg_id; ?>" onchange='document.getElementById("text_<?php echo $this_widg_id; ?>").value = "Slider Value = " + document.getElementById("<?php echo $this_widg_id; ?>").value;'/>
<br><center><input type="text" style="text-align: center" name="text_<?php echo $this_widg_id; ?>" id="text_<?php echo $this_widg_id; ?>" value="Slider Value = <?php echo $default_slider_value ?>" disabled /></center>
<br />
<center><input style="text-align: center" type="submit" value="Submit" /></center>
</form>
<?php
$this_widg_id = esc_attr($this->id) ; /* Get the widget ID to be the unique ID Key */
echo "The key is : "; echo esc_attr($this->id); echo "<br><br>";
if(isset($_GET[esc_attr($this->id)])){
echo '<br> isset below get <br>';
echo "<center>The example selected is: ".$_GET[esc_attr($this->id)]." </center>";
// Your Slider value is here, do what you want with it. Mail/Print anything..
$example_run = $_GET[esc_attr($this->id)];
$set_slider_value = $_GET[esc_attr($this->id)]; /*If the value has been posted, then show it.*/
} else {
/* $set_slider_value = $default_slider_value; */ /* Slider value is initially set to default. */
}
}
/* Try https://www.w3schools.com/php/php_forms.asp example here, since form update was not working before */
?>
<!-- <form action="wp-content/plugins/carousel_edit_url_for_newspro/welcome_get.php" method="post"> -->
<form action="" method="get">
Name: <input type="text" name="thisexname"><br>
<!-- E-mail: <input type="text" name="email"><br> -->
<input type="submit">
</form>
<?php
if (isset($_GET[esc_attr("thisexname")])){
echo 'Welcome '; echo $_GET["thisexname"]; echo "<br>";
}
?>
<?php
/*Setup example runs; an example run of 0 does not run! */
if ($example_run > 0) {
echo '<p>Example content being viewed currently is now example number '.$example_run.':';
}
if ($example_run == 1) {
echo '<p>Example Business Partnerships Begins</p>';
include "businesspartnerships.php"; /*full copy*/
echo '<p>Example BP Ends</p>';
}
elseif ($example_run == 2) {
echo '<p>Example for Business Begins</b>';
include "businesscarousel/business_head.php"; /*content before the loop*/
include "businesscarousel/business_loop.php"; /*the loop*/
include "businesscarousel/business_tail.php"; /*content after the loop*/
echo '<p>Example for Business Ends</b>';
}
elseif ($example_run == 3) {
echo '<p>Example for Books Begins</b>';
include "bookscarousel/books.php";
echo '<p>Example for Books Ends</b>';
}
elseif ($example_run == 4) {
echo '<p>Example for Looped Books Begins</b>';
include "bookscarousel/books_head_vars.php";
include "bookscarousel/books_head.php";
include "bookscarousel/books_loop_vars.php";
include "bookscarousel/books_loop.php";
include "bookscarousel/books_tail_vars.php";
include "bookscarousel/books_tail.php";
echo '<p>Example for Looped Books Ends</b>';
}
}
我终于清理代码一点点,取出例子,只是要领运行(证实,这主要工作):
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
* @param array $instance Saved values from database.
*/
public function widget($args, $instance) {
/* include "thiswidget.php"; */ /*disable for now*/
/* Include the widget($args, $instance) function body */
/* include "businesspartnerships.php"; */ /*Original works fine with id update */
$example_run = 4; /*Set to 0 to disable all widget output */
$example_max = 4;
$example_min = 0;
$recommended_ex_no = 4;
$default_slider_value = 4; /*Default slider value if nothing new has been posted */
$this_widg_id = $this->id; /* Get the widget ID to be the unique ID Key */
if ($example_run > 0) { /*Provide a slider to select examples */
?><p> The Recommended Example Number To View is Example Number <?php echo $recommended_ex_no ?>.</p>
<!-- See: http://stackoverflow.com/questions/11788005/how-to-get-fetch-html5-range-sliders-value-in-php -->
<form action="" method="get">
<input type="range" min="<?php echo $example_min;?>" max="<?php echo $example_max;?>" step="1" value="<?php echo $set_slider_value ?>" id="<?php echo $this_widg_id; ?>" name="<?php echo $this_widg_id; ?>" onchange='document.getElementById("text_<?php echo $this_widg_id; ?>").value = "Slider Value = " + document.getElementById("<?php echo $this_widg_id; ?>").value;'/>
<br><center><input type="text" style="text-align: center" name="text_<?php echo $this_widg_id; ?>" id="text_<?php echo $this_widg_id; ?>" value="Slider Value = <?php echo $default_slider_value ?>" disabled /></center>
<br />
<center><input style="text-align: center" type="submit" value="Submit Slider Value" /></center>
</form>
<?php
$this_widg_id = esc_attr($this->id) ; /* Get the widget ID to be the unique ID Key */
echo '<br><br><center><p style="text-align: center">This example widget instance is entitled, "';
echo esc_attr($this->id); echo '".</p></center>';
if(isset($_GET[esc_attr($this->id)])){
/* echo "<center>The example selected is: ".$_GET[esc_attr($this->id)]." </center>"; */ /*Activate to debug */
// Your Slider value is here, do what you want with it. Mail/Print anything..
$example_run = $_GET[esc_attr($this->id)];
$set_slider_value = $_GET[esc_attr($this->id)]; /*If the value has been posted, then show it.*/
} else {
$set_slider_value = $default_slider_value; /* Slider value is initially set to default. */
}
} /*End if example is >0 */
/* If not working, adapt https://www.w3schools.com/php/php_forms.asp example here, since form update was not working before */
/*Setup example runs; an example run of 0 does not run! */
if ($example_run > 0) {
echo '<center><p style="text-align: center">The example content being viewed currently is now example number '.$example_run.':</p>';
}
if ($example_run == 1) {
echo '<p>Example Business Partnerships Begins</p>';
include "businesspartnerships.php"; /*full copy*/
echo '<p>Example BP Ends</p>';
}
elseif ($example_run == 2) {
echo '<p>Example for Business Begins</b>';
include "businesscarousel/business_head.php"; /*content before the loop*/
include "businesscarousel/business_loop.php"; /*the loop*/
include "businesscarousel/business_tail.php"; /*content after the loop*/
echo '<p>Example for Business Ends</b>';
}
elseif ($example_run == 3) {
echo '<p>Example for Books Begins</b>';
include "bookscarousel/books.php";
echo '<p>Example for Books Ends</b>';
}
elseif ($example_run == 4) {
echo '<p>Example for Looped Books Begins</b>';
include "bookscarousel/books_head_vars.php";
include "bookscarousel/books_head.php";
include "bookscarousel/books_loop_vars.php";
include "bookscarousel/books_loop.php";
include "bookscarousel/books_tail_vars.php";
include "bookscarousel/books_tail.php";
echo '<p>Example for Looped Books Ends</b>';
}
}
此外,我必须确保双引号“在没有使用更好的'引用不引用'的地方使用。
有什么建议可以做得更好?
问题的下一部分:这是如何在管理屏幕的小部件外观部分的后端完成的?
我对后端功能的代码是(两部分):
public function form($instance) {
/*
$title - site title
$url_config - private key to website or local file for include
$category_name_textbox - name of the category used for the widget in the textbox
$category_name - name of the category used for the widget
$posts_to_show - number of posts to show
*/
$title = ! empty($instance['title']) ? $instance['title'] : esc_html__('New title', 'text_domain');
$url_config = ! empty($instance['url_config']) ? $instance['url_config'] : esc_html__('Display All', 'text_domain');
$category_name_textbox = ! empty($instance['category_name_textbox']) ? $instance['category_name_textbox'] : esc_html__('uncategorized', 'text_domain');
$category_name = ! empty($instance['$category_name']) ? $instance['$category_name'] : esc_html__('uncategorized', 'text_domain');
$posts_to_show = ! empty($instance['$posts_to_show']) ? $instance['posts_to_show'] : esc_html__('4', 'text_domain');
include "form_html.php";
}
的包括“form_html.php”包括接近结束“公共职能的形式(例如$)”使代码更具可读性并突出问题。它的内容在这里:
<p>
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_attr_e('Title:', 'text_domain'); ?></label>
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($title); ?>">
</p><label for="<?php echo esc_attr($this->get_field_id('url_config')); ?>"><?php esc_attr_e('URL Config:', 'text_domain'); ?></label>
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('url_config')); ?>" name="<?php echo esc_attr($this->get_field_name('url_config')); ?>" type="text" value="<?php echo esc_attr($url_config); ?>">
</p>
</p><label for="<?php echo esc_attr($this->get_field_id('category_name_textbox')); ?>"><?php esc_attr_e('Category Name:', 'text_domain'); ?></label>
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('category_name_textbox')); ?>" name="<?php echo esc_attr($this->get_field_name('category_name_textbox')); ?>" type="text" value="<?php echo esc_attr($category_name_textbox); ?>">
</p>
<p>
<label><?php _e('Category', 'newsmag-pro'); ?> :</label>
<select name="<?php echo esc_attr($this->get_field_name('newsmag_category')); ?>"
id="<?php echo esc_attr($this->get_field_id('newsmag_category')); ?>">
<option value="" <?php if (empty($instance['newsmag_category'])) {
echo 'selected="selected"';
} ?>><?php _e('– Select a category –', 'newsmag-pro') ?></option>
<?php
$categories = get_categories('hide_empty=0');
foreach ($categories as $category) { ?>
<option
value="<?php echo esc_attr($category->slug); ?>" <?php selected(esc_attr($category->slug), $instance['newsmag_category']); ?>><?php echo esc_attr($category->cat_name); ?></option>
<?php } ?>
</select>
</p>
<p>Selected Category: <? echo $instance['newsmag_category'] ?></p>
<label class="block" for="input_<?php echo esc_attr($this->get_field_id('show_post')); ?>">
<span class="customize-control-title">
<?php _e('Posts to Show', 'newsmag-pro'); ?> :
</span>
</label>
<input type="text" name="<?php echo esc_attr($this->get_field_name('show_post')); ?>" class="rl-slider"
id="input_<?php echo esc_attr($this->get_field_id('show_post')); ?>"
value="<?php echo esc_attr($instance['show_post']); ?>" />
<div id="slider_<?php echo esc_attr($this->get_field_id('show_post')) ?>" data-attr-min="4"
data-attr-max="12" data-attr-step="1" class="ss-slider"></div>
<script>
jQuery(document).ready(function ($) {
$('[id="slider_<?php echo esc_attr($this->get_field_id('show_post')); ?>"]').slider({
value: <?php echo esc_attr($instance['show_post']); ?>,
range: 'min',
min : 4,
max : 12,
step : 1,
slide: function (event, ui) {
$('[id="input_<?php echo esc_attr($this->get_field_id('show_post')); ?>"]').val(ui.value).keyup();
}
});
$('[id="input_<?php echo esc_attr($this->get_field_id('show_post')) ?>"]').on('focus', function(){
$('[id="input_<?php echo esc_attr($this->get_field_id('show_post')) ?>"]').trigger('blur');
});
$('[id="input_<?php echo esc_attr($this->get_field_id('show_post')) ?>"]').val($('[id="slider_<?php echo esc_attr($this->get_field_id('show_post')) ?>"]').slider("value"));
$('[id="input_<?php echo esc_attr($this->get_field_id('show_post')) ?>"]').change(function() {
$('[id="slider_<?php echo esc_attr($this->get_field_id('show_post')) ?>"]').slider({
value: $(this).val()
});
});
});
</script>
<?php
那么后端出了什么问题?我可以选择下拉菜单,但不会更新任何内容。我可以调整滑动条,但之后我不能再引用它的值来保存它的值。任何想法如何解决这个问题的后端网站管理方面?
因为我只是注册,所以引用被限制为两个。所以,你可以找到更完整的参考名单:
顺便说一句,如果你去到主网站:
http://www.newyorkbusinessreview.com/
主要网站目前重定向而尚在建设中来:
http://www.newyorkbusinessreview.com/wordpress/
我可能会禁用插件前端,以便在我仍然在处理问题时不关闭网站。
哇......这是一个巨大的代码墙......修改这个问题的任何改变,直到问题的核心? 。请参见[如何创建最小,完整和可验证示例](http://stackoverflow.com/help/mcve) – brasofilo