I am currently creating a custom WordPress theme. A couple of the archive pages will include hidden forms (represented by pictures). Each of these forms represent a post. When a user clicks on a form, a GET request is run and the browser retrieves the content of the specific post.
Each hidden form takes the title of the post as the value (as a query string like this: /lokaler/?lokal=Studion ('Studion' being the title, 'lokaler' being the post type)). This works fine and I get the information needed, no problem.
The problem arises when I want to do the same with a title that contains Ä. The query /lokaler/?lokal=Sammanträdesrummet (obviously) doesn't work, but I can't seem to find how to work around it... I am using a combination of JavaScript and PHP on this page.
How would I be able to work around it so that the GET request works but I don't have to change the title of the post?
//how the form is created:
<form method="GET" id="lokal_<?php echo get_the_title(); ?>" class="single_lokal_container single_lokal_lokal">
<input type="hidden" name="lokal" value="<?php echo get_the_title(); ?>">
etc...
//how post is queried
$current_lokal_from_uri = htmlspecialchars($_GET['lokal']);
$args3 = array(
'name' => $current_lokal_from_uri,
'post_type' => 'lokaler'
);
You should be able to pass these through GET parameters fine. Just be sure to encode and decode consistently:
var plain = 'åäö';
var urlencoded = encodeURIComponent(plain);
var unencoded = decodeURIComponent(urlencoded);
console.log('plain', plain);// plain åäö
console.log('urlencoded', urlencoded);// urlencoded %C3%A5%C3%A4%C3%B6
console.log('unencoded', unencoded);// unencoded åäö
echo urldecode('%C3%A5%C3%A4%C3%B6'); // åäö
Bear in mind that HTMLSpecialChars is for encoding characters to html-safe flavours not parameter encoding (eg &
becomes &
and <
becomes $lt;
)
I have managed to solve the problem.
I used urlencode and decode for the value of the form. The next problem arose when trying to make a wp_query from the decoded value. At the end, it was a simple fix: i had to replace the ä from the decoded value with an a:
$current_lokal_from_uri_decoded = urldecode($current_lokal_from_uri);
if(strpos($current_lokal_from_uri_decoded, 'ä') !== false) {
$current_lokal_from_uri_decoded = str_replace('ä', 'a', $current_lokal_from_uri_decoded);
}
$args3 = array(
'name' => $current_lokal_from_uri_decoded,
'post_type' => 'lokaler'
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.