Site Builder
Editing:
BDO-Business-Card-Manager.txt
writable 0666
<?php /* Plugin Name: BDO Business Card Manager Description: Lets you manage business card data for your site, auto-generates JSON in your phone# static directory. Designed for /ph/{PHONE}/business.json Version: 1.0 Author: BestDealOn Dev Team Network: true */ if ( ! defined( 'ABSPATH' ) ) exit; // SETTINGS PAGE add_action('admin_menu', function() { add_menu_page( 'Business Card Manager', 'Business Card', 'manage_options', 'bdo_bc_manager', 'bdo_bc_manager_page', 'dashicons-id-alt', 23 ); }); function bdo_bc_manager_page() { $phone = bdo_bc_get_phone_digits(); $dir = bdo_bc_get_static_dir(); $json_file = trailingslashit($dir) . 'business.json'; // Handle save if ($_SERVER['REQUEST_METHOD'] === 'POST' && check_admin_referer('bdo_bc_save')) { $data = [ 'name' => sanitize_text_field($_POST['bc_name']), 'address' => sanitize_text_field($_POST['bc_address']), 'city' => sanitize_text_field($_POST['bc_city']), 'state' => sanitize_text_field($_POST['bc_state']), 'zip' => sanitize_text_field($_POST['bc_zip']), 'phone' => sanitize_text_field($_POST['bc_phone']), 'description' => sanitize_textarea_field($_POST['bc_description']), 'tags' => array_filter(array_map('trim', explode(',', $_POST['bc_tags']))), ]; // Auto-fill city/state from address if blank (very simple, adjust as needed) if (!$data['city'] && preg_match('/,\\s*([^,]+),\\s*\\w+\\s*\\d+$/', $data['address'], $m)) { $data['city'] = trim($m[1]); } if (!$data['state'] && preg_match('/,\\s*([^,]+)\\s*\\d+$/', $data['address'], $m)) { $data['state'] = trim($m[1]); } // Always add city/state/zip as location tags (even if not in business tags) $data['location_tags'] = array_filter(array_unique([ $data['city'], $data['state'], $data['zip'] ])); // Ensure directory exists if ( ! file_exists($dir) ) wp_mkdir_p($dir); // Write JSON file_put_contents($json_file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); echo '<div class="notice notice-success is-dismissible"><p>Business card saved to <code>' . esc_html($json_file) . '</code></p></div>'; } else if ( file_exists($json_file) ) { $data = json_decode(file_get_contents($json_file), true); } else { $data = []; } // Form UI ?> <div class="wrap"> <h1>Business Card Manager</h1> <form method="post"> <?php wp_nonce_field('bdo_bc_save'); ?> <table class="form-table"> <tr> <th><label for="bc_name">Business Name</label></th> <td><input name="bc_name" id="bc_name" type="text" class="regular-text" value="<?php echo esc_attr($data['name'] ?? ''); ?>" required></td> </tr> <tr> <th><label for="bc_address">Street Address</label></th> <td><input name="bc_address" id="bc_address" type="text" class="regular-text" value="<?php echo esc_attr($data['address'] ?? ''); ?>" required></td> </tr> <tr> <th><label for="bc_city">City</label></th> <td><input name="bc_city" id="bc_city" type="text" class="regular-text" value="<?php echo esc_attr($data['city'] ?? ''); ?>"></td> </tr> <tr> <th><label for="bc_state">State</label></th> <td><input name="bc_state" id="bc_state" type="text" class="regular-text" value="<?php echo esc_attr($data['state'] ?? ''); ?>"></td> </tr> <tr> <th><label for="bc_zip">Zip Code</label></th> <td><input name="bc_zip" id="bc_zip" type="text" class="regular-text" value="<?php echo esc_attr($data['zip'] ?? ''); ?>"></td> </tr> <tr> <th><label for="bc_phone">Phone</label></th> <td><input name="bc_phone" id="bc_phone" type="text" class="regular-text" value="<?php echo esc_attr($data['phone'] ?? ''); ?>" required></td> </tr> <tr> <th><label for="bc_description">Short Description</label></th> <td><textarea name="bc_description" id="bc_description" rows="3" class="large-text"><?php echo esc_textarea($data['description'] ?? ''); ?></textarea></td> </tr> <tr> <th><label for="bc_tags">Business Tags</label></th> <td><input name="bc_tags" id="bc_tags" type="text" class="regular-text" value="<?php echo esc_attr(implode(', ', $data['tags'] ?? [])); ?>" placeholder="e.g. RF Safe, Radiation Protection, Accessories"></td> </tr> </table> <?php submit_button('Save Business Card'); ?> </form> </div> <?php } // ==== HELPERS ==== function bdo_bc_get_phone_digits() { $blog_id = get_current_blog_id(); if ( function_exists('get_blog_meta') ) { $digits = (string) get_blog_meta($blog_id, 'pns_phone', true); if ( $digits && preg_match('/^\\d{10}$/', $digits) ) return $digits; } elseif ( function_exists('get_site_meta') ) { $digits = (string) get_site_meta($blog_id, 'pns_phone', true); if ( $digits && preg_match('/^\\d{10}$/', $digits) ) return $digits; } $details = get_blog_details($blog_id); if ( $details && preg_match('~/([0-9]{10})/~', $details->path, $m) ) return $m[1]; return '0000000000'; } function bdo_bc_get_static_dir() { return trailingslashit('/home/bestdealon/public_html/ph') . bdo_bc_get_phone_digits(); } // End plugin
Save changes
Create folder
writable 0777
Create
Cancel