Created
February 18, 2012 19:56
-
-
Save jtsternberg/1860851 to your computer and use it in GitHub Desktop.
Use wp_editor internal linking for metabox. Still a work in progress (adds autop when visual editor is selected)
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
function nbc_dynamic_lead_link_meta_box( $post ) { | |
global $post; | |
// Use nonce for verification | |
wp_nonce_field( basename( __FILE__ ), 'nbc_dynamic_lead_link_nonce' ); | |
$link = get_post_meta( $post->ID, 'lead_link', true ); | |
$settings = array( | |
'media_buttons' => false, | |
'wpautop' => false, | |
'textarea_rows' => 1, | |
'tinymce' => false, | |
'quicktags' => array( 'buttons' => 'link' ) | |
); | |
wp_editor( $link, 'lead_link', $settings ); | |
} | |
/** | |
* Save our custom metabox data | |
* | |
*/ | |
function nbc_dynamic_lead_link_save( $post_id ) { | |
// verify this came from our screen and with proper authorization. | |
if ( !wp_verify_nonce( $_POST['nbc_dynamic_lead_link_nonce'], basename(__FILE__) ) ) { | |
return $post_id; | |
} | |
// check autosave | |
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) | |
return $post_id; | |
// check permissions | |
if ('page' == $_POST['post_type']) { | |
if (!current_user_can('edit_page', $post_id)) { | |
return $post_id; | |
} | |
} elseif (!current_user_can('edit_post', $post_id)) { | |
return $post_id; | |
} | |
// OK, we're authenticated: we need to find and save the data | |
$post = get_post( $post_id ); | |
if ( $post->post_type == 'dynamic-leads' ) { | |
$old = get_post_meta( $post_id, 'lead_link', true ); | |
$new = $_POST['lead_link']; | |
if ($new && $new != $old) { | |
if ( strpos( $new, 'a href=' ) > 0 ) { | |
preg_match( '!"([^"]+)"!', $new, $url ); | |
update_post_meta( $post_id, 'lead_link', $url[1] ); | |
} else { | |
update_post_meta( $post_id, 'lead_link', esc_url( $new ) ); | |
} | |
} elseif ('' == $new && $old) { | |
delete_post_meta( $post_id, 'lead_link', $old); | |
} | |
} | |
return $lead_link; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment