-
-
Save davidchase/df9adeb1e03b88691899 to your computer and use it in GitHub Desktop.
MetaBox for links/blogroll in 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
<?php | |
/* Define the custom box */ | |
add_action( 'add_meta_boxes', 'myplugin_add_custom_box' ); | |
// backwards compatible (before WP 3.0) | |
// add_action( 'admin_init', 'myplugin_add_custom_box', 1 ); | |
/* Do something with the data entered */ | |
add_action( 'edit_link', 'myplugin_save_postdata' ); | |
/* Adds a box to the main column on the Post and Page edit screens */ | |
function myplugin_add_custom_box() { | |
$screens = array( 'link' ); | |
foreach ($screens as $screen) { | |
add_meta_box( | |
'myplugin_sectionid', | |
__( 'My Post Section Title', 'myplugin_textdomain' ), | |
'myplugin_inner_custom_box', | |
$screen | |
); | |
} | |
} | |
/* Prints the box content */ | |
function myplugin_inner_custom_box( $post ) { | |
// Use nonce for verification | |
wp_nonce_field( plugin_basename( __FILE__ ), 'myplugin_noncename' ); | |
// The actual fields for data entry | |
// Use get_post_meta to retrieve an existing value from the database and use the value for the form | |
$value = get_option('got_new_key'); | |
echo '<label for="myplugin_new_field">'; | |
_e("Description for this field", 'myplugin_textdomain' ); | |
echo '</label> '; | |
echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="'.esc_attr($value).'" size="25" />'; | |
} | |
/* When the post is saved, saves our custom data */ | |
function myplugin_save_postdata( $post_id ) { | |
// verify if this is an auto save routine. | |
// If it is our form has not been submitted, so we dont want to do anything | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) | |
return; | |
// verify this came from the our screen and with proper authorization, | |
// because save_post can be triggered at other times | |
if ( !isset( $_POST['myplugin_noncename'] ) || !wp_verify_nonce( $_POST['myplugin_noncename'], plugin_basename( __FILE__ ) ) ) | |
return; | |
// OK, we're authenticated: we need to find and save the data | |
//sanitize user input | |
$mydata = sanitize_text_field( $_POST['myplugin_new_field'] ); | |
update_option('got_new_key', $mydata); | |
} |
update_option('got_new_key', $mydata);
should be replaced with something like
update_option('got_new_key'.$post_id, $mydata);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You are saving the custom field value to the options, this will not work for multiple links. The value for 'got_new_key' will be replaced when adding a new link.