Metabox
class google_adwords_options {
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(
'google_adwords_options',
__( 'Google Adwords Options', 'google_adwords_prev' ),
array( $this, 'render_metabox' ),
'post',
'advanced',
'default'
);
}
public function render_metabox( $post ) {
// Add nonce for security and authentication.
wp_nonce_field( 'custom_nonce_action', 'custom_nonce' );
// Retrieve an existing value from the database.
$custom_language = get_post_meta( $post->ID, 'custom_language', true );
$custom_devices = get_post_meta( $post->ID, 'custom_devices', true );
// Set default values.
if( empty( $custom_language ) ) $custom_language = '';
if( empty( $custom_devices ) ) $custom_devices = array();
// Form fields.
echo '<table class="form-table">';
echo ' <tr>';
echo ' <th><label for="custom_language" class="custom_language_label">' . __( 'Sprache', 'google_adwords_prev' ) . '</label></th>';
echo ' <td>';
echo ' <label><input type="radio" name="custom_language" class="custom_language_field" value="custom_german" ' . checked( $custom_language, 'custom_german', false ) . '> ' . __( 'Deutsch', 'google_adwords_prev' ) . '</label><br>';
echo ' <label><input type="radio" name="custom_language" class="custom_language_field" value="custom_english" ' . checked( $custom_language, 'custom_english', false ) . '> ' . __( 'Englisch', 'google_adwords_prev' ) . '</label><br>';
echo ' <p class="description">' . __( 'Wählen Sie eine Sprache', 'google_adwords_prev' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo ' <tr>';
echo ' <th><label for="custom_devices" class="custom_devices_label">' . __( 'Geräte', 'google_adwords_prev' ) . '</label></th>';
echo ' <td>';
echo ' <label><input type="checkbox" name="custom_devices[]" class="custom_devices_field" value="' . esc_attr( 'mobile' ) . '" ' . ( in_array( 'mobile', $custom_devices )? 'checked="checked"' : '' ) . '> ' . __( 'Mobile Device', 'google_adwords_prev' ) . '</label><br>';
echo ' <label><input type="checkbox" name="custom_devices[]" class="custom_devices_field" value="' . esc_attr( 'desktop' ) . '" ' . ( in_array( 'desktop', $custom_devices )? 'checked="checked"' : '' ) . '> ' . __( 'Desktop Device', 'google_adwords_prev' ) . '</label><br>';
echo ' <p class="description">' . __( 'Wählen Sie die Geräte', 'google_adwords_prev' ) . '</p>';
echo ' </td>';
echo ' </tr>';
echo '</table>';
}
public function save_metabox( $post_id, $post ) {
// Add nonce for security and authentication.
$nonce_name = isset( $_POST['custom_nonce'] ) ? $_POST['custom_nonce'] : '';
$nonce_action = 'custom_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;
// Sanitize user input.
$custom_new_language = isset( $_POST[ 'custom_language' ] ) ? $_POST[ 'custom_language' ] : '';
$custom_new_devices = isset( $_POST[ 'custom_devices' ] ) ? array_intersect( (array) $_POST[ 'custom_devices' ], array( 'mobile','desktop' ) ) : array();
// Update the meta field in the database.
update_post_meta( $post_id, 'custom_language', $custom_new_language );
update_post_meta( $post_id, 'custom_devices', $custom_new_devices );
}
}
new google_adwords_options;