Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rajucs/7edf2c5e423a46489aa44d25c09d842c to your computer and use it in GitHub Desktop.
Save rajucs/7edf2c5e423a46489aa44d25c09d842c to your computer and use it in GitHub Desktop.
//Adds custom data field form
function wpse_259995_menu_custom_fields_fields( $id, $item, $depth, $args ) {
$_key = 'img_url';
$label = "Image URL";
$key = sprintf( 'menu-item-%s', $_key );
$id = sprintf( 'edit-%s-%s', $key, $item->ID );
$name = sprintf( '%s[%s]', $key, $item->ID );
$value = get_post_meta( $item->ID, $key, true );
$class = sprintf( 'field-%s', $_key );
printf(
'<p class="%5$s"><label for="%1$s">%2$s<br />
<input type="text" id="%1$s" class="%5$s" name="%3$s" value="%4$s" /></label></p>',
esc_attr( $id ),
esc_html( $label ),
esc_attr( $name ),
esc_url( $value ),
esc_attr( $class )
);
}
add_action( 'wp_nav_menu_item_custom_fields', 'wpse_259995_menu_custom_fields_fields', 10, 4 );
//Save Custom Menu data
function wpse_259995_menu_custom_fields_save( $menu_id, $menu_item_db_id, $menu_item_args ) {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
check_admin_referer( 'update-nav_menu', 'update-nav-menu-nonce' );
$_key = 'img_url';
$label = "Image URL";
$key = sprintf( 'menu-item-%s', $_key );
// Sanitize
if ( ! empty( $_POST[ $key ][ $menu_item_db_id ] ) ) {
// Do some checks here...
$value = $_POST[ $key ][ $menu_item_db_id ];
}
else {
$value = null;
}
// Update
if ( ! is_null( $value ) ) {
update_post_meta( $menu_item_db_id, $key, $value );
}
else {
delete_post_meta( $menu_item_db_id, $key );
}
}
add_action( 'wp_update_nav_menu_item', 'wpse_259995_menu_custom_fields_save', 10, 3 );
//Adds toggle to show field in screen options
function wpse_259995_menu_custom_fields_columns( $columns ) {
$fields = array('img_url' => "Imae URL");
$columns = array_merge( $columns, $fields );
return $columns;
}
add_filter( 'manage_nav-menus_columns', 'wpse_259995_menu_custom_fields_columns', 99 );
//Modify the element of the WP walker
function wpse_259995_walker_nav_menu_start_el_menu_with_images ($item_output, $item, $depth, $args ){
$attr_title = ! empty( $item->attr_title ) ? $item->attr_title : $item->title;
$img_url = get_post_meta( $item->ID, 'menu-item-img_url', true );
$item_output = $args->before;
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . '<img src="'.$img_url.'" />' . $args->link_after;
$item_output .= $args->after;
return $item_output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment