【发布时间】:2021-09-11 01:58:53
【问题描述】:
我正在创建一个使用 AJAX 更新页面内容的前端编辑器。
它通过使用 JS 进行更新,以获取 HTML 组件的块并保存到页面内容,包裹在 <!-- wp:html --> 标记中。
该功能仅在用户登录并选择前端编辑器选项(自定义主题选项)时可用。
AJAX 代码是:
function scedPage(post_id) {
var pageComs = ["<!-- wp:html -->"];
jQuery('section component').each(function () {
$this0 = jQuery(this)[0].outerHTML;
pageComs.push($this0);
});
pageComs.push("<!-- /wp:html -->");
pageHtml = pageComs.join(' ');
console.log(pageHtml);
jQuery.ajax({
type: "POST",
url: ajax_object.ajaxurl,
async: true,
data: {
action: 'sced_page',
post_content: pageHtml,
postId: post_id
},
success: function (data) {
location.reload();
},
error: function (error) {
console.log(error)
}
});
};
PHP 是:
function sced_page() {
$dev_sced = get_option('scale_opt_field2');
if (current_user_can('editor') || current_user_can('administrator')) {
if ($dev_sced !== "1") {
} else if ($dev_sced == "1") {
$post_id = $_POST['postId'];
$post_content = $_POST['post_content'];
$the_post = array();
$the_post['ID'] = $post_id;
$the_post['post_content'] = $post_content;
$post_id = wp_update_post($the_post);
wp_die();
add_action( 'wp_ajax_sced_page', 'sced_page' );
}
}
};
发布大块未转义/未过滤的 HTML 是否会造成问题? 我意识到我需要在其中添加随机数。
感谢您的帮助。
【问题讨论】:
标签: javascript php ajax wordpress