creating url shortner inside wordpress
i am trying to make short url or even pretty URLs inside my wordpress. suppose, i have a the_content filter that finds all external links inside post content, it needs to return short or pretty urls relative to my site URL.
external url:
stackoverflow.com/questions/ask
pretty url:
myblog.com/xyxd
making url is not a big deal! but I don't understand how can I then redirects visitors to original external site when he/she is visiting this pretty url?
suppose, one clicks or or enter:
myblog.com/xyxd
now, I need to redirect him (301/302) to:
stackoverflow.com/questions/ask
how can I do handle this (and also without conflicting with wordpress's native post/page urls).
any help?
thanks in advance even if you read this problem :).
Note: I h开发者_Go百科ad to remove http:// from all links as stackoverflow is not allowing me to put more than one link!
Check out WordPress pretty link.
UPDATE:
You could hook into wp_insert_post
and scan the content for URLs using regex or a HTML parser.
Then use the Pretty Link API to insert pretty links for each URL.
You already have one by default
http://yourweb.local/blog/?p=2365
You can use post IDs as shorturl, something like default settings for permalink structure http://codex.wordpress.org/Using_Permalinks
So inside loop You can use:
<a href="<?php bloginfo('url');?>?p=<?php the_ID(); ?>" title="<?php the_title(); ?>"><?php bloginfo('url');?>?p=<?php the_ID(); ?></a>
(EDITED:or even better) but don't use: Because moving WP brog to another blog, will leave old blog's address
<a href="<?php the_guid(); ?>" title="<?php the_title(); ?>"><?php the_guid(); ?></a>
精彩评论