Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

When someone creates a post in a custom post type and uploads an image the Link URL box shows it as the attachment URL. What I would like is for it to be the permalink to the post. Is there a way to do it without modifying core files?

share|improve this question
    
Can you give example URLs and if possible annotated screenshots so we can more easily understand exactly what you are asking. As is, I'd only be guessing what you want. –  MikeSchinkel Jan 31 '11 at 3:20
    
@Mike you can take a look flesheatingzipper.com On some of the post the image links to the attachment page. The front page is posts from 4 custom posts types. The images are just inserted into the post and by default they have the attachment URL for the images but I would like that to be the permalink. There is an option for Post URL when adding an image but clicking it does nothing. I'm guessing this is because it's a custom post type. –  kel Jan 31 '11 at 4:56
    
Thanks. In future, always update your original question if someone asks for clarification; it makes it more likely others will see your new info. –  MikeSchinkel Jan 31 '11 at 8:11
add comment

2 Answers

up vote 2 down vote accepted

Hi @kel:

Thanks for answering the comments and thanks to @בניית אתרים providing the screenshot; now I have a better idea of what you wanted.

Based on what you asked for I think what would be ideal would be for the dialog box to simple generate your preferred URL; that way you don't have to remember to click. You can accomplish what you are after by using the 'attachment_fields_to_edit' hook and modifying the sub-elements of the passed array: $form_fields['url']['html'].

You can copy the following code to your theme's functions.php file, or to a .PHP file for a plugin you might be writing:

add_filter('attachment_fields_to_edit','your_attachment_fields_to_edit',10,2);
function your_attachment_fields_to_edit($form_fields,$post) {
  $url = get_permalink($post->ID);

  /* UNCOMMENT THESE LINES IF FOR PARENT POST URL AND NOT ATTACHMENT POST URL
  $url_parts = explode('/',trim($url,'/'));
  array_pop($url_parts);
  $url = implode('/',$url_parts) . '/';
  */
  $form_fields['url']['html'] =<<<HTML
<input type="text" class="text urlfield" name="attachments[{$post->ID}][url]" 
       value="{$url}" />
HTML;
  return $form_fields;
}

Here's what it looks like with the commented-out code so that the full permalink to the attachment post shows:

Screenshot of a Modified WordPress File Upload Dialog with changed URL

Here's what it looks like with the commented-out code so that the full permalink to the attachment post shows:

Screenshot of a Modified WordPress File Upload Dialog with different URL

Note that one caveat is this only works after you have saved the parent post because the permalink is evidently not finalized until then. I'm sure there's a workaround but I'll leave that as an exercise for the reader (like my professors used to say...)

P.S. I simultaneously love your domain name, and I feel quite queasy about it at the same time! ;-)

share|improve this answer
    
Hey, I added the code and uncommented out the code you gave. What it's doing is linking the image to the front page and not the actual article. Would the post need to be saved as a draft first to get the URL then the image added to the post? –  kel Feb 1 '11 at 5:51
    
@kel - I believe that's the caveat I noted at the end of my answer? –  MikeSchinkel Feb 1 '11 at 5:53
    
You are right sorry, it was late when I was testing it out. I did save it first then inserted the image and it works perfect. Thanks for all your help, you really explained it well! –  kel Feb 1 '11 at 18:44
    
@kel - Glad I could help. –  MikeSchinkel Feb 2 '11 at 0:13
add comment

On the media uploader, Once an image is uploaded , before you click the "insert to post" you need to click the "none" button under link url. here, they say a picture is worth a 1000 words: image with no link url

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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