Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

Paid content field

class Bi_Functionality_Paid_Content_Field {

	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(
			'paid_content_field',
			__( 'Paid Content', 'bi_functionality' ),
			array( $this, 'render_metabox' ),
			'post',
			'side',
			'default'
		);

	}

	public function render_metabox( $post ) {

		// Add nonce for security and authentication.
		wp_nonce_field( 'bi_functionalitynonce_action', 'bi_functionalitynonce' );

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

		// Set default values.
		if( empty( $bi_functionality_paid_content ) ) $bi_functionality_paid_content = array();

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

		echo '	<tr>';
		echo '		<th><label for="bi_functionality_paid_content" class="bi_functionality_paid_content_label">' . __( 'Paid', 'bi_functionality' ) . '</label></th>';
		echo '		<td>';
		echo '			<label><input type="checkbox" name="bi_functionality_paid_content[]" class="bi_functionality_paid_content_field" value="' . esc_attr( '' ) . '" ' . ( in_array( '', $bi_functionality_paid_content )? 'checked="checked"' : '' ) . '> ' . __( '', 'bi_functionality' ) . '</label><br>';
		echo '			<p class="description">' . __( 'Check if content is paid by sponsor', 'bi_functionality' ) . '</p>';
		echo '		</td>';
		echo '	</tr>';

		echo '</table>';

	}

	public function save_metabox( $post_id, $post ) {

		// Add nonce for security and authentication.
		$nonce_name   = isset( $_POST['bi_functionalitynonce'] ) ? $_POST['bi_functionalitynonce'] : '';
		$nonce_action = 'bi_functionalitynonce_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 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.
		$bi_functionalitynew__paid_content = isset( $_POST[ 'bi_functionality_paid_content' ] ) ? array_intersect( (array) $_POST[ 'bi_functionality_paid_content' ], array( '' ) )  : array();

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

	}

}

new Bi_Functionality_Paid_Content_Field;