我在帖子中创建了一个带有新元盒的插件。php。它本身工作得很好,但问题是如果用户选择在WordPress中按“发布/更新/保存”,它将保存其值。
我尝试将JQuery函数添加到#publish
项目
这种方式是可行的,但它会带来一个恼人的问题:“您确定要离开此页面吗?”window.location.reload()
我不能在那里。
$("#publish").click(function(e) {
//e.preventDefault();
// SM Data
var data = prepareData();
if(data.length !== 0) {
for(var i = 0; i < data.length; i++ ) {
$.post( postdataAjax.ajaxurl, data[i] );
}
}
// WP Data
$.ajax({
url: "post.php",
type: "POST",
data: $("form#post").serialize(),
success: function(response,status) {
window.location.reload();
}
});
return true;
});
对于此最终解决方案,我尝试使用
window.onbeforeunload = null
诸如此类,但没有取得任何成功。
这是全部功能:
private function __construct() {
add_action( \'add_meta_boxes\', array( $this, \'add_custom_metabox\' ) );
}
public function add_custom_metabox()
{
$screens = array( \'post\', \'page\', \'landing\' );
foreach ( $screens as $screen ) {
$this->set_plugin_slug();
add_meta_box(
\'_meta\',
__( \'###\', $this->plugin_slug ),
function( $post ) {
global $slug;
$html_code = _HTML_Code::get_instance();
echo $html_code->generate_script_new_publication($post, $slug);
echo $html_code->generate_script_list($post, $slug);
}, $screen
);
}
}
...
public function generate_script_list($post, $slug)
{
global $wpdb;
$mainTable = $wpdb->prefix.\'esm\';
$data = $wpdb->get_results("SELECT * FROM $mainTable WHERE post_id = $post->ID ORDER BY publish_localtime ASC");
$d_ids = array();
foreach( $data as $item ) {
$d_ids[] = $item->id;
}
ob_start();
?>
<script type="text/javascript">
(function ( $ ) {
"use strict";
$(function () {
$(document).ready(function() {
var $data_ids = <?php echo json_encode($d_ids); ?>;
function prepareData()
{
var $changed = parseInt($("#new-changed-new").val());
var data = [];
if( $changed == 1 ) {
$data_ids.push("new");
}
for(var i = 0; i < $data_ids.length; i++) {
var item_id = $data_ids[i];
var ch = parseInt($("#new-changed-"+item_id).val());
if(ch) {
var $selected_weekdays = \'\';
$("input[name=weekday-"+item_id+"]:checked").each(
function() {
$selected_weekdays += $(this).val()+\';\';
}
);
var action = "update";
if(item_id == "new") {
action = "create";
}
var d = new Date($("#one-time-date-"+item_id).datepicker("getDate"));
var d_local = new Date(Date.UTC(d.getFullYear(), d.getMonth(), d.getDate(), $("#hour-"+item_id).val(), $("#minute-"+item_id).val(), 0));
var d_utc = new Date(d.getFullYear(), d.getMonth(), d.getDate(), $("#hour-"+item_id).val(), $("#minute-"+item_id).val(), 0);
//var d_from = new Date($range_from_year.val(), $range_from_month.val()-1, $range_from_day.val(), $hour.val(), $minute.val(), 0);
//var d_to = new Date($range_to_year.val(), $range_to_month.val()-1, $range_to_day.val(), $hour.val(), $minute.val(), 0);
var item = [
{ \'name\': "_wpnonce", \'value\' : postdataAjax.nonce },
{ \'name\' : "action", \'value\' : action },
{ \'name\' : "item-id", \'value\' : $("#item-id-"+item_id).val() },
{ \'name\' : "post-id", \'value\' : $("#post-id-"+item_id).val() },
{ \'name\' : "channel", \'value\' : $("#channel-"+item_id).val() },
{ \'name\' : "text", \'value\' : $("#text-"+item_id).val() },
{ \'name\' : "image_url", \'value\' : $("#filepath-"+item_id).val() },
{ \'name\' : "local_time", \'value\' : Math.round(d_local.getTime() / 1000), \'real\' : d_local.toJSON() },
{ \'name\' : "one_time_utc_time", \'value\' : Math.round(d_utc.getTime() / 1000), \'real\' : d_utc.toJSON() },
//{ \'name\' : "range_from_utc_time", \'value\' : Math.round(d_from.getTime() / 1000), \'real\' : d_from.toJSON() },
//{ \'name\' : "range_to_utc_time", \'value\' : Math.round(d_to.getTime() / 1000), \'real\' : d_to.toJSON() },
{ \'name\' : "pattern", \'value\' : $("#pattern-"+item_id).val() },
{ \'name\' : "daily-pattern", \'value\' : $("input[name=daily-pattern]:checked").val() },
{ \'name\' : "daily-pattern-specified", \'value\' : $("#daily-event-"+item_id+"-spec-day").val() },
{ \'name\' : "weekly-pattern-specified", \'value\' : $("#weekly-event-"+item_id+"-spec").val() },
{ \'name\' : "weekday", \'value\' : $selected_weekdays },
{ \'name\' : "monthly-pattern", \'value\' : $("input[name=monthly-pattern]:checked").val() },
{ \'name\' : "monthly-pattern-1-day", \'value\' : $("#monthly-event-"+item_id+"-pattern-1-day").val() },
{ \'name\' : "monthly-pattern-1-month", \'value\' : $("#monthly-event-"+item_id+"-pattern-1-month").val() },
{ \'name\' : "monthly-pattern-2-select-num", \'value\' : $("#monthly-event-"+item_id+"-pattern-2-select-num").val() },
{ \'name\' : "monthly-pattern-2-select-weekday", \'value\' : $("#monthly-event-"+item_id+"-pattern-2-select-weekday").val() },
{ \'name\' : "monthly-pattern-2-month", \'value\' : $("#monthly-event-"+item_id+"-pattern-2-month").val() },
{ \'name\' : "yearly-pattern", \'value\' : $("input[name=yearly-pattern]:checked").val() },
{ \'name\' : "yearly-pattern-frequence", \'value\' : $("#yearly-event-"+item_id+"-pattern-year").val() },
{ \'name\' : "yearly-pattern-1-day", \'value\' : $("#yearly-event-"+item_id+"-pattern-1-day").val() },
{ \'name\' : "yearly-pattern-1-month", \'value\' : $("#yearly-event-"+item_id+"-pattern-1-month").val() },
{ \'name\' : "yearly-select-num", \'value\' : $("#yearly-event-"+item_id+"-pattern-2-select-num").val() },
{ \'name\' : "yearly-select-weekday", \'value\' : $("#yearly-event-"+item_id+"-pattern-2-select-weekday").val() },
{ \'name\' : "yearly-pattern-2-month", \'value\' : $("#yearly-event-"+item_id+"-pattern-2-month").val() }
];
switch( $("#channel-"+item_id).val() ) {
case \'Facebook\' :
item.push({ \'name\' : "publish_account", \'value\' : $("input[name=facebook-publish-"+item_id+"]:checked").val() });
break;
case \'Twitter\' :
item.push({ \'name\' : "publish_account", \'value\' : $("input[name=twitter-publish-"+item_id+"]:checked").val() });
break;
default :
break;
}
data.push(item);
}
}
return data;
}
$("form#post").submit(function(e) {
//e.preventDefault();
var data = prepareData();
if(data.length !== 0) {
for(var i = 0; i < data.length; i++ ) {
$.post( postdataAjax.ajaxurl, data[i] );
}
}
/*$.ajax({
url: "post.php",
type: "POST",
data: $("form#post").serialize(),
success: function(response,status) {
//window.location.reload();
}
});*/
return true;
});
});
});
})(jQuery);
</script>
<style>
.sm-table {
width: 100%;
}
</style>
<table id="sm-table" class="sm-table">
<thead>
<tr>
<th style="width:2%;text-align:center"></th>
<th style="width:10%;text-align:left;"><?php _e(\'Channel\', $slug); ?></th>
<th style="width:43%;text-align:left;"><?php _e(\'Text\', $slug); ?></th>
<th style="width:30%;text-align:right;"><?php _e(\'Publish Date\', $slug); ?></th>
<th style="width:15%;text-align:right;"><?php _e(\'Actions\', $slug); ?></th>
</tr>
</thead>
<tbody>
<?php if( count($data) !== 0 ) : ?>
<?php $i = 0;
foreach($data as $itemObj) :
$item = (array) $itemObj;
$i++;
?>
<?php echo $this->generate_script_list_row($post, $item, $i, $slug); ?>
<?php echo $this->generate_script_publication($post, $item, $slug); ?>
<?php endforeach; ?>
<?php else : ?>
<tr>
<td colspan="5"><?php _e(\'No publishing options found.\', $slug); ?></td>
</tr>
<?php endif; ?>
</tbody>
</table>
<?php
$out = ob_get_contents();
ob_end_clean();
return $out;
}
所以我的问题是,我想做的是可以实现的吗?我该怎么做?对现有函数的编辑还是完全重写?