Skip to content

Instantly share code, notes, and snippets.

@davidwolfpaw
Created August 3, 2017 13:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidwolfpaw/a1d61e33b6a6effc1015c41a33929006 to your computer and use it in GitHub Desktop.
Save davidwolfpaw/a1d61e33b6a6effc1015c41a33929006 to your computer and use it in GitHub Desktop.
settings page options for ccpr
/**
* Initializes the general settings by registering the Sections, Fields, and Settings.
*
* This function is registered with the 'admin_init' hook.
*/
public function initialize_general_settings() {
// Control title of republish
// Set Terms text
// Choose CC license to use
// Choose whether to display at end of post or not
add_settings_section(
'general_settings_section', // ID used to identify this section and with which to register options
__( 'General Settings', $this->plugin_name ), // Title to be displayed on the administration page
'', // Callback used to render the description of the section
'cc_post_republisher_general_settings' // Page on which to add this section of options
);
add_settings_field(
'republish_box_title',
__( 'Republish Box Title', $this->plugin_name ),
array( $this, 'text_input_callback'),
'cc_post_republisher_general_settings',
'general_settings_section',
array( 'label_for' => 'republish_box_title', 'option_group' => 'cc_post_republisher_general_settings', 'option_id' => 'republish_box_title' )
);
add_settings_field(
'termstext' ,
__( 'Terms Text', $this->plugin_name ),
array( $this, 'wp_editor_input_callback'),
'cc_post_republisher_general_settings',
'general_settings_section',
array( 'label_for' => 'termstext' , 'option_group' => 'cc_post_republisher_general_settings', 'option_id' => 'termstext' )
);
add_settings_field(
'license_type',
__( 'Creative Commons License Type', $this->plugin_name ),
array( $this, 'license_input_callback'),
'cc_post_republisher_general_settings',
'general_settings_section',
array( 'label_for' => 'license_type', 'option_group' => 'cc_post_republisher_general_settings', 'option_id' => 'license_type', 'option_description' => 'Select the license that you want to apply to your post content.' )
);
register_setting(
'cc_post_republisher_general_settings',
'cc_post_republisher_general_settings'
);
}
/**
* Input Callbacks
*/
public function text_input_callback( $text_input ) {
// Get arguments from setting
$option_group = $text_input['option_group'];
$option_id = $text_input['option_id'];
$option_name = "{$option_group}[{$option_id}]";
// Get existing option from database
$options = get_option( $option_group );
$option_value = isset( $options[$option_id] ) ? $options[$option_id] : '0';
// Render the output
echo "<input type='text' id='{$option_id}' name='{$option_name}' value='{$option_value}' />";
}
public function checkbox_input_callback( $checkbox_input ) {
// Get arguments from setting
$option_group = $checkbox_input['option_group'];
$option_id = $checkbox_input['option_id'];
$option_name = "{$option_group}[{$option_id}]";
$option_description = $checkbox_input['option_description'];
// Get existing option from database
$options = get_option( $option_group );
$option_value = isset( $options[$option_id] ) ? $options[$option_id] : "";
// Render the output
$input = '';
$input .= "<input type='checkbox' id='{$option_id}' name='{$option_name}' value='1' " . checked( $option_value, 1, false ) . " />";
$input .= "<label for='{$option_id}'>{$option_description}</label>";
echo $input;
}
public function wp_editor_input_callback( $wp_editor_input ) {
// Get existing option from database
$option_group = $wp_editor_input['option_group'];
$option_id = $wp_editor_input['option_id'];
$option_name = "{$option_group}[{$option_id}]";
$options = get_option( $option_group );
$content = isset( $options[$option_id] ) ? $options[$option_id] : "";
// Get arguments from setting
$settings = array(
'quicktags' => array( 'buttons' => 'strong,em,del,ul,ol,li,close' ),
'textarea_name' => $option_id,
);
// Render the output
wp_editor( $content, $option_id, $settings );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment