php

wordpress插件开发基础知识(二)

行云流水
2023-04-18 / 0 评论 / 193 阅读 / 正在检测是否收录...

前言

最近在写wordpress插件,总结一下技巧经验。

快速设置链接

插件列表页添加,快速设置链接
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'bbpay_action_links');

function bbpay_action_links($links)
{
    $plugin_links = array(
            '<a href="' . admin_url('admin.php?page=wc-settings&tab=checkout&section=superxpay') . '">' . __('Settings', 'superxpay') . '</a>',
        );

        // Merge our new link with the default ones
        return array_merge($plugin_links, $links);
}

自定义页面

在不影响原模板的情况下,自定义新页面
// 注册查询变量
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

延迟跳转js代码

加载load页面后,需要等待一段时钱,等待后端结果返回。然后再跳转。
<script>
function alertSet(e, delay = 5000) {
    return new Promise(function(resolve, reject) {
        document.getElementById("js-alert-box").style.display = "block";
        document.getElementById("js-alert-head").innerHTML = e;
        var t = 5,
            n = document.getElementById("js-sec-circle");
        document.getElementById("js-sec-text").innerHTML = t;

        // 启动计时器
        var timerId = setInterval(function() {
            if (t == 0){
                clearInterval(timerId);
                resolve();
            } else {
                t -= 1;
                document.getElementById("js-sec-text").innerHTML = t;
                var e = Math.round(t / 5 * 735);
                n.style.strokeDashoffset = e - 735;
            }
        }, 970);

        // 设置延迟时间,等待指定时长后跳转到目标链接
        setTimeout(function() {
            clearInterval(timerId);
            location.href="/?wc-api=wc_order_pay&ref=<?php echo $tm_ref ?>";  //#时间到后跳转地址
            reject();
        }, delay);
    });
}
// 调用函数并等待
alertSet('您将购买商品', 5000)
    .then(function() {
    })
    .catch(function() {
    });
</script>

根据价格获取商品

需要根据价格匹配相应商品
        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;
        }

评论 (0)

取消
只有登录/注册用户才可评论