Skip to content

Instantly share code, notes, and snippets.

@carlodaniele
carlodaniele / custom_menu_admin.php
Last active May 26, 2022 20:32
A basic plugin showing how to add menu metaboxes to admin menu page
<?php
/**
* @package Custom_menu_admin
* @version 1.0
*/
/*
Plugin Name: Custom menu admin
Plugin URI: http://wordpress.org/extend/plugins/#
Description: This is an example plugin
Author: Carlo Daniele
require( dirname( __FILE__ ) . '/wp_install/wp-blog-header.php' );
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
<?php
/**
* Create [usermeta] shortcode
*
* @param $atts An array of attributes
*
* @return string A single user meta field
*
* @link https://codex.wordpress.org/Shortcode_API
* @link https://codex.wordpress.org/Function_Reference/shortcode_atts
<?php
/**
* Plugin init
*
* @link https://codex.wordpress.org/Function_Reference/add_shortcode
*/
function wpmu_user_init(){
add_shortcode( 'usermeta', 'wpmu_user_meta_shortcode' );
}
add_action( 'init', 'wpmu_user_init' );
<?php
add_action( 'personal_options_update', 'wpmu_usermeta_update' );
add_action( 'edit_user_profile_update', 'wpmu_usermeta_update' );
/**
* Save user extra meta data
*
* @param $user_id The user ID of the user being edited
*
* @link https://developer.wordpress.org/reference/hooks/personal_options_update/
<tr class="wpmu-user-group">
<th scope="row"><span><?php echo __( 'Music group or singer' ); ?></span></th>
<td>
<input type="text" list="wpmu_list" name="wpmu[artist]" class="regular-text" value="<?php echo esc_attr( get_user_meta( $user->ID, 'wpmu_artist', true ) ); ?>" />
<datalist id="wpmu_list">
<option value="ACDC" />
<option value="Iron Maiden" />
<option value="Led Zeppelin" />
<option value="Metallica" />
<option value="Megadeth" />
<tr class="wpmu-user-music">
<th scope="row"><span><?php echo __( 'Music genre' ); ?></span></th>
<td>
<select id="wpmu_music" name="wpmu[music]">
<option value="" <?php selected( esc_attr( get_user_meta( $user->ID, 'wpmu_music', true ) ), '' ); ?>><?php echo __( 'Select music genre' ); ?></option>
<option value="Rock" <?php selected( esc_attr( get_user_meta( $user->ID, 'wpmu_music', true ) ), 'Rock' ); ?>><?php echo __( 'Rock' ); ?></option>
<option value="Heavy Metal" <?php selected( esc_attr( get_user_meta( $user->ID, 'wpmu_music', true ) ), 'Heavy Metal' ); ?>><?php echo __( 'Heavy Metal' ); ?></option>
<option value="Thrash Metal" <?php selected( esc_attr( get_user_meta( $user->ID, 'wpmu_music', true ) ), 'Thrash Metal' ); ?>><?php echo __( 'Thrash Metal' ); ?></option>
<option value="Death Metal" <?php selected( esc_attr( get_user_meta( $user->ID, 'wpmu_music', true ) ), 'Death Metal' ); ?>><?php echo __( 'Death Metal' ); ?></option>
<option value="Speed Metal" <?php selected( esc_
<tr class="wpmu-user-guitar">
<th scope="row"><span><?php echo __( 'Play the guitar' ); ?></span></th>
<td>
<legend class="screen-reader-text">
<span><?php echo __( 'Play the Guitar' ); ?></span>
</legend>
<div class="wpmu-guitar-checkbox">
<label for="wpmu_guitar">
<input type="checkbox" name="wpmu[guitar]" id="wpmu_guitar" value="Yes" <?php checked( esc_attr( get_user_meta( $user->ID, 'wpmu_guitar', true ) ), 'Yes' ); ?> />
<?php echo __( 'You Play the Guitar on the M.T.V.' ); ?>
<tr class="wpmu-user-sex">
<th scope="row"><span><?php echo __( 'Sex' ); ?></span></th>
<td>
<input type="radio" name="wpmu[sex]" id="wpmu_male" value="male" <?php checked( esc_attr( get_user_meta( $user->ID, 'wpmu_sex', true ) ), 'male' ); ?> class="tog" /> <label for="wpmu_male"><?php _e( 'Male' ); ?></label> <br />
<input type="radio" name="wpmu[sex]" id="wpmu_female" value="female" <?php checked( esc_attr( get_user_meta( $user->ID, 'wpmu_sex', true ) ), 'female' ); ?> class="tog" /> <label for="wpmu_female"><?php _e( 'Female' ); ?></label>
</td>
</tr>