Skip to content

Instantly share code, notes, and snippets.

@olawanlejoel
Created August 19, 2025 12:32
Show Gist options
  • Save olawanlejoel/964409d74b71fb98e36b56179b8b09ec to your computer and use it in GitHub Desktop.
Save olawanlejoel/964409d74b71fb98e36b56179b8b09ec to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: Font Weight Options
* Description: Manage custom font weights for body text, headings, and code elements.
*/
// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
// Main font weight options class.
class Font_Weight_Options {
// Constructor.
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
add_action( 'wp_head', array( $this, 'apply_font_weights_frontend' ) );
add_action( 'admin_head', array( $this, 'apply_font_weights_admin' ) );
register_activation_hook( __FILE__, array( $this, 'activate' ) );
}
// Plugin activation.
public function activate() {
// Set default font weights.
$default_weights = array(
'body' => 400,
'headings' => 600,
'code' => 400,
);
if ( ! get_option( 'fwo_font_weights' ) ) {
add_option( 'fwo_font_weights', $default_weights );
}
}
// Add admin menu.
public function add_admin_menu() {
add_theme_page(
__( 'Font Weight Settings', 'font-weight-options' ),
__( 'Font Weight', 'font-weight-options' ),
'manage_options',
'font-weight-settings',
array( $this, 'render_admin_page' )
);
}
// Render admin page.
public function render_admin_page() {
// Handle form submission.
if ( isset( $_POST['submit_font_weights'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'fwo_font_weights_nonce' ) ) {
$font_weights = array(
'body' => absint( $_POST['font_weights']['body'] ?? 400 ),
'headings' => absint( $_POST['font_weights']['headings'] ?? 600 ),
'code' => absint( $_POST['font_weights']['code'] ?? 400 ),
);
// Validate ranges.
foreach ( $font_weights as $key => $weight ) {
if ( $weight < 100 || $weight > 900 ) {
$font_weights[ $key ] = ( $key === 'headings' ) ? 600 : 400;
}
}
update_option( 'fwo_font_weights', $font_weights );
echo '<div class="notice notice-success"><p>' .
__( 'Font weights updated successfully!', 'font-weight-options' ) .
'</p></div>';
}
$saved_weights = get_option( 'fwo_font_weights', array(
'body' => 400,
'headings' => 600,
'code' => 400,
) );
?>
<div class="wrap">
<h1><?php _e( 'Font Weight Settings', 'font-weight-options' ); ?></h1>
<p><?php _e( 'Configure custom font weights for your theme. Enter values between 100–900.', 'font-weight-options' ); ?></p>
<form method="post" action="">
<?php wp_nonce_field( 'fwo_font_weights_nonce' ); ?>
<table class="form-table">
<tr>
<th scope="row"><?php _e( 'Body Text (Manrope)', 'font-weight-options' ); ?></th>
<td>
<input type="number" name="font_weights[body]"
value="<?php echo esc_attr( $saved_weights['body'] ); ?>"
min="100" max="900" step="1" />
<p class="description"><?php _e( 'Default: 400 (Regular)', 'font-weight-options' ); ?></p>
</td>
</tr>
<tr>
<th scope="row"><?php _e( 'Headings (Vollkorn)', 'font-weight-options' ); ?></th>
<td>
<input type="number" name="font_weights[headings]"
value="<?php echo esc_attr( $saved_weights['headings'] ); ?>"
min="100" max="900" step="1" />
<p class="description"><?php _e( 'Default: 600 (Semi Bold)', 'font-weight-options' ); ?></p>
</td>
</tr>
<tr>
<th scope="row"><?php _e( 'Code Text (Fira Code)', 'font-weight-options' ); ?></th>
<td>
<input type="number" name="font_weights[code]"
value="<?php echo esc_attr( $saved_weights['code'] ); ?>"
min="100" max="900" step="1" />
<p class="description"><?php _e( 'Default: 400 (Regular)', 'font-weight-options' ); ?></p>
</td>
</tr>
</table>
<?php submit_button( __( 'Save Font Weights', 'font-weight-options' ), 'primary', 'submit_font_weights' ); ?>
</form>
</div>
<?php
}
// Apply custom font weights to the frontend.
public function apply_font_weights_frontend() {
$this->output_font_weight_css( 'fwo-frontend-styles', false );
}
// Apply custom font weights to the editor.
public function apply_font_weights_admin() {
$this->output_font_weight_css( 'fwo-admin-styles', true );
}
// Generate and output font weight CSS.
private function output_font_weight_css( $style_id, $include_admin = false ) {
$font_weights = get_option( 'fwo_font_weights' );
if ( ! $font_weights ) {
return;
}
$body_weight = absint( $font_weights['body'] ?? 400 );
$headings_weight = absint( $font_weights['headings'] ?? 600 );
$code_weight = absint( $font_weights['code'] ?? 400 );
$css = "
body { font-weight: {$body_weight} !important; }
h1, h2, h3, h4, h5, h6 { font-weight: {$headings_weight} !important; }
code, pre { font-weight: {$code_weight} !important; }
";
if ( $include_admin ) {
$css .= "
.editor-styles-wrapper body { font-weight: {$body_weight} !important; }
.editor-styles-wrapper h1,
.editor-styles-wrapper h2,
.editor-styles-wrapper h3,
.editor-styles-wrapper h4,
.editor-styles-wrapper h5,
.editor-styles-wrapper h6 { font-weight: {$headings_weight} !important; }
.editor-styles-wrapper code,
.editor-styles-wrapper pre { font-weight: {$code_weight} !important; }
";
}
echo '<style id="' . esc_attr( $style_id ) . '">' . esc_html( $css ) . '</style>';
}
}
// Initialize the plugin.
new Font_Weight_Options();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment