Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

Services Metabox

class KarmaServices {

	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(
			'karma-services',
			__( 'Additional Info', 'karma' ),
			array( $this, 'render_services_metabox' ),
			'services',
			'side',
			'high'
		);

	}

	public function render_services_metabox( $post ) {

		// Add nonce for security and authentication.
		wp_nonce_field( 'karma_servicesnonce_action', 'karma_servicesnonce' );

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

		// Set default values.
		if( empty( $karma_services ) ) $karma_services = 'Get a Quote';
		if( empty( $karma_services ) ) $karma_services = 'Click here';
		if( empty( $karma_services ) ) $karma_services = '';

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

		echo '	<tr>';
		echo '		<th><label for="karma_services" class="karma_services_label">' . __( 'Title', 'karma' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="karma_services" name="karma_services" class="karma_services_field" placeholder="' . esc_attr__( 'Enter the title', 'karma' ) . '" value="' . esc_attr( $karma_services ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="karma_services" class="karma_services_label">' . __( 'Button Text', 'karma' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="karma_services" name="karma_services" class="karma_services_field" placeholder="' . esc_attr__( '', 'karma' ) . '" value="' . esc_attr( $karma_services ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="karma_services" class="karma_services_label">' . __( 'Button URL', 'karma' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="url" id="karma_services" name="karma_services" class="karma_services_field" placeholder="' . esc_attr__( '', 'karma' ) . '" value="' . esc_attr( $karma_services ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '</table>';

	}

	public function save_metabox( $post_id, $post ) {

		// Add nonce for security and authentication.
		$nonce_name   = isset( $_POST['karma_servicesnonce'] ) ? $_POST['karma_servicesnonce'] : '';
		$nonce_action = 'karma_servicesnonce_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;

		// Sanitize user input.
		$karma_servicesnew_ = isset( $_POST[ 'karma_services' ] ) ? sanitize_text_field( $_POST[ 'karma_services' ] ) : '';
		$karma_servicesnew_ = isset( $_POST[ 'karma_services' ] ) ? sanitize_text_field( $_POST[ 'karma_services' ] ) : '';
		$karma_servicesnew_ = isset( $_POST[ 'karma_services' ] ) ? esc_url( $_POST[ 'karma_services' ] ) : '';

		// Update the meta field in the database.
		update_post_meta( $post_id, 'karma_services', $karma_servicesnew_ );
		update_post_meta( $post_id, 'karma_services', $karma_servicesnew_ );
		update_post_meta( $post_id, 'karma_services', $karma_servicesnew_ );

	}

}

new KarmaServices;