Skip to content

Commit

Permalink
add settings pages and options
Browse files Browse the repository at this point in the history
  • Loading branch information
followfung committed Oct 9, 2019
1 parent 9cbae16 commit 6ab7987
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 3 deletions.
113 changes: 113 additions & 0 deletions inc/rl-collapsible-section-settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* Add a custom options page.
*/
function rl_collapsible_section_add_options_page() {
add_options_page(
__( 'Collapsible Section Shortcode Settings', 'rl_collapsible_section' ),
__( 'Collapsible Section Shortcode Settings', 'rl_collapsible_section' ),
'manage_options',
'rl_collapsible_section',
'rl_collapsible_section_render_options_page_callback'
);
}
add_action( 'admin_menu', 'rl_collapsible_section_add_options_page' );

/**
* Callback function to render custom options page.
*/
function rl_collapsible_section_render_options_page_callback() {
?>
<form method="POST" action="options.php">
<?php
settings_fields( 'rl_collapsible_section' );
do_settings_sections( 'rl_collapsible_section' );
submit_button();
?>
</form>
<?php
}

/**
* Register and initialize settings for the plugin.
*/
function rl_collapsible_section_settings_init() {
$settings_section = 'rl_collapsible_section-main';
$settings_page = 'rl_collapsible_section';

add_settings_section(
$settings_section,
'Settings for Collapsible Section Shortcode plugin',
'rl_collapsible_section_settings_section_main_callback',
$settings_page
);

add_settings_field(
'rl_collapsible_section-default_title_tag',
'Default Title Tag',
'rl_collapsible_section_default_title_tag_callback',
$settings_page,
$settings_section,
array( 'label_for' => 'rl_collapsible_section-default_title_tag' )
);

$default_title_tag_args = array(
'type' => 'string',
'default' => 'h2',
'description' => 'Default tag used for the title of a collapsible section.'
);
register_setting($settings_page, 'rl_collapsible_section-default_title_tag', $default_title_tag_args);

add_settings_field(
'rl_collapsible_section-expand_first_collapsible',
'Expand First Collapsible',
'rl_collapsible_section_expand_first_collapsible_callback',
$settings_page,
$settings_section,
array( 'label_for' => 'rl_collapsible_section-expand_first_collapsible' )
);

$expand_first_collapsible_args = array(
'type' => 'boolean',
'default' => true,
'description' => 'Whether or not to expand the first collapsible section.'
);
register_setting($settings_page, 'rl_collapsible_section-expand_first_collapsible', $expand_first_collapsible_args);
}
add_action( 'admin_init', 'rl_collapsible_section_settings_init' );

/**
* Callback function to render settings section html
*/
function rl_collapsible_section_settings_section_main_callback() {
echo '';
}

/**
* Callback functions to render settings
*/
function rl_collapsible_section_default_title_tag_callback( $args ) {
$setting_id = 'rl_collapsible_section-default_title_tag';
$setting_value = esc_attr( get_option( 'rl_collapsible_section-default_title_tag' ) );

echo <<<setting_html
<input type="text" id="{$setting_id}" name="{$setting_id}" value="{$setting_value}" />
<p class="description">{$args[0]}</p>
setting_html;
}

function rl_collapsible_section_expand_first_collapsible_callback( $args ) {
$setting_id = 'rl_collapsible_section-expand_first_collapsible';
$setting_value = esc_attr( get_option( 'rl_collapsible_section-expand_first_collapsible' ) );

if ($setting_value) {
$checked = 'checked';
} else {
$checked = 'unchecked';
}

echo <<<setting_html
<input type="checkbox" id="{$setting_id}" name="{$setting_id}" checked="{$checked}" />
<p class="description">{$args[0]}</p>
setting_html;
}
12 changes: 9 additions & 3 deletions rl-collapsible-section.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
* Version: 0.1.5
*/

// Include our custom settings page for the plugin
require_once plugin_dir_path( __FILE__ ).'/inc/rl-collapsible-section-settings.php';

// Register styles and scripts to be used later when needed
function rl_collapsible_section_register_scripts() {
wp_register_style( 'rl-collapsible-section-style', plugin_dir_url( __FILE__ ).'/css/rl-collapsible-section.css', array(), '0.0.7' );
Expand All @@ -29,7 +32,7 @@ function rl_collapsible_section_shortcode($atts = [], $content = null) {
// override default attributes with user attributes
$shortcode_atts = shortcode_atts([
'title' => 'Collapsible section',
'title-tag' => 'h1',
'title-tag' => esc_attr( get_option( 'rl_collapsible_section-default_title_tag', 'h2' ) ),
'collapsed' => 'yes'
], $atts, 'rl_collapsible_section');

Expand All @@ -38,9 +41,9 @@ function rl_collapsible_section_shortcode($atts = [], $content = null) {
$collapsed = $shortcode_atts['collapsed'] == 'yes';

// Forces the first collapsible section to be expanded
if ($rl_collapsible_section_first_instance) {
$expand_first_collapsible = esc_attr( get_option( 'rl_collapsible_section-expand_first_collapsible', true ) );
if ($rl_collapsible_section_first_instance && $expand_first_collapsible) {
$collapsed = false;
$rl_collapsible_section_first_instance = false;
}

$collapsible_section_classes = '';
Expand All @@ -55,6 +58,9 @@ function rl_collapsible_section_shortcode($atts = [], $content = null) {
$output .= "<div class=\"rl-collapsible-section-content\">{$content}</div>";
$output .= "</div>";

// Flag for determining if this is the first instance of [rl_collapsible_section]
$rl_collapsible_section_first_instance = false;

return do_shortcode($output);
}
add_shortcode('rl_collapsible_section', 'rl_collapsible_section_shortcode');
Expand Down

0 comments on commit 6ab7987

Please sign in to comment.