I am currently developing my first WordPress plugin. A few days ago I submitted it to WordPress for review. Unfortunately, the plugin was not (yet) published, because I still have to close some security issues. More specifically, it is about the fact that data must be Sanitized, Escaped, and Validated.
Unfortunately, I completely forgot about this point during development. Now I have familiarized myself with the above principles via the documentation and I think I have understood them now, however I am still unsure how to apply this practically in some places.
1. Highlight the active menu item
To highlight the active menu item I have written a small helper function:
protected function get_current_site() {
$page = $_GET['page'];
return $page;
}
The function is used in the header:
<li <?php if($this->get_current_site() == 'myplugin-settings') { echo 'class="active"'; } ?>><a href="?page=myplugin-settings">Settings</a></li>
How do I apply the three principles (Sanitized, Escaped, and Validated) here?
2. Toggle status via AJAX
To change the status of my plugin I use a small function via AJAX. Here $_POST["active"] contains either 0 or 1:
$this->loader->add_action('wp_ajax_toggle_myplugin', $this, 'toggle_myplugin');
public function toggle_myplugin() {
$active = $_POST["active"];
$status = update_option('myplugin_status', $active);
echo $status;
wp_die();
}
How do I apply the three principles (Sanitized, Escaped, and Validated) here?
3. Dynamic rendering of a template
I use a GET variable to output a different template on a specific page of my plugin:
public function render_template() {
if(isset($_GET['action'])) {
$action = $_GET['action'];
switch($action) {
case("edit"):
include MYPLUGIN_ROOT_PATH . 'includes/admin/templates/template1.php';
break;
case("add"):
include MYPLUGIN_ROOT_PATH . 'includes/admin/templates/template2.php';
break;
default:
include MYPLUGIN_ROOT_PATH . 'includes/admin/templates/template3.php';
}
}
}
How do I check the action
variable?
4. Check value from an hidden input-field
I use hidden input-field to pass hidden values (in my case an ID):
<input type="hidden" name="id" value="<?php echo $id; ?>">
public function actions() {
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$action = $_POST["action"];
switch($action) {
case "edit":
$id = $_POST["id"];
$name = $_POST["name"];
[...]
break;
case "add":
$name = $_POST["name"];
[...]
break;
}
}
Thank you very much in advance. Please be gracious as this is my first plugin.