Include file if checkbox is checked
-
I’m relatively new to coding, I know the concept of a lot of WordPress functions/actions etc.
I’m building my own plug-in just for my own use and for learning.
I have the the plug-in working in the sense I have it displaying and installing correctly in the back end.. but what I want to do is once a check box has been checked, to do an action, which is to include a file from the same plugin dir /src/somefile.php
The section at the bottom that I’ve commented out isn’t working, I’ve lost my train of thought and just can’t figure it out.
Here’s my code:
<?php /* Plugin Name: Custom Functionality Plugin Description: enable/disable site specific functions Author: damion Founde Version: 1.0.0 */ class Custom_Functions_Plugin { public function __construct() { // Hook into the admin menu add_action( 'admin_menu', array( $this, 'create_plugin_settings_page' ) ); // Add Settings and Fields add_action( 'admin_init', array( $this, 'setup_sections' ) ); add_action( 'admin_init', array( $this, 'setup_fields' ) ); } public function create_plugin_settings_page() { // Add the menu item and page $page_title = 'Funconality Plugin'; $menu_title = 'Functions Plugin'; $capability = 'manage_options'; $slug = 'custom_functionality'; $callback = array( $this, 'plugin_settings_page_content' ); $icon = 'dashicons-admin-plugins'; $position = 100; add_menu_page( $page_title, $menu_title, $capability, $slug, $callback, $icon, $position ); } public function plugin_settings_page_content() {?> <div class="wrap"> <h2>Functionality Plugin</h2><?php if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] ){ $this->admin_notice(); } ?> <form method="POST" action="options.php"> <?php settings_fields( 'cf_fields' ); do_settings_sections( 'cf_fields' ); submit_button(); ?> </form> </div> <?php } public function admin_notice() { ?> <div class="notice notice-success is-dismissible"> <p>Your settings have been updated!</p> </div><?php } public function setup_sections() { add_settings_section( 'css_scripts_section', 'CSS and Scripts', array( $this, 'section_callback' ), 'cf_fields' ); add_settings_section( 'frontend_section', 'Front End Functions', array( $this, 'section_callback' ), 'cf_fields' ); add_settings_section( 'backend_section', 'Back End Functions', array( $this, 'section_callback' ), 'cf_fields' ); } public function section_callback( $arguments ) { switch( $arguments['id'] ){ case 'css_scripts_section': echo 'These functions will enqueue css and/or scripts'; break; case 'frontend_section': echo 'These Functions will modify how the end user sees features'; break; case 'backend_section': echo 'These Functions will modify features/aspects of the admin section.'; break; } } public function setup_fields() { $fields = array( array( 'uid' => 'cf_script_checkboxes', 'label' => 'Functions:', 'section' => 'css_scripts_section', 'type' => 'checkbox', 'options' => array( 'option1' => 'Option 1', 'option2' => 'Option 2', 'option3' => 'Option 3', 'option4' => 'Option 4', 'option5' => 'Option 5', ), 'default' => array() ), array( 'uid' => 'cf_fe_heckboxes', 'label' => 'Functions:', 'section' => 'frontend_section', 'type' => 'checkbox', 'options' => array( 'option1' => 'Option 1', 'option2' => 'Option 2', 'option3' => 'Option 3', 'option4' => 'Option 4', 'option5' => 'Option 5', ), 'default' => array() ), array( 'uid' => 'cf_be_checkboxes', 'label' => 'Functions:', 'section' => 'backend_section', 'type' => 'checkbox', 'options' => array( 'option1' => 'Option 1', 'option2' => 'Option 2', 'option3' => 'Option 3', 'option4' => 'Option 4', 'option5' => 'Option 5', ), 'default' => array() ), ); foreach( $fields as $field ){ add_settings_field( $field['uid'], $field['label'], array( $this, 'field_callback' ), 'cf_fields', $field['section'], $field ); register_setting( 'cf_fields', $field['uid'] ); } } public function field_callback( $arguments ) { $value = get_option( $arguments['uid'] ); if( ! $value ) { $value = $arguments['default']; } switch( $arguments['type'] ){ case 'checkbox': if( ! empty ( $arguments['options'] ) && is_array( $arguments['options'] ) ){ $options_markup = ''; $iterator = 0; foreach( $arguments['options'] as $key => $label ){ $iterator++; $options_markup .= sprintf( '<label for="%1$s_%6$s"><input id="%1$s_%6$s" name="%1$s[]" type="%2$s" value="%3$s" %4$s /> %5$s</label><br/>', $arguments['uid'], $arguments['type'], $key, checked( $value[ array_search( $key, $value, true ) ], $key, false ), $label, $iterator ); } printf( '<fieldset>%s</fieldset>', $options_markup ); } break; } if( $helper = $arguments['helper'] ){ printf( '<span class="helper"> %s</span>', $helper ); } if( $supplimental = $arguments['supplimental'] ){ printf( '<p class="description">%s</p>', $supplimental ); } } } /*add_action('init', 'nd_handle_cb'); function nd_handle_cb() { $options = get_option( $arguments['cf_checkboxes'] ); get_option( $arguments['cf_checkboxes']); if ( $options['option1'] == '1' ) { //do something } else { //do nothing } return; } */ new Custom_Functions_Plugin();
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Include file if checkbox is checked’ is closed to new replies.