SEO Schema JSON-LD Metabox

Home » Snippets » SEO Schema JSON-LD Metabox
0

Created with:

Meta Box Generator

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project
				/**
 * The WPTurbo Custom Meta Box class.
 *
 * This class is responsible for rendering a custom meta box in WordPress with user-defined fields/options.
 * It uses the WordPress add_meta_box() and update_post_meta() functions to render and save field values.
 *
 * @link              https://wpturbo.ai
 * @since             1.0.0
 * @package           WPTurbo
 * 
 */
class wpturbo_JSON_LD_SCHEMA_METABOX {

	/**
	 * Array that defines display locations.
	 *
	 * @since 1.0.0
	 * @access private
	 * @var array $display_locations The list of locations where this meta box should be displayed.
	 */
	private $display_locations = [
		'post',
		'page',
	];
	
	/**
	 * Variables array that defines fields/options for the meta box.
	 *
	 * @since 1.0.0
	 * @access private
	 * @var array $fields The list of user defined fields/options.
	 */
	private $fields = [
		'wpturbo-json-ld-schema' => [
			'type' => 'textarea',
			'label' => 'JSON-LD Schema',
			'default' => '{\n  "@context": "https://schema.org",\n  "@type": "BlogPosting",\n  "headline": "",\n  "editor": "John Doe",\n  "author": {\n    "@type": "Person",\n    "name": "Peter"\n  }\n}',
		],
	];
	
	/**
	 * WPTurbo_Custom_Meta_Box constructor.
	 *
	 * Adds actions to WordPress hooks "add_meta_boxes" and "save_post".
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		add_action( 'add_meta_boxes', [ $this, 'wpturbo_register_meta_boxes' ] );
		add_action( 'save_post', [ $this, 'wpturbo_save_meta_box_fields' ] );
	}
	
	/**
	 * Adds meta boxes to appropriate WordPress screens.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @return void
	 */
	public function wpturbo_register_meta_boxes() : void {
		foreach ( $this->display_locations as $location ) {
			add_meta_box(
				'wpturbo-json-ld-schema-metabox', /* The id of our meta box. */
				'SEO JSON-LD Schema', /* The title of our meta box. */
				[ $this, 'wpturbo_render_meta_box_fields' ], /* The callback function that renders the metabox. */
				$location, /* The screen on which to show the box. */
				'normal', /* The placement of our meta box. */
				'low', /* The priority of our meta box. */
			);
		}
	}
	
	/**
	 * Renders the Meta Box and its fields.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @param WP_Post $post The post object.
	 *
	 * @return void
	 */
	public function wpturbo_render_meta_box_fields(WP_Post $post) : void {
		wp_nonce_field( 'wpturbo-json-ld-schema-metabox_data', 'wpturbo-json-ld-schema-metabox_nonce' );
		echo '<h3>SEO JSON-LD Schema</h3>';
		$html = '';
		foreach( $this->fields as $field_id => $field ){
			$meta_value = get_post_meta( $post->ID, $field_id, true );
			if ( empty( $meta_value ) && isset( $field['default'] ) ) {
				$meta_value = $field['default'];
			}
	
			$field_html = $this->wpturbo_render_input_field( $field_id, $field, $meta_value );
			$label = "<label for='$field_id'>{$field['label']}</label>";
			$html .= $this->wpturbo_format_field( $label, $field_html );
		}
		echo '<table class="form-table"><tbody>' . $html . '</tbody></table>';
	}
	
	/**
	 * Formats each field to table display.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @param string $label The field label.
	 * @param string $field The field HTML code.
	 *
	 * @return string
	 */
	public function wpturbo_format_field( string $label, string $field ): string {
		return '<tr class="form-field"><th>' . $label . '</th><td>' . $field . '</td></tr>';
	}
	
	/**
	 * Renders each individual field HTML code.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @param string $field_id The field ID.
	 * @param array $field The field configuration array.
	 * @param string $field_value The field value.
	 *
	 * @return string The HTML code.
	 */
	public function wpturbo_render_input_field( string $field_id, array $field, string $field_value): string {
		switch( $field['type'] ){
			case 'select': {
				$field_html = '<select name="'.$field_id.'" id="'.$field_id.'">';
					foreach( $field['options'] as $key => $value ) {
						$key = !is_numeric( $key ) ? $key : $value;
						$selected = '';
						if( $field_value === $key ) {
							$selected = 'selected="selected"';
						}
						$field_html .= '<option value="' . $key . '" ' . $selected . '>' . $value . '</option>';
					}
				$field_html .= '</select>';
				break;
			}
			case 'textarea': {
				$field_html = '<textarea name="' . $field_id . '" id="' . $field_id . '" rows="6">' . $field_value . '</textarea>';
				break;
			}
			default: {
				$field_html = "<input type='{$field['type']}' id='$field_id' name='$field_id' value='$field_value' />";
				break;
			}
		}
	
		return $field_html;
	}
	
	/**
	 * Called when this metabox is saved.
	 *
	 * Saves the new meta values of our metabox.
	 *
	 * @since 1.0.0
	 * @access public
	 *
	 * @param int $post_id The post ID.
	 *
	 * @return int The post ID.
	 */
	public function wpturbo_save_meta_box_fields( int $post_id ) {
		if ( ! isset( $_POST['wpturbo-json-ld-schema-metabox_nonce'] ) ) return;
	
		$nonce = $_POST['wpturbo-json-ld-schema-metabox_nonce'];
		if ( !wp_verify_nonce( $nonce, 'wpturbo-json-ld-schema-metabox_data' ) ) return;
	
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
	
		foreach ( $this->fields as $field_id => $field ) {
			if( isset( $_POST[$field_id] ) ){
				// Sanitize fields that need to be sanitized.
				switch( $field['type'] ) {
					case 'email': {
						$_POST[$field_id] = sanitize_email( $_POST[$field_id] );
						break;
					}
					case 'text': {
						$_POST[$field_id] = sanitize_text_field( $_POST[$field_id] );
						break;
					}
				}
				update_post_meta( $post_id, $field_id, $_POST[$field_id] );
			}
		}
	}
	
}

if ( class_exists( 'wpturbo_JSON_LD_SCHEMA_METABOX' ) ) {
	new wpturbo_JSON_LD_SCHEMA_METABOX();
}
			

Register an account to save your snippets or go Pro to get more features.