Heading Title Settings
class hero_title {
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(
'hero-heading',
__( 'Hero Title Settings', 'leadeight' ),
array( $this, 'render_metabox' ),
'page',
'normal',
'high'
);
}
public function render_metabox( $post ) {
// Retrieve an existing value from the database.
$lead8_hero_title_wrapper = get_post_meta( $post->ID, 'lead8_hero-title-wrapper', true );
$lead8_hero_title = get_post_meta( $post->ID, 'lead8_hero-title', true );
$lead8_sub_header_wrapper = get_post_meta( $post->ID, 'lead8_sub-header-wrapper', true );
$lead8_sub_header_text = get_post_meta( $post->ID, 'lead8_sub-header-text', true );
$lead8_cta_1_text = get_post_meta( $post->ID, 'lead8_cta-1-text', true );
$lead8_cta_1_url = get_post_meta( $post->ID, 'lead8_cta-1-url', true );
$lead8_cta_2_text = get_post_meta( $post->ID, 'lead8_cta-2-text', true );
$lead8_cta_2_link = get_post_meta( $post->ID, 'lead8_cta-2-link', true );
// Set default values.
if( empty( $lead8_hero_title_wrapper ) ) $lead8_hero_title_wrapper = '';
if( empty( $lead8_hero_title ) ) $lead8_hero_title = '';
if( empty( $lead8_sub_header_wrapper ) ) $lead8_sub_header_wrapper = '';
if( empty( $lead8_sub_header_text ) ) $lead8_sub_header_text = '';
if( empty( $lead8_cta_1_text ) ) $lead8_cta_1_text = '';
if( empty( $lead8_cta_1_url ) ) $lead8_cta_1_url = '';
if( empty( $lead8_cta_2_text ) ) $lead8_cta_2_text = '';
if( empty( $lead8_cta_2_link ) ) $lead8_cta_2_link = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="lead8_hero-title-wrapper" class="lead8_hero-title-wrapper_label">' . __( 'Headline Wrapper', 'leadeight' ) . '</label></th>';
echo ' <td>';
echo ' <select id="lead8_hero_title_wrapper" name="lead8_hero-title-wrapper" class="lead8_hero_title_wrapper_field">';
echo ' <option value="lead8_h1" ' . selected( $lead8_hero_title_wrapper, 'lead8_h1', false ) . '> ' . __( 'H1', 'leadeight' ) . '</option>';
echo ' <option value="lead8_h2" ' . selected( $lead8_hero_title_wrapper, 'lead8_h2', false ) . '> ' . __( 'H2', 'leadeight' ) . '</option>';
echo ' <option value="lead8_h3" ' . selected( $lead8_hero_title_wrapper, 'lead8_h3', false ) . '> ' . __( 'H3', 'leadeight' ) . '</option>';
echo ' <option value="lead8_h4" ' . selected( $lead8_hero_title_wrapper, 'lead8_h4', false ) . '> ' . __( 'H4', 'leadeight' ) . '</option>';
echo ' <option value="lead8_h5" ' . selected( $lead8_hero_title_wrapper, 'lead8_h5', false ) . '> ' . __( 'H5', 'leadeight' ) . '</option>';
echo ' <option value="lead8_h6" ' . selected( $lead8_hero_title_wrapper, 'lead8_h6', false ) . '> ' . __( 'H6', 'leadeight' ) . '</option>';
echo ' <option value="lead8_p" ' . selected( $lead8_hero_title_wrapper, 'lead8_p', false ) . '> ' . __( 'P', 'leadeight' ) . '</option>';
echo ' <option value="lead8_span" ' . selected( $lead8_hero_title_wrapper, 'lead8_span', false ) . '> ' . __( 'Span', 'leadeight' ) . '</option>';
echo ' </select>';
echo ' <p class="description">' . __( 'What will the first headline be wrapped around?', 'leadeight' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="lead8_hero-title" class="lead8_hero-title_label">' . __( 'Hero Title', 'leadeight' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="lead8_hero_title" name="lead8_hero-title" class="lead8_hero_title_field" placeholder="' . esc_attr__( '', 'leadeight' ) . '" value="' . esc_attr( $lead8_hero_title ) . '">';
echo ' <p class="description">' . __( 'Title displayed over the hero', 'leadeight' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="lead8_sub-header-wrapper" class="lead8_sub-header-wrapper_label">' . __( 'Sub Header Wrapper', 'leadeight' ) . '</label></th>';
echo ' <td>';
echo ' <select id="lead8_sub_header_wrapper" name="lead8_sub-header-wrapper" class="lead8_sub_header_wrapper_field">';
echo ' <option value="lead8_span" ' . selected( $lead8_sub_header_wrapper, 'lead8_span', false ) . '> ' . __( 'Span', 'leadeight' ) . '</option>';
echo ' <option value="lead8_h1" ' . selected( $lead8_sub_header_wrapper, 'lead8_h1', false ) . '> ' . __( 'H1', 'leadeight' ) . '</option>';
echo ' <option value="lead8_h2" ' . selected( $lead8_sub_header_wrapper, 'lead8_h2', false ) . '> ' . __( 'H2', 'leadeight' ) . '</option>';
echo ' <option value="lead8_h3" ' . selected( $lead8_sub_header_wrapper, 'lead8_h3', false ) . '> ' . __( 'H3', 'leadeight' ) . '</option>';
echo ' <option value="lead8_h4" ' . selected( $lead8_sub_header_wrapper, 'lead8_h4', false ) . '> ' . __( 'H4', 'leadeight' ) . '</option>';
echo ' <option value="lead8_h5" ' . selected( $lead8_sub_header_wrapper, 'lead8_h5', false ) . '> ' . __( 'H5', 'leadeight' ) . '</option>';
echo ' <option value="lead8_h6" ' . selected( $lead8_sub_header_wrapper, 'lead8_h6', false ) . '> ' . __( 'H6', 'leadeight' ) . '</option>';
echo ' <option value="lead8_p" ' . selected( $lead8_sub_header_wrapper, 'lead8_p', false ) . '> ' . __( 'P', 'leadeight' ) . '</option>';
echo ' <option value="lead8_" ' . selected( $lead8_sub_header_wrapper, 'lead8_', false ) . '> ' . __( '', 'leadeight' ) . '</option>';
echo ' </select>';
echo ' <p class="description">' . __( 'The text wrapper below the title', 'leadeight' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="lead8_sub-header-text" class="lead8_sub-header-text_label">' . __( 'Sub Hero Text', 'leadeight' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="lead8_sub_header_text" name="lead8_sub-header-text" class="lead8_sub_header_text_field" placeholder="' . esc_attr__( '', 'leadeight' ) . '" value="' . esc_attr( $lead8_sub_header_text ) . '">';
echo ' <p class="description">' . __( 'The text below the main title', 'leadeight' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="lead8_cta-1-text" class="lead8_cta-1-text_label">' . __( 'Call to Action 1 Text', 'leadeight' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="lead8_cta_1_text" name="lead8_cta-1-text" class="lead8_cta_1_text_field" placeholder="' . esc_attr__( '', 'leadeight' ) . '" value="' . esc_attr( $lead8_cta_1_text ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="lead8_cta-1-url" class="lead8_cta-1-url_label">' . __( 'Call to Action Link', 'leadeight' ) . '</label></th>';
echo ' <td>';
echo ' <input type="url" id="lead8_cta_1_url" name="lead8_cta-1-url" class="lead8_cta_1_url_field" placeholder="' . esc_attr__( '', 'leadeight' ) . '" value="' . esc_attr( $lead8_cta_1_url ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="lead8_cta-2-text" class="lead8_cta-2-text_label">' . __( 'Call to Action 2 Text', 'leadeight' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="lead8_cta_2_text" name="lead8_cta-2-text" class="lead8_cta_2_text_field" placeholder="' . esc_attr__( '', 'leadeight' ) . '" value="' . esc_attr( $lead8_cta_2_text ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="lead8_cta-2-link" class="lead8_cta-2-link_label">' . __( 'Call to Action 2 Link', 'leadeight' ) . '</label></th>';
echo ' <td>';
echo ' <input type="url" id="lead8_cta_2_link" name="lead8_cta-2-link" class="lead8_cta_2_link_field" placeholder="' . esc_attr__( '', 'leadeight' ) . '" value="' . esc_attr( $lead8_cta_2_link ) . '">';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// 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.
$lead8_new_hero_title_wrapper = isset( $_POST[ 'lead8_hero-title-wrapper' ] ) ? $_POST[ 'lead8_hero-title-wrapper' ] : '';
$lead8_new_hero_title = isset( $_POST[ 'lead8_hero-title' ] ) ? sanitize_text_field( $_POST[ 'lead8_hero-title' ] ) : '';
$lead8_new_sub_header_wrapper = isset( $_POST[ 'lead8_sub-header-wrapper' ] ) ? $_POST[ 'lead8_sub-header-wrapper' ] : '';
$lead8_new_sub_header_text = isset( $_POST[ 'lead8_sub-header-text' ] ) ? sanitize_text_field( $_POST[ 'lead8_sub-header-text' ] ) : '';
$lead8_new_cta_1_text = isset( $_POST[ 'lead8_cta-1-text' ] ) ? sanitize_text_field( $_POST[ 'lead8_cta-1-text' ] ) : '';
$lead8_new_cta_1_url = isset( $_POST[ 'lead8_cta-1-url' ] ) ? esc_url( $_POST[ 'lead8_cta-1-url' ] ) : '';
$lead8_new_cta_2_text = isset( $_POST[ 'lead8_cta-2-text' ] ) ? sanitize_text_field( $_POST[ 'lead8_cta-2-text' ] ) : '';
$lead8_new_cta_2_link = isset( $_POST[ 'lead8_cta-2-link' ] ) ? esc_url( $_POST[ 'lead8_cta-2-link' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'lead8_hero-title-wrapper', $lead8_new_hero_title_wrapper );
update_post_meta( $post_id, 'lead8_hero-title', $lead8_new_hero_title );
update_post_meta( $post_id, 'lead8_sub-header-wrapper', $lead8_new_sub_header_wrapper );
update_post_meta( $post_id, 'lead8_sub-header-text', $lead8_new_sub_header_text );
update_post_meta( $post_id, 'lead8_cta-1-text', $lead8_new_cta_1_text );
update_post_meta( $post_id, 'lead8_cta-1-url', $lead8_new_cta_1_url );
update_post_meta( $post_id, 'lead8_cta-2-text', $lead8_new_cta_2_text );
update_post_meta( $post_id, 'lead8_cta-2-link', $lead8_new_cta_2_link );
}
}
new hero_title;