Convert & back to ampersand

2019-05-28 03:02发布

I'm using wordpress, and for the website that I'm working on, I've set up a custom post type for listing each of their services and details.

On their custom contact form, I'm querying the custom post type to get the titles for each post into an option list so a user can select which service they're interested in.

Part of the form logic when the form is submitted checks to see that all of the required fields are filled in, and if not, it gives an alert and asks the user to fill in the needed info.

I have a piece of php code in each input to echo the posted data back into the form so that it is not lost, and I'm trying to do the same for the option drop down but I'm running into a really frustrating issue:

Any wordpress post title with and ampersand in it is having the ampersand replaced by a char code of &#038, which is fine, it shows as an & in my browser, but after it's submitted it turns into an & in the post data.

When I try to compare each post title to the submitted selection, the if statement never finds a match because the &#038 and & don't match up.

I've tried using htmlspecialchars_decode() on the titles from wordpress, but this doesn't decode the &#038 back into and &.

Does anyone know of a function to accomplish this and decode any weird char encoding that might pop up?

Here's my while loop where this takes place.

while ($custom_post_type->have_posts()) : $custom_post_type->the_post();

//set selected if submission is bouncing back                                       
if($_POST['ftype'] == get_the_title())
{
    $the_selected = ' selected="selected" ';
}
else
{
    $the_selected = '';
}

?>

    <option value="<?php echo get_the_title(); ?>" <?php echo $the_selected; ?>><?php echo get_the_title(); ?></option>

<?php


endwhile;

标签: php wordpress
2条回答
看我几分像从前
2楼-- · 2019-05-28 03:24

html_entity_decode should work.

echo html_entity_decode('&#038;');
查看更多
对你真心纯属浪费
3楼-- · 2019-05-28 03:43

There's a plugin for WordPress that can help you:
https://wordpress.org/plugins/wp-no-format/

The idea of this plugin is to let you stop WordPress from formatting your HTML in specific parts of your posts.

It's very simple to use, you just have to write:

<!-- noformat on -->

on the WordPress Editor, just before the part you want to protect, and from that point on, WordPress won't touch the HTML code you write. If you want to resume the standard "formatting" you have to type:

<!-- noformat off -->
查看更多
登录 后发表回答