hlavnistrana
class HlavniStrana {
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(
'hlavnistrana',
__( 'Informace na hlavní straně', 'text_domain' ),
array( $this, 'render_metabox' ),
'post',
'advanced',
'default'
);
}
public function render_metabox( $post ) {
// Retrieve an existing value from the database.
$tabor_podnadpis = get_post_meta( $post->ID, 'tabor_podnadpis', true );
$tabor_vyber = get_post_meta( $post->ID, 'tabor_vyber', true );
$tabor_obrazek = get_post_meta( $post->ID, 'tabor_obrazek', true );
// Set default values.
if( empty( $tabor_podnadpis ) ) $tabor_podnadpis = '';
if( empty( $tabor_vyber ) ) $tabor_vyber = '';
if( empty( $tabor_obrazek ) ) $tabor_obrazek = '';
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="tabor_podnadpis" class="tabor_podnadpis_label">' . __( 'Podnadpis', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="text" id="tabor_podnadpis" name="tabor_podnadpis" class="tabor_podnadpis_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr__( $tabor_podnadpis ) . '">';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="tabor_vyber" class="tabor_vyber_label">' . __( 'Zobrazit výběr z', 'text_domain' ) . '</label></th>';
echo ' <td>';
wp_dropdown_categories( array( 'id' => 'tabor_vyber', 'name' => 'tabor_vyber', 'class' => 'tabor_vyber_field', 'selected' => $tabor_vyber ) );
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="tabor_obrazek" class="tabor_obrazek_label">' . __( 'Obrázek sekce o nás', 'text_domain' ) . '</label></th>';
echo ' <td>';
echo ' <input type="url" id="tabor_obrazek" name="tabor_obrazek" class="tabor_obrazek_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr__( $tabor_obrazek ) . '">';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Sanitize user input.
$tabor_new_podnadpis = isset( $_POST[ 'tabor_podnadpis' ] ) ? sanitize_text_field( $_POST[ 'tabor_podnadpis' ] ) : '';
$tabor_new_vyber = isset( $_POST[ 'tabor_vyber' ] ) ? sanitize_text_field( $_POST[ 'tabor_vyber' ] ) : '';
$tabor_new_obrazek = isset( $_POST[ 'tabor_obrazek' ] ) ? esc_url( $_POST[ 'tabor_obrazek' ] ) : '';
// Update the meta field in the database.
update_post_meta( $post_id, 'tabor_podnadpis', $tabor_new_podnadpis );
update_post_meta( $post_id, 'tabor_vyber', $tabor_new_vyber );
update_post_meta( $post_id, 'tabor_obrazek', $tabor_new_obrazek );
}
}
new HlavniStrana;