htmlspecialchars

Using htmlspecialchars dynamic title as a parameter for a shortcode


I am using post title as a shortcode parameter but it does not completely work clean when a page title has & in it. 'Communications & Media Coordinator' .

I am trying to do htmlspecialchars but it still outputs 'Communications' only.

$jobtitlestr = htmlspecialchars($jobtitle);

echo do_shortcode('[gravityform id="1" field_values="job_title=' . $jobtitlestr . '&job_location=' . $locationtitle . '&list_count='" ajax="true" tabindex="1"]');

Thanks


Solution

  • Assuming this shortcode is part of a GET query, the '&' is used as a separator between terms and explains why your title is getting cut off.

    In PHP, htmlspecialchars translates certain characters to their equivalent HTML entities. The '&' character will become '&' so will still provide a problem if you're using it as part of a URI.

    I suspect the function you need instead is urlencode. You will have to run it through urldecode on the other side too.

    https://www.php.net/manual/en/function.urlencode.php