Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

365 – zdroje

class ZdrojMetaBox {

	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(
			'zdroje',
			__( 'Zdroje', 'text_domain' ),
			array( $this, 'render_metabox' ),
			'post',
			'normal',
			'default'
		);

	}

	public function render_metabox( $post ) {

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

		// Set default values.
		if( empty( $kbnt_url ) ) $kbnt_url = '';

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

		echo '	<tr>';
		echo '		<th><label for="kbnt_show" class="kbnt_show_label">' . __( 'Zobrazit zdroje v příspěvku', 'text_domain' ) . '</label></th>';
		echo '		<td>';
		echo '			<label><input type="checkbox" name="kbnt_" class="kbnt_show_field" value="' . $kbnt_ . '" ' . checked( $kbnt_, 'checked', false ) . '> ' . __( '', 'text_domain' ) . '</label><br>';
		echo '		</td>';
		echo '	</tr>';

		echo '	<tr>';
		echo '		<th><label for="kbnt_url" class="kbnt_url_label">' . __( 'URL zdroje', 'text_domain' ) . '</label></th>';
		echo '		<td>';
		echo '			<input type="text" id="kbnt_url" name="kbnt_url" class="kbnt_url_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr__( $kbnt_url ) . '">';
		echo '		</td>';
		echo '	</tr>';

		echo '</table>';

	}

	public function save_metabox( $post_id, $post ) {

		// Sanitize user input.
		$kbnt_new_ = isset( $_POST[ 'kbnt_' ] ) ? 'checked' : '';
		$kbnt_new_url = isset( $_POST[ 'kbnt_url' ] ) ? sanitize_text_field( $_POST[ 'kbnt_url' ] ) : '';

		// Update the meta field in the database.
		update_post_meta( $post_id, 'kbnt_', $kbnt_new_ );
		update_post_meta( $post_id, 'kbnt_url', $kbnt_new_url );

	}

}

new ZdrojMetaBox;