因此,我试图在仪表板上制作一个小部件,教会可以在其中粘贴Facebook Live中的iframe嵌入代码,然后将其放在网站的正确页面上。
问题是,我使用Ajax保存小部件选项,但当您将HTML放入文本区域时,它不会保存。我在ajax调用中转储了$\\u帖子,而textarea没有HTML代码的数据。
function add_dashboard_widgets() {
wp_add_dashboard_widget(
\'stream_options\', // $widget_id
\'Stream Settings\', // $widget_name
\'stream_options_widget\' // $callback
);
}
add_action(\'wp_dashboard_setup\', \'add_dashboard_widgets\' );
function stream_options_widget() {
$code = stripslashes( get_option( \'stream_code\' ));
$status = get_option( \'stream_status\' );
$live = $offline = "";
if($status == "1") {
$live = "checked";
} else {
$offline = "checked";
}
echo "
<form action=\'". admin_url( \'admin-post.php\' ) ."\' method=\'post\'>
<div class=\'options_class_wrap\'>
<textarea name=\'stream_code\' id=\'stream_code\' col=\'10\' style=\'width: 100%\' rows=\'5\'>". $code ."</textarea>
</div>
<div class=\'options_class_wrap\'>
<label for=\'live\'><input type=\'radio\' ". $live ." name=\'stream_status\' id=\'live\' value=\'1\' /> Live</label> <label for=\'offline\'><input type=\'radio\' ". $offline ." name=\'stream_status\' id=\'offline\' value=\'0\' /> Offline</label>
</div>
<input type=\'hidden\' name=\'ajax_action\' value=\'stream_options_ajax_call\' />
<input type=\'hidden\' name=\'_ajax_nonce\' value=\'". wp_create_nonce(\'stream_options_ajax_call\')."\' />
<br />
<div class=\'options_buttons\'>
<input type=\'submit\' class=\'button\' value=\'Save\'>
</div>";
}
function stream_options_ajax_call() {
if(!wp_verify_nonce( $_POST[\'_ajax_nonce\'], \'stream_options_ajax_call\' )) {
die(-1);
}
$status = wp_kses($_POST[\'stream_status\'],array() );
update_option( \'stream_code\', stripslashes(wp_filter_post_kses(addslashes($_POST[\'stream_code\']))));
update_option( \'stream_status\', $status );
wp_redirect(\'/wp-admin/index.php\');
exit;
}
function stream_options_ajax_call_init() {
if(isset($_REQUEST[\'ajax_action\']) && $_REQUEST[\'ajax_action\'] === \'stream_options_ajax_call\') {
do_action( \'wp_ajax_stream_options_ajax_call\' );
}
}
if (is_admin()){
add_action(\'wp_ajax_stream_options_ajax_call\', \'stream_options_ajax_call\');
}
add_action( \'init\', \'stream_options_ajax_call_init\');