Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Additional script options for the Genesis Framework
<?php
/**
* Create additional scripts options for the Genesis Framework.
*
* Genesis provides site wide script options out of the box. It also provides
* the ability to set script options on a per page/post basis.
*
* It does not provide the ability to control before/after script options
* globally for posts and/or pages, which is what we provide below.
*/
/**
* Defaults
*
* @since 1.0.0
* @param array $defaults
* @return array
*/
function ja_genesis_additional_scripts_defaults( $defaults ) {
$defaults['page_before'] = '';
$defaults['page_after'] = '';
$defaults['post_before'] = '';
$defaults['page_after'] = '';
return $defaults;
}
add_filter( 'genesis_theme_settings_defaults', 'ja_genesis_additional_scripts_defaults' );
/**
* Sanitization options
*
* @since 1.0.0
*/
function ja_genesis_additional_scripts_sanitization() {
genesis_add_option_filter( 'requires_unfiltered_html', GENESIS_SETTINGS_FIELD,
array(
'page_before',
'page_after',
'post_before',
'post_after',
) );
}
add_action( 'genesis_settings_sanitizer_init', 'ja_genesis_additional_scripts_sanitization' );
/**
* Register Metabox
*
* @since 1.0.0
* @param string $_genesis_theme_settings_pagehook
*/
function ja_genesis_additional_scripts_metabox_register( $_genesis_theme_settings_pagehook ) {
add_meta_box('ja-genesis-additional-scripts', 'Additional Scripts', 'ja_genesis_additional_scripts_metabox_render', $_genesis_theme_settings_pagehook, 'main', 'low');
}
add_action('genesis_theme_settings_metaboxes', 'ja_genesis_additional_scripts_metabox_register');
/**
* Render Metabox
*
* @since 1.0.0
*/
function ja_genesis_additional_scripts_metabox_render() {
?>
<div style="background-color:#eee;margin:-11px -10px 10px -10px;padding:8px 10px;font-weight:700;font-size:16px;">PAGES</div>
<p><label for="<?php echo GENESIS_SETTINGS_FIELD; ?>[page_before]">Enter scripts or code you would like output to <code>wp_head()</code> on pages:</label></p>
<textarea name="<?php echo GENESIS_SETTINGS_FIELD; ?>[page_before]" class="large-text" id="<?php echo GENESIS_SETTINGS_FIELD; ?>[page_before]" cols="78" rows="8"><?php echo esc_textarea( genesis_get_option( 'page_before' ) ); ?></textarea>
<hr class="div">
<p><label for="<?php echo GENESIS_SETTINGS_FIELD; ?>[page_after]">Enter scripts or code you would like output to <code>wp_footer()</code> on pages:</label></p>
<textarea name="<?php echo GENESIS_SETTINGS_FIELD; ?>[page_after]" class="large-text" id="<?php echo GENESIS_SETTINGS_FIELD; ?>[page_after]" cols="78" rows="8"><?php echo esc_textarea( genesis_get_option( 'page_after' ) ); ?></textarea>
<div style="background-color:#eee;margin: 10px -10px 10px -10px;padding:8px 10px;font-weight:700;font-size:16px;">POSTS</div>
<p><label for="<?php echo GENESIS_SETTINGS_FIELD; ?>[post_before]">Enter scripts or code you would like output to <code>wp_head()</code> on posts:</label></p>
<textarea name="<?php echo GENESIS_SETTINGS_FIELD; ?>[post_before]" class="large-text" id="<?php echo GENESIS_SETTINGS_FIELD; ?>[post_before]" cols="78" rows="8"><?php echo esc_textarea( genesis_get_option( 'post_before' ) ); ?></textarea>
<hr class="div">
<p><label for="<?php echo GENESIS_SETTINGS_FIELD; ?>[post_after]">Enter scripts or code you would like output to <code>wp_footer()</code> on posts:</label></p>
<textarea name="<?php echo GENESIS_SETTINGS_FIELD; ?>[post_after]" class="large-text" id="<?php echo GENESIS_SETTINGS_FIELD; ?>[post_after]" cols="78" rows="8"><?php echo esc_textarea( genesis_get_option( 'post_after' ) ); ?></textarea>
<?php
}
/**
* Add after scripts
*
* @since 1.0.0
*/
function ja_genesis_additional_scripts_after() {
if ( is_page() ) {
echo genesis_get_option( 'page_after' );
}
if ( is_singular( 'post' ) ) {
echo genesis_get_option( 'post_after' );
}
}
add_action('wp_footer', 'ja_genesis_additional_scripts_after', 30 );
/**
* Add before scripts
*
* @since 1.0.0
*/
function ja_genesis_additional_scripts_before() {
if ( is_page() ) {
echo genesis_get_option( 'page_before' );
}
if ( is_singular( 'post' ) ) {
echo genesis_get_option( 'post_before' );
}
}
add_action( 'wp_head', 'ja_genesis_additional_scripts_before', 30 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment