Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

wPeanuts “External Link” Meta Box

class wPeanuts_External_Link {

	public function __construct() {

		if ( is_admin() ) {
			add_action( 'load-post.php',     array( $this, 'init_metabox' ) );
			add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
		}

	}

	public function init_metabox() {

		add_action( 'add_meta_boxes',        array( $this, 'add_metabox' )         );
		add_action( 'save_post',             array( $this, 'save_metabox' ), 10, 2 );

	}

	public function add_metabox() {

		add_meta_box(
			'external_link',
			__( 'External Link', 'wpeanuts' ),
			array( $this, 'render_metabox' ),
			'item',
			'advanced',
			'default'
		);

	}

	public function render_metabox( $post ) {

		// Add nonce for security and authentication.
		wp_nonce_field( 'wpeanuts_nonce_action', 'wpeanuts_nonce' );

		// Retrieve an existing value from the database.
		$wpeanuts_item_url = get_post_meta( $post->ID, 'wpeanuts_item_url', true );
		$wpeanuts_item_author = get_post_meta( $post->ID, 'wpeanuts_item_author', true );
		$wpeanuts_item_author_url = get_post_meta( $post->ID, 'wpeanuts_item_author_url', true );

		// Set default values.
		if( empty( $wpeanuts_item_url ) ) $wpeanuts_item_url = '';
		if( empty( $wpeanuts_item_author ) ) $wpeanuts_item_author = '';
		if( empty( $wpeanuts_item_author_url ) ) $wpeanuts_item_author_url = '';

		// Form fields.
		echo '<table class="form-table">';

		echo '	<tr>';
		echo '		<th><label for="wpeanuts_item_url" class="wpeanuts_item_url_label">' . __( 'Item URL', 'wpeanuts' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="url" id="wpeanuts_item_url" name="wpeanuts_item_url" class="wpeanuts_item_url_field" placeholder="' . esc_attr__( 'https://...', 'wpeanuts' ) . '" value="' . esc_attr( $wpeanuts_item_url ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="wpeanuts_item_author" class="wpeanuts_item_author_label">' . __( 'Item Author', 'wpeanuts' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="wpeanuts_item_author" name="wpeanuts_item_author" class="wpeanuts_item_author_field" placeholder="' . esc_attr__( '', 'wpeanuts' ) . '" value="' . esc_attr( $wpeanuts_item_author ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="wpeanuts_item_author_url" class="wpeanuts_item_author_url_label">' . __( 'Item Author URL', 'wpeanuts' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="url" id="wpeanuts_item_author_url" name="wpeanuts_item_author_url" class="wpeanuts_item_author_url_field" placeholder="' . esc_attr__( '', 'wpeanuts' ) . '" value="' . esc_attr( $wpeanuts_item_author_url ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '</table>';

	}

	public function save_metabox( $post_id, $post ) {

		// Add nonce for security and authentication.
		$nonce_name   = isset( $_POST['wpeanuts_nonce'] ) ? $_POST['wpeanuts_nonce'] : '';
		$nonce_action = 'wpeanuts_nonce_action';

		// Check if a nonce is set.
		if ( ! isset( $nonce_name ) )
			return;

		// Check if a nonce is valid.
		if ( ! wp_verify_nonce( $nonce_name, $nonce_action ) )
			return;

		// Check if the user has permissions to save data.
		if ( ! current_user_can( 'edit_post', $post_id ) )
			return;

		// Check if it's not an autosave.
		if ( wp_is_post_autosave( $post_id ) )
			return;

		// Check if it's not a revision.
		if ( wp_is_post_revision( $post_id ) )
			return;

		// Sanitize user input.
		$wpeanuts_new_item_url = isset( $_POST[ 'wpeanuts_item_url' ] ) ? esc_url( $_POST[ 'wpeanuts_item_url' ] ) : '';
		$wpeanuts_new_item_author = isset( $_POST[ 'wpeanuts_item_author' ] ) ? sanitize_text_field( $_POST[ 'wpeanuts_item_author' ] ) : '';
		$wpeanuts_new_item_author_url = isset( $_POST[ 'wpeanuts_item_author_url' ] ) ? esc_url( $_POST[ 'wpeanuts_item_author_url' ] ) : '';

		// Update the meta field in the database.
		update_post_meta( $post_id, 'wpeanuts_item_url', $wpeanuts_new_item_url );
		update_post_meta( $post_id, 'wpeanuts_item_author', $wpeanuts_new_item_author );
		update_post_meta( $post_id, 'wpeanuts_item_author_url', $wpeanuts_new_item_author_url );

	}

}

new wPeanuts_External_Link;