Skip to content

Instantly share code, notes, and snippets.

@wpmark
Created March 24, 2022 15:56
Show Gist options
  • Save wpmark/66c52ac9bd46e32fc1db30e409652c98 to your computer and use it in GitHub Desktop.
Save wpmark/66c52ac9bd46e32fc1db30e409652c98 to your computer and use it in GitHub Desktop.
Add a placeholder image ID setting in WordPress, under Settings > Media
<?php
/**
* Registers any additional settings required for the plugin.
*/
/**
* Registers the setting for the placeholder attachment ID.
*/
function hd_utility_register_image_placeholder_id_setting() {
register_setting(
'media',
'hd_utility_placeholder_image_id',
);
}
add_action( 'admin_init', 'hd_utility_register_image_placeholder_id_setting' );
/**
* Adds the setting to the media page in the WP admin area.
*/
function hd_utility_add_placeholder_id_setting() {
add_settings_field(
'srgroup-utility-placeholder-id',
__( 'Placeholder Image ID', 'srgroup-utility' ),
'hd_utility_placeholder_id_setting_output',
'media',
'default',
array(
'label_for' => 'srgroup-placeholder-id',
)
);
}
add_action( 'admin_init', 'hd_utility_add_placeholder_id_setting' );
/**
* Outputs the settings field for the placeholder image ID.
*/
function hd_utility_placeholder_id_setting_output() {
?>
<input type="text" name="<?php echo esc_attr( 'hd_utility_placeholder_image_id' ); ?>" id="<?php echo esc_attr( 'hd_utility_placeholder_image_id' ); ?>" class="regular-text" value="<?php echo esc_attr( get_option( 'hd_utility_placeholder_image_id' ) ); ?>" />
<p class="description"><?php esc_html_e( 'Enter the attachment ID of the image to be used as a placeholder', 'srgroup-utility' ); ?></p>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment