我無法讓我的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