So I'm trying to make a widget on the dashboard where this church can paste the iframe embed code from Facebook Live and then it puts that on the correct page on the site.
The problem is that I'm using Ajax to save the widget options but when you put HTML into the textarea, it does not save. I dumped the $_POST on ajax call, and the textarea has no data if its HTML code.
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');
stream_options_ajax_call()
to thewp_ajax_
hook in this code. Does that not exist in your full code either? And I don't see any JS for submitting with AJAX.admin_enqueue_scripts
will load on the dashboard. But is there a particular reason you're using a Dashboard widget? A customiser field, or even a settings page, would be much easier to set up.