New2Tip: Adding A Click Highlighting Shortlink Input To Posts
Using WordPress 3.0 Shortlinks
This is a quick tip for how you can add an input form field to your posts that has the posts shortlink now built into WordPress 3.0, that automatically highlights for easy copying, just like the one you see at the bottom right of every post on New2WP.
Here's an example of this
Shortlink:
First check if the function exists incase for some reason it doesn't your theme won't break then.
<?php if ( function_exists( 'wp_get_shortlink' ) ) { ?>
Then create a span element which will include the input field and if you want, the word Shortlink so that people know what it is for.
For the input value echo out the wp_get_shortlink( get_the_ID() ) functions for getting the shortlink of the current post by it's ID.
Then add an onclick event with some Javascript code that says basically, when this is clicked, the input is in focus, this should be selected.
<span class="shortlink">Shortlink: <input type='text' value='<?php echo wp_get_shortlink(get_the_ID()); ?>' onclick='this.focus(); this.select();' /> </span>
Finally, close the if statement first opened to check if the function exists.
<?php } ?>
It's really as simple as that. You can style it with CSS if you want easily, and whatever else you can think of.
Here's The Full Code
<?php if ( function_exists( 'wp_get_shortlink' ) ) { ?>
<span class="shortlink">Shortlink:
<input type='text' value='<?php echo wp_get_shortlink(get_the_ID()); ?>' onclick='this.focus(); this.select();' />
</span>
<?php } ?>
If you have questions, don't hesitate to post a comment below as always.






User Comments
( ADD YOURS )Assistanc3 March 28
Exactly the code I was looking for, ty Jared.
Used it in conjunction with php-code-widget to use in sidebar
Jared March 29
Your welcome :)
Trackbacks