Created
July 17, 2012 17:44
-
-
Save stellarcowboy/3130778 to your computer and use it in GitHub Desktop.
Correct the gallery URL attachemnt field WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Corrects the gallery URL attachment that disallows custom URLS | |
// Use get_post_meta( ATTACHMENT_ID_HERE, '_wp_attachment_url', true ) to retrieve | |
function _save_attachment_url($post, $attachment) { | |
if ( isset($attachment['url']) ) | |
update_post_meta( $post['ID'], '_wp_attachment_url', esc_url_raw($attachment['url']) ); | |
return $post; | |
} | |
add_filter('attachment_fields_to_save', '_save_attachment_url', 10, 2); | |
function _replace_attachment_url($form_fields, $post) { | |
if ( isset($form_fields['url']['html']) ) { | |
$url = get_post_meta( $post->ID, '_wp_attachment_url', true ); | |
if ( ! empty($url) ) | |
$form_fields['url']['html'] = preg_replace( "/value='.*?'/", "value='$url'", $form_fields['url']['html'] ); | |
} | |
return $form_fields; | |
} | |
add_filter('attachment_fields_to_edit', '_replace_attachment_url', 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment