0

I have created a form (Gravity Form) which I want to be able to complete dynamically from several of my other websites. In such a case, one of the fields that I am trying to send to the form is the post title, which can - and does - includes ampersands from time to time (e.g. The Ins & Outs of Gravity Forms).

The trouble I'm having is figuring out how to encode the post title so that ampersands within the post title are not treated as separators for the next query var.

For example, the URL mydomain.com/page-with-my-form-on-it/?title=The Ins & Outs of Gravity Forms&type=new should complete the 'title' field with 'The Ins & Outs of Gravity Forms' and the 'type' field with 'new', but instead it puts 'The Ins ' in the 'title' field and leaves the 'type' field (and any other subsequent fields) empty.

To create that URL, my current code is simply as follows, though I have tried using urlencode(), htmlentities(), and str_replace() with the_title() to try and overcome this issue:

<a href="http://www.mydomain.com/page-with-my-form-on-it/?title=<?php the_title(); ?>&type=new">Link</a>

Any suggestions?

  • If you haven't already, read up on Data Validation as well. Don't trust any input from a form. (It's possible that Gravity Forms does its own validation, but check that with the vendor's documentation rather than assuming that it happens.) – Pat J Nov 20 '13 at 19:46
0

You already said you tried urlencode(), but what did you try that with? If you've only tried urlencode() with values from the_title() or the_title_attibute() so far, try it with get_the_title() directly. I'm able to get different results by trying this out if I try with a little test shortcode:

  1. Using the_title(): the_title

    function wpse_123927_cb() {
        return urlencode( the_title() );
    }
    add_shortcode('test_wpse_123927', 'wpse_123927_cb');
    
  2. Using the_title_attribute(): the_title_attribute

    function wpse_123927_cb() {
        return urlencode( the_title_attribute() );
    }
    add_shortcode('test_wpse_123927', 'wpse_123927_cb');
    
  3. Using get_the_title(): get_the_title

    function wpse_123927_cb() {
        return urlencode( get_the_title() );
    }
    add_shortcode('test_wpse_123927', 'wpse_123927_cb');
    

The third solution here works well both ways. I used this little code here to test:

function wpse_123927_cb() {
    $encode = esc_attr( urlencode( get_the_title() ) );
    $decode = urldecode($encode);

    return 'Encoded: ' . $encode . '<br>Decoded: '. $decode;
}
add_shortcode('test_wpse_123927', 'wpse_123927_cb');

And got the following output: encoded & decoded

Hope that helps!

  • 1
    Of course. Why am I trying to urlencode the_title? It should be get_the_title! My final code was slightly tweaked to fit the application, but it was like this: <a href="http://www.mydomain.com/page-with-my-form-on-it/?title=<?php echo esc_attr( urlencode( get_the_title() ) ); ?>&type=new">Link</a>. Works like a dream now. – Dave Clements Nov 20 '13 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.