nicolaswehmeyer
Forum Replies Created
-
Hello again,
I was able to finish the plugin by myself, but incase someone has a similar question here is what I did.
After trying different solutions I thought it might be the best to Ajax for that. WordPress provides us with a great solution to use Ajax within the admin area through admin-ajax.php. So we have to generate our page in the first step and then add Ajax functionalities to the buttons. The javascript then gets the chosen blog id from the html-select field and passes this information to the referenced ajax php function and also fills the form fields with the data from the database.
Then there is a second button that saves the new data back to the database. It is still not finished and the code is not fully secure yet but here is the basic code for you to get started:
Plugin-php-file:
<?php if ( is_admin() ){ /* Define vars */ $blogs = wp_get_sites(); /* Call the html code */ function test_location_admin_menu() { // Generate Admin Page add_menu_page('test location', 'location', 'manage_options', 'test_location', 'test_location_create_page', 'dashicons-location', 6); // Add Submenu Pages add_submenu_page('test_location', 'Standortoptionen', 'General', 'manage_options', 'test_location', 'test_location_create_page' ); // Initialize custom settings add_action('admin_init', 'register_test_location_settings'); } add_action('admin_menu', 'test_location_admin_menu'); // AJAX function test_location_load_scripts() { wp_enqueue_script( 'test-location-ajax', plugin_dir_url(__FILE__).'js/test-location-ajax.js', array('jquery')); } add_action('admin_enqueue_scripts', 'test_location_load_scripts'); function test_location_process_ajax_get() { // GET ACCESS TO DATABASE global $wpdb; $blog_id = intval( $_POST['blogId']); $blog_details = get_blog_details( $blog_id ); switch_to_blog( $blog_id ); $testStandortname = esc_attr(get_option('test_standortname')); $testAdresse = esc_attr(get_option('test_adresse')); $testOeffnungszeiten = esc_attr(get_option('test_oeffnungszeiten')); $testFootertext = esc_attr(get_option('test_footertext')); $data['result'] = $array = [ 'test_standortname' => $testStandortname, 'test_adresse' => $testAdresse, 'test_oeffnungszeiten' => $testOeffnungszeiten, 'test_footertext' => $testFootertext, ]; echo(json_encode($data)); restore_current_blog(); wp_die(); } add_action('wp_ajax_test_location_get_results', 'test_location_process_ajax_get'); function test_location_process_ajax_save() { global $wpdb; // GET VARS FROM AJAX $blog_id = intval( $_POST['blogId']); $locationName = $_POST['locationName']; $address = $_POST['address']; $businessHours = $_POST['businessHours']; $footerText = $_POST['footerText']; $blog_details = get_blog_details( $blog_id ); switch_to_blog( $blog_id ); update_option('test_standortname', $locationName ); update_option('test_adresse', $address ); update_option('test_oeffnungszeiten', $businessHours ); update_option('test_footertext', $footerText ); restore_current_blog(); wp_die(); } add_action('wp_ajax_test_location_save_results', 'test_location_process_ajax_save'); // SETTINGS API CONFIGURATION function register_test_location_settings() { // Register our settings register_setting( 'test-location-settings-group', 'test_standortname' ); register_setting( 'test-location-settings-group', 'test_adresse' ); register_setting( 'test-location-settings-group', 'test_oeffnungszeiten' ); register_setting( 'test-location-settings-group', 'test_footertext' ); // Add settings section add_settings_section( 'test-location-sidebar-options', 'Standort Stammdaten', 'test_sidebar_options', 'test_location'); // ADD SETTINGS FIELDS add_settings_field( 'sidebar-name', 'Standortname', 'test_sidebar_name', 'test_location', 'test-location-sidebar-options' ); add_settings_field( 'sidebar-adresse', 'Adresse', 'test_sidebar_adresse', 'test_location', 'test-location-sidebar-options' ); add_settings_field( 'sidebar-oeffnungszeiten', '?ffnungszeiten', 'test_sidebar_oeffnungszeiten', 'test_location', 'test-location-sidebar-options' ); add_settings_field( 'sidebar-footertext', 'Footer Text', 'test_sidebar_footertext', 'test_location', 'test-location-sidebar-options' ); } // GENERATE VIEWS function test_sidebar_options() { echo 'Füllen Sie die nachfolgender Felder bitte vollst?ndig aus:'; } function test_sidebar_name() { echo '<input type="text" id="test_standortname" name="test_standortname" />'; } function test_sidebar_adresse() { echo '<input type="text" id="test_adresse" name="test_adresse" />'; } function test_sidebar_oeffnungszeiten() { echo '<input type="text" id="test_oeffnungszeiten" name="test_oeffnungszeiten" />'; } function test_sidebar_footertext() { echo '<input type="text" id="test_footertext" name="test_footertext" />'; } // CREATE THE FINAL PAGE function test_location_create_page() { ?> <h1>test location</h1> <label>Standort ausw?hlen</label> <select id="test-location-select"> <?php global $blogs; // get all blogs if ( 0 < count( $blogs ) ) : foreach( $blogs as $blog ) : switch_to_blog( $blog[ 'blog_id' ] ); if ( get_theme_mod( 'show_in_home', 'on' ) !== 'on' ) { continue; } $blog_details = get_blog_details( $blog[ 'blog_id' ] ); ?> <option value="<?php echo $blog_details->blog_id ?>"><?php echo $blog_details->blogname; ?></option> <?php restore_current_blog(); endforeach; endif; ?> ?> </select> <?php submit_button( 'übernehmen', 'secondary', 'test-location-load-data', false ); ?> <img src="<?php echo admin_url('/images/spinner.gif'); ?>" class="loading" id="test-location-spinner" style="display:none" /> <form id="test-location-form" method="post"> <div id="test-location-results" style="display:none"> <?php settings_errors(); ?> <?php settings_fields('test-location-settings-group' ); ?> <?php do_settings_sections( 'test_location' ); ?> <?php submit_button( 'Daten aktualisieren', 'primary', 'test-location-save-data' ); ?> </div> </form> <?php } } // SHORTCODES function shortcode_sitename(){ return get_bloginfo('name'); } add_shortcode( 'mbp-site-name', 'shortcode_sitename' ); function test_location_shortcode() { global $wpdb; $blog_id = get_current_blog_id(); $result = $wpdb->get_var( 'SELECT option_value FROM mbpwp_'.$blog_id.'_options WHERE option_name = "test_standortname"' ); return $result; } add_shortcode('mbp-location-name', 'test_location_shortcode'); function test_address_shortcode() { global $wpdb; $blog_id = get_current_blog_id(); $result = $wpdb->get_var( 'SELECT option_value FROM mbpwp_'.$blog_id.'_options WHERE option_name = "test_adresse"' ); return $result; } add_shortcode('mbp-location-address', 'test_address_shortcode'); function test_businesshours_shortcode() { global $wpdb; $blog_id = get_current_blog_id(); $result = $wpdb->get_var( 'SELECT option_value FROM mbpwp_'.$blog_id.'_options WHERE option_name = "test_oeffnungszeiten"' ); return $result; } add_shortcode('mbp-business-hours', 'test_businesshours_shortcode'); function test_footertext_shortcode() { global $wpdb; $blog_id = get_current_blog_id(); $result = $wpdb->get_var( 'SELECT option_value FROM mbpwp_'.$blog_id.'_options WHERE option_name = "test_footertext"' ); return $result; } add_shortcode('mbp-footertext', 'test_footertext_shortcode'); ?>
Plugin-javascript-file:
jQuery(document).ready(function($) { // GET DATA FOR CURRENT $('#test-standorte-load-data').click(function() { // SHOW LOADING SPINNER $('#test-standorte-spinner').show(); // GET SELECTED FROM OPTIONS var selected = $( "#test-standorte-select" ).val(); data = { action: 'test_standorte_get_results', blogId: selected, }; $.post(ajaxurl, data, function(data) { // CONVERT RESPONSE TO JSON var response = $.parseJSON(data); // DEFINE VARS var standortname = response.result.test_standortname; var adresse = response.result.test_adresse; var oeffnungszeiten = response.result.test_oeffnungszeiten; var footertext = response.result.test_footertext; if(standortname === "") { $("#test_standortname").attr("placeholder", "Standortname")}else{ $("#test_standortname").attr("placeholder", ""); }; if(adresse === "") { $("#test_adresse").attr("placeholder", "Adresse")}else{ $("#test_adresse").attr("placeholder", ""); }; if(oeffnungszeiten === "") { $("#test_oeffnungszeiten").attr("placeholder", "?ffnungszeiten")}else{ $("#test_oeffnungszeiten").attr("placeholder", ""); }; if(footertext === "") { $("#test_footertext").attr("placeholder", "Footer Text")}else{ $("#test_footertext").attr("placeholder", ""); }; $('#test_standortname').val(standortname); $('#test_adresse').val(adresse); $('#test_oeffnungszeiten').val(oeffnungszeiten); $('#test_footertext').val(footertext); $('#test-standorte-results').fadeIn(); $('#test-standorte-spinner').hide(); }); return false; }); // SAVE DATA FOR CURRENT $('#test-standorte-save-data').click(function() { var selected = $( "#test-standorte-select" ).val(); var Name = $('#test_standortname').val(); var address = $('#test_adresse').val(); var businessHours = $('#test_oeffnungszeiten').val(); var footerText = $('#test_footertext').val(); alert(footerText); data = { action: 'test_standorte_save_results', blogId: selected, Name: Name, address: address, businessHours: businessHours, footerText: footerText, } $.post(ajaxurl, data, function(data) { alert('Data has been updated successfully'); }); return false; }); });
I hope this helps you guys.
Best regards,
NicolasForum: Plugins
In reply to: [Broadcast] Error with syncing menus (AddonPremium Pack)Ok I will do that.