Skip to content

Instantly share code, notes, and snippets.

@iamfaisal
Created August 16, 2018 19:42
Show Gist options
  • Save iamfaisal/b1d917727815902f21b2a2a35bc78c4a to your computer and use it in GitHub Desktop.
Save iamfaisal/b1d917727815902f21b2a2a35bc78c4a to your computer and use it in GitHub Desktop.
dynamic keyword insertion
<?php
add_shortcode("dki", "dynamic_content");
/**
* Usage: [dki key="city" default="London"]
* Scenario 1: "city = Manchester" exists in the URL, function returns "Manchester"
* Scenario 2: "city" does not appear in the UR, return "London"
*/
function dynamic_content($attributes) {
$default = "";
$return = "";
$key = FALSE;
if (!empty( $attributes['key'])) {
$key = sanitize_text_field($attributes['key']);
}
if (!empty($attributes['default'])) {
$default = sanitize_text_field($attributes['default']);
}
parse_str($_SERVER["QUERY_STRING"], $url_array);
if ($key != FALSE && array_key_exists($key, $url_array)) {
$return = sanitize_text_field($url_array[$key]);
}
else $return = $default;
return esc_html($return);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment