Skip to content

Instantly share code, notes, and snippets.

@dobsondev
Created September 24, 2019 17:38
Show Gist options
  • Save dobsondev/d00819ba848417941a25a3b33afdd6b7 to your computer and use it in GitHub Desktop.
Save dobsondev/d00819ba848417941a25a3b33afdd6b7 to your computer and use it in GitHub Desktop.
Add meta boxes with WordPress editor boxes in them to your home page.
<?php
/**
* Create the meta boxes for the home as long as it is the home page template that is currently
* being used.
*
* https://wordpress.stackexchange.com/questions/82477/how-to-add-add-meta-box-to-specific-page-template
* https://developer.wordpress.org/reference/hooks/add_meta_boxes/
* https://developer.wordpress.org/reference/functions/get_post_meta/
* https://developer.wordpress.org/reference/functions/add_meta_box/
*/
function THEMESLUG_register_home_page_meta_boxes() {
global $post;
if ( 'templates/template-home-page.php' == get_post_meta( $post->ID, '_wp_page_template', true ) ) {
add_meta_box( 'THEMESLUG-home-page-box-1-meta-box-id', __( 'Home Page Box 1', 'THEMESLUG_text_domain' ), 'THEMESLUG_home_page_box_1_meta_box_callback' );
add_meta_box( 'THEMESLUG-home-page-box-2-meta-box-id', __( 'Home Page Box 2', 'THEMESLUG_text_domain' ), 'THEMESLUG_home_page_box_2_meta_box_callback' );
add_meta_box( 'THEMESLUG-home-page-box-3-meta-box-id', __( 'Home Page Box 3', 'THEMESLUG_text_domain' ), 'THEMESLUG_home_page_box_3_meta_box_callback' );
}
}
add_action( 'add_meta_boxes_page', 'THEMESLUG_register_home_page_meta_boxes' );
/**
* Creates the text box using the WordPress editor for the home page meta box.
*
* https://developer.wordpress.org/reference/functions/wp_nonce_field/
* https://developer.wordpress.org/reference/functions/get_post_meta/
* https://make.wordpress.org/support/user-manual/content/editors/
*/
function THEMESLUG_home_page_box_1_meta_box_callback( $post ) {
wp_nonce_field( 'save_THEMESLUG_home_page_box_1_meta_box', 'THEMESLUG_home_page_box_1_meta_box_nonce' );
$editor_text = get_post_meta( $post->ID, 'home_page_box_1_text', true );
wp_editor( htmlspecialchars_decode( $editor_text ), 'THEMESLUG_home_page_box_1', $settings = array( 'textarea_name' => 'home-page-box-1' ) );
}
/**
* Save the text from the editor to the post meta.
*
* https://developer.wordpress.org/reference/hooks/save_post/
* https://developer.wordpress.org/reference/functions/wp_verify_nonce/
* https://developer.wordpress.org/reference/functions/current_user_can/
* https://developer.wordpress.org/reference/functions/update_post_meta/
*/
function THEMESLUG_home_page_box_1_save_meta_box( $post_id ) {
// Check if our nonce is set.
if ( ! isset( $_POST['THEMESLUG_home_page_box_1_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['THEMESLUG_home_page_box_1_meta_box_nonce'], 'save_THEMESLUG_home_page_box_1_meta_box' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
// Save the text
$editor_data = htmlspecialchars( $_POST['home-page-box-1'] );
update_post_meta( $post_id, 'home_page_box_1_text', $editor_data );
}
add_action( 'save_post', 'THEMESLUG_home_page_box_1_save_meta_box' );
/**
* Creates the text box using the WordPress editor for the home page meta box.
*
* https://developer.wordpress.org/reference/functions/wp_nonce_field/
* https://developer.wordpress.org/reference/functions/get_post_meta/
* https://make.wordpress.org/support/user-manual/content/editors/
*/
function THEMESLUG_home_page_box_2_meta_box_callback( $post ) {
wp_nonce_field( 'save_THEMESLUG_home_page_box_2_meta_box', 'THEMESLUG_home_page_box_2_meta_box_nonce' );
$editor_text = get_post_meta( $post->ID, 'home_page_box_2_text', true );
wp_editor( htmlspecialchars_decode( $editor_text ), 'THEMESLUG_home_page_box_2', $settings = array( 'textarea_name' => 'home-page-box-2' ) );
}
/**
* Save the text from the editor to the post meta.
*
* https://developer.wordpress.org/reference/hooks/save_post/
* https://developer.wordpress.org/reference/functions/wp_verify_nonce/
* https://developer.wordpress.org/reference/functions/current_user_can/
* https://developer.wordpress.org/reference/functions/update_post_meta/
*/
function THEMESLUG_home_page_box_2_save_meta_box( $post_id ) {
// Check if our nonce is set.
if ( ! isset( $_POST['THEMESLUG_home_page_box_2_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['THEMESLUG_home_page_box_2_meta_box_nonce'], 'save_THEMESLUG_home_page_box_2_meta_box' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
// Save the text
$editor_data = htmlspecialchars( $_POST['home-page-box-2'] );
update_post_meta( $post_id, 'home_page_box_2_text', $editor_data );
}
add_action( 'save_post', 'THEMESLUG_home_page_box_2_save_meta_box' );
/**
* Creates the text box using the WordPress editor for the home page meta box.
*
* https://developer.wordpress.org/reference/functions/wp_nonce_field/
* https://developer.wordpress.org/reference/functions/get_post_meta/
* https://make.wordpress.org/support/user-manual/content/editors/
*/
function THEMESLUG_home_page_box_3_meta_box_callback( $post ) {
wp_nonce_field( 'save_THEMESLUG_home_page_box_3_meta_box', 'THEMESLUG_home_page_box_3_meta_box_nonce' );
$editor_text = get_post_meta( $post->ID, 'home_page_box_3_text', true );
wp_editor( htmlspecialchars_decode( $editor_text ), 'THEMESLUG_home_page_box_3', $settings = array( 'textarea_name' => 'home-page-box-3' ) );
}
/**
* Save the text from the editor to the post meta.
*
* https://developer.wordpress.org/reference/hooks/save_post/
* https://developer.wordpress.org/reference/functions/wp_verify_nonce/
* https://developer.wordpress.org/reference/functions/current_user_can/
* https://developer.wordpress.org/reference/functions/update_post_meta/
*/
function THEMESLUG_home_page_box_3_save_meta_box( $post_id ) {
// Check if our nonce is set.
if ( ! isset( $_POST['THEMESLUG_home_page_box_3_meta_box_nonce'] ) ) {
return;
}
// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['THEMESLUG_home_page_box_3_meta_box_nonce'], 'save_THEMESLUG_home_page_box_3_meta_box' ) ) {
return;
}
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check the user's permissions.
if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return;
}
} else {
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
}
// Save the text
$editor_data = htmlspecialchars( $_POST['home-page-box-3'] );
update_post_meta( $post_id, 'home_page_box_3_text', $editor_data );
}
add_action( 'save_post', 'THEMESLUG_home_page_box_3_save_meta_box' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment