IT技术家园 - 插件
type: channel
link: https://www.itbunan.xyz/tag/%E6%8F%92%E4%BB%B6/
items: wordpress插件开发-自定义插件设置页
items: wordpress插件开发基础知识（二）

wordpress插件开发-自定义插件设置页
type: item
date: 2023-04-25T16:08:39+00:00
description: 前言插件越来越复杂，原先一个插件只包括一个功能。最近把各个小功能集成到一起。需要一个插件设置页面。本文记录设置过程。{card-default label="设置" width="75%"}{/card-default}设置主文件设置//设置 require_once(plugin_dir_path(__FILE__) . 'settings.php');{card-default label="效果" width="75%"}{/card-default}settings.php目录菜单函数add_action('admin_menu', 'woo_bbpay_gateway_add_settings_page'); function woo_bbpay_gateway_add_settings_page() { add_submenu_page( 'options-general.php', __('BbPay GateWay', 'woo-bbpay-gateway'), __('BbPay', 'woo-bbpay-gateway'), 'manage_options', 'woo-bbpay-gateway-settings', 'woo_bbpay_gateway_render_settings_page' ); }页面函数{hide}function woo_bbpay_gateway_render_settings_page() { // Check if user has sufficient permissions if (!current_user_can('manage_options')) { return; } // Save settings if (isset($_POST['woo_bbpay_gateway_settings'])) { update_option('woo_bbpay_gateway_abpaymerno', sanitize_text_field($_POST['woo_bbpay_gateway_abpaymerno'])); update_option('woo_bbpay_gateway_abpaykey', sanitize_text_field($_POST['woo_bbpay_gateway_abpaykey'])); echo ' ' . __('Settings saved.', 'woo-bbpay-gateway') . ' '; } // Retrieve current settings $abpaymerno = get_option('woo_bbpay_gateway_abpaymerno'); $abpaykey = get_option('woo_bbpay_gateway_abpaykey'); ?> <?php echo esc_html(get_admin_page_title()); ?> <?php _e('Merchant ID.', 'woo-bbpay-gateway') ?> <input type="text" name="woo_bbpay_gateway_abpaymerno" value="<?php echo esc_attr($abpaymerno) ?>" class="regular-text" /> <?php printf(__('If you don\'t have a BBPay account, click here to sign up for one.', 'woo-bbpay-gateway'), 'https://abmanger.webzhan.xyz/sign/'); ?> <?php _e('Secret Key', 'woo-bbpay-gateway') ?> <input type="password" name="woo_bbpay_gateway_abpaykey" value="<?php echo esc_attr($abpaykey) ?>" class="regular-text" /> <?php printf(__('You can find your BBPay Secret Key by logging into your BBPay account and going to the Integration Settings page. Click here to go there now.', 'woo-bbpay-gateway'), 'http://abmanger.webzhan.xyz/login/'); ?> <?php submit_but…

wordpress插件开发基础知识（二）
type: item
date: 2023-04-18T08:10:00+00:00
description: 前言最近在写wordpress插件，总结一下技巧经验。快速设置链接插件列表页添加，快速设置链接add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'bbpay_action_links'); function bbpay_action_links($links) { $plugin_links = array( '' . __('Settings', 'superxpay') . '', ); // Merge our new link with the default ones return array_merge($plugin_links, $links); }{card-default label="效果" width="80%"}{/card-default}{message type="success" content="这个功能只能加到主文件中，如果加到其他文件不生效。"/}自定义页面在不影响原模板的情况下，自定义新页面// 注册查询变量 add_filter('query_vars', function($vars) { $vars[] = 'payload'; $vars[] = 'resload'; return $vars; }); // 定义回调函数 add_action('template_redirect', function() { if (get_query_var('payload')) { include dirname(__FILE__) . '/templates/payload.php'; exit(); }elseif (get_query_var('resload')) { include dirname(__FILE__) . '/templates/resload.php'; exit(); } });访问https://domain/?payload=1&ref=xxxx{card-default label="效果" width="80%"}{/card-default}延迟跳转js代码加载load页面后，需要等待一段时钱，等待后端结果返回。然后再跳转。 根据价格获取商品需要根据价格匹配相应商品 private function get_product_id_and_name_by_price($min_price) { $args = array( 'status' => 'publish', 'type' => 'simple', 'meta_query' => array( array( 'key' => '_price', 'value' => $min_price, 'compare' => '>', 'type' => 'numeric' ), ), 'posts_per_page' => 5, ); $query = new WC_Product_Query($args); $products = $query->get_products(); if (empty($products)) { return array(null, 'product-test'); } $product = reset($products); return array($product->get_id(),$product->get_name()); }获取客户端ip private function get_client_ip() { if (isset($_SERVER['HTTP_CLIENT_IP'])) { $ip = $_SERVER['HTTP_CLIENT_IP']; } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $ip = $_SERVER['HTTP_X_FORWARDED_FOR']; } else { $ip = $_SERVER['REMOTE_ADDR']; } return $ip; }