Make WordPress Core

Ticket #13429: save-custom-link-url.php

File save-custom-link-url.php, 969 bytes (added by SergeyBiryukov, 14 years ago)
Line 
1<?php
2/*
3Plugin Name: Save Custom Link URL
4Version: 0.1
5Plugin URI: http://core.trac.wordpress.org/ticket/13429
6Description: Allows to specify a custom Link URL for any attachment.
7Author: Sergey Biryukov
8Author URI: http://profiles.wordpress.org/sergeybiryukov/
9*/
10
11function _save_attachment_url($post, $attachment) {
12        if ( isset($attachment['url']) ) 
13                update_post_meta( $post['ID'], '_wp_attachment_url', esc_url_raw($attachment['url']) ); 
14
15        return $post;
16}
17add_filter('attachment_fields_to_save', '_save_attachment_url', 10, 2);
18
19function _replace_attachment_url($form_fields, $post) {
20        if ( isset($form_fields['url']['html']) ) {
21                $url = get_post_meta( $post->ID, '_wp_attachment_url', true );
22                if ( ! empty($url) )
23                        $form_fields['url']['html'] = preg_replace( "/value='.*?'/", "value='$url'", $form_fields['url']['html'] );
24        }
25
26        return $form_fields;
27}
28add_filter('attachment_fields_to_edit', '_replace_attachment_url', 10, 2);
29?>