PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

URL encode title to create link

  1. I have set up a gravity form to help speed up the process of submitting a rather time consuming post layout that I regularly use. And I have got it doing everything I need, apart from 1 thing.

    I need to be able to include the URL of the post into the actual post as a link.

    My posts are not automatically published, so the post-id hasn't worked, but what I thought would work would be if there was a way I could URL encode the post title and then I can add that to the end of a http://mysite.com/post-title-here (as this is how my permalinks are set up).

    I have tried just having the post title as is at the end of the URL, but with the spaces it doesn't work.

    Anyway to either URL encode (I think that is what it's called), or some other way to make the title text URL friendly ?

    Thanks
    Kate

    Posted 11 years ago on Sunday July 29, 2012 | Permalink
  2. You would need to use the same function that WordPress uses to create the permalink, to ensure it's exactly the same. You're basically trying to guess what WordPress is going to use as a permalink.

    There is a post ID for draft posts. Can you use that? Then you could do http://mysite.com/?p=12345 and that will be rewritten with your permalink rules. That's the short link, basically.

    Can you see what information is stored in your entry currently and see if you can include one of those pieces of information in the post you're creating?

    Posted 11 years ago on Sunday July 29, 2012 | Permalink
  3. I tried the id thing and it doesn;t work.

    I need the URL for what published post will be, and in 95% of cases it will just be the title with dashes instead of spaces, and I am happy to fix any that need to be fixed.

    But this is a process that I do a lot each day, and if I could get this working it would save me so much time!

    So is there any way to show the title with dashes instead of spaces?

    Posted 11 years ago on Tuesday July 31, 2012 | Permalink
  4. I tried the id thing and it doesn;t work.

    What happened when you tried it? Can you show us how you did it? Using the ID will be the most reliable way to do it and should work. I'm curious why it did not.

    Posted 11 years ago on Tuesday July 31, 2012 | Permalink
  5. It created the link - with the numbers at the end, but then if you actually tried to open that link it said page not found.

    Please can you just help me to change the title to a URL friendly version. It really is the best way for what I am trying to do. thanks.

    Posted 11 years ago on Wednesday August 1, 2012 | Permalink
  6. Can you provide a link to your site please? Thank you.

    Posted 11 years ago on Wednesday August 1, 2012 | Permalink
  7. The WordPress function which is used to create the permalink is called sanitize_title_with_dashes. Here is an explanation of how it's used: http://codex.wordpress.org/Function_Reference/sanitize_title_with_dashes

    You probably want to use the gform_post_data filter to add your post link to the post:
    http://www.gravityhelp.com/documentation/page/Gform_post_data

    The function you write will read the title that was submitted in the form, run it through the WordPress sanitize_title_with_dashes function, then create the link to the post, and add that link to your post body. Then, you would return the $post_data.

    It's more work than using the ?p=12345 notation, but it can be done. You could still run into problems with the permalink not being identical to the one used in the published post, as I mentioned here.

    Posted 11 years ago on Wednesday August 1, 2012 | Permalink
  8. ok, that's way over my head! I thought there might be a quick URL encode function or something I could use.

    And I'd rather not post links to the site here as they show up in search results (and it doesnt look very professional). thanks!

    Posted 11 years ago on Thursday August 2, 2012 | Permalink
  9. The easiest solution to implement will be to use the ?p=12345 notation as described. There are other ways to do it, but they are all more complicated and have downsides. If you'd like to send me an admin login to your WordPress site, please email it to chris@rocketgenius.com and I will help get it working for you on Friday.

    Thank you.

    Posted 11 years ago on Thursday August 2, 2012 | Permalink
  10. Also, URL encoding does not do what you want:
    http://php.net/manual/en/function.urlencode.php

    It would make a mess of your post titles and would not work to direct visitors to their newly created post.

    Posted 11 years ago on Thursday August 2, 2012 | Permalink