custom_hero_html
class custom_hero_html {
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(
'custom_hero_html',
'Custom Hero HTML',
array( $this, 'render_metabox' ),
'page',
'advanced',
'high'
);
}
public function render_metabox( $post ) {
// Add nonce for security and authentication.
wp_nonce_field( 'custom_hero_html_nonce_action', 'custom_hero_html_nonce' );
// Retrieve an existing value from the database.
$custom_hero_html_custom_hero_html = get_post_meta( $post->ID, 'custom_hero_html_custom_hero_html', true );
// Set default values.
if( empty( $custom_hero_html_custom_hero_html ) ) $custom_hero_html_custom_hero_html = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="custom_hero_html_custom_hero_html" class="custom_hero_html_custom_hero_html_label">' . 'Custom Hero HTML' . '</label></th>';
echo ' <td>';
echo ' <textarea id="custom_hero_html_custom_hero_html" name="custom_hero_html_custom_hero_html" class="custom_hero_html_custom_hero_html_field" placeholder="' . 'Enter custom HTML' . '">' . $custom_hero_html_custom_hero_html . '</textarea>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Add nonce for security and authentication.
$nonce_name = $_POST['custom_hero_html_nonce'];
$nonce_action = 'custom_hero_html_nonce_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.
$custom_hero_html_new_custom_hero_html = isset( $_POST[ 'custom_hero_html_custom_hero_html' ] ) ? sanitize_text_field( $_POST[ 'custom_hero_html_custom_hero_html' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'custom_hero_html_custom_hero_html', $custom_hero_html_new_custom_hero_html );
}
}
new custom_hero_html;