简体   繁体   English

在 Wordpress 插件中调用未定义的函数

[英]Call for undefined function in Wordpress plugin

I'm currently writing a WordPress plugin that works with WooCommerce.我目前正在编写一个适用于 WooCommerce 的 WordPress 插件。 I query WooCommerce orders and the goal is to use the adresses of those orders to plan a delivery route using the API from a Dutch route planner service.我查询 WooCommerce 订单,目标是使用这些订单的地址,使用来自荷兰路线规划服务的 API 来规划交付路线。

It's been a while writing PHP for me, the last PHP I wrote was 7 years ago, so my PHP skills are a bit rusty.为我写 PHP 已经有一段时间了,我最后一次写 PHP 是 7 年前,所以我的 PHP 技能有点生疏。

After I query my orders, I want to call the function routexl_generate(), this function is defined in my code.查询订单后,我想调用函数routexl_generate(),这个函数是在我的代码中定义的。 However, running code this throws the following error:但是,运行代码会引发以下错误:

Fatal error: Uncaught Error: Call to undefined function routexl_generate() in /home/u48469p44993/domains/dev.geusdiervoeding.nl/public_html/wp-content/plugins/geusdiervoeding-wc-rxlplanner/geusdiervoeding-wc-rxlplanner.php:120 Stack trace: #0 /home/u48469p44993/domains/dev.geusdiervoeding.nl/public_html/wp-content/plugins/geusdiervoeding-wc-rxlplanner/geusdiervoeding-wc-rxlplanner.php(100): wc_order_query() #1 /home/u48469p44993/domains/dev.geusdiervoeding.nl/public_html/wp-includes/class-wp-hook.php(288): rxlp_menu_init('') #2 /home/u48469p44993/domains/dev.geusdiervoeding.nl/public_html/wp-includes/class-wp-hook.php(312): WP_Hook->apply_filters('', Array) #3 /home/u48469p44993/domains/dev.geusdiervoeding.nl/public_html/wp-includes/plugin.php(478): WP_Hook->do_action(Array) #4 /home/u48469p44993/domains/dev.geusdiervoeding.nl/public_html/wp-admin/admin.php(254): do_action('woocommerce_pag...') #5 {main} thrown in /home/u48469p44993/domains/dev.geusdiervoeding.nl/public_html/wp-content致命错误:未捕获的错误:调用 /home/u48469p44993/domains/dev.geusdiervoeding.nl/public_html/wp-content/plugins/geusdiervoeding-wc-rxlplanner/geusdiervoeding-wc-0rxlplanner/geusdiervoeding-wc-0r中的未定义函数 routexl_generate()堆栈跟踪:#0 /home/u48469p44993/domains/dev.geusdiervoeding.nl/public_html/wp-content/plugins/geusdiervoeding-wc-rxlplanner/geusdiervoeding-wc-rxlplanner.php(100): #wc_order_home() /u48469p44993/domains/dev.geusdiervoeding.nl/public_html/wp-includes/class-wp-hook.php(288): rxlp_menu_init('') #2 /home/u48469p44993/domains/dev.geusnier/voeding_public wp-includes/class-wp-hook.php(312): WP_Hook->apply_filters('', Array) #3 /home/u48469p44993/domains/dev.geusdiervoeding.nl/public_html/wp-includes/plugin.php( 478): WP_Hook->do_action(Array) #4 /home/u48469p44993/domains/dev.geusdiervoeding.nl/public_html/wp-admin/admin.php(254): do_action('woocommerce_pag...') #5 { main} 扔在 /home/u48469p44993/domains/dev.geusdiervoeding.nl/public_html/wp-content /plugins/geusdiervoeding-wc-rxlplanner/geusdiervoeding-wc-rxlplanner.php on line 120 /plugins/geusdiervoeding-wc-rxlplanner/geusdiervoeding-wc-rxlplanner.php 第 120 行

I don't really see why it behaves like that, since the function is defined... Help would be appreciated.我真的不明白为什么它会这样,因为函数是定义的...帮助将不胜感激。

This is (part of) my code:这是(部分)我的代码:

function wc_order_query()
{   

    if(isset($_GET['area'])) { 
        $route = $_GET[ 'area' ];
        $date = $_GET[ 'date' ];    

        $args = array(
            'status' => $route,
            'meta_key' => 'jckwds_date',
            'meta_value' => $date,      
        );
        $orders = wc_get_orders( $args );

        if (empty($orders)) {
            echo ('<ul><li>Op '.$date.' zijn er geen bezorgorders voor het gekozen routegebied.</li></ul>');
        } else {
            routexl_generate();
        }

        //RouteXL API aanroepen
        function routexl_generate(){
            //Query results to API
        }
    }

I realise this is 'only part of your code', but there are issues with what you've shown:我意识到这只是“您代码的一部分”,但是您所展示的内容存在问题:

  • wc_order_query() function isn't closed; wc_order_query()函数没有关闭;
  • routexl_generate() is inside wc_order_query() ; routexl_generate()wc_order_query()里面
  • routexl_generate() is inside an if conditional inside wc_order_query() routexl_generate()是一个内if条件内wc_order_query()

Technically there are no problems with nested functions, but for the sake of clarity it would make sense to move routexl_generate() outside of the function entirely:从技术上讲,嵌套函数没有问题,但为了清楚起见,将routexl_generate()完全移到函数之外是有意义的:

function wc_order_query() {
    if (isset($_GET['area'])) {
        $route = $_GET[ 'area' ];
        $date = $_GET[ 'date' ];

        $args = array(
            'status' => $route,
            'meta_key' => 'jckwds_date',
            'meta_value' => $date,
        );

        $orders = wc_get_orders( $args );

        if (empty($orders)) {
            echo ('<ul><li>Op '.$date.' zijn er geen bezorgorders voor het gekozen routegebied.</li></ul>');
        } else {
            routexl_generate();
        }
    }
}

//  RouteXL API aanroepen

function routexl_generate(){
    //  Query results to API
}

Tested & works.测试和工作。

First you have to declare function then call you made a mistake you are calling a function before declaration首先你必须声明函数然后调用你犯了一个错误你在声明之前调用了一个函数

//RouteXL API aanroepen
function routexl_generate(){
 //Query results to API
}

function wc_order_query() { 

    if(isset($_GET['area'])){ 
        $route = $_GET[ 'area' ];
        $date = $_GET[ 'date' ];    

        $args = array(
        'status' => $route,
        'meta_key' => 'jckwds_date',
        'meta_value' => $date,      
        );
        $orders = wc_get_orders( $args );
        if (empty($orders)) {
            echo ('<ul><li>Op '.$date.' zijn er geen bezorgorders voor het gekozen routegebied.</li></ul>');
        }else{
            routexl_generate();
        }
    }
}

If you define a function INSIDE AN IF then it is not actually defined until it is reached by an actual execution.如果您在 INSIDE AN IF 中定义了一个函数,那么在实际执行到达它之前,它实际上并没有被定义。 In other words it is not define in the normal code interpretation process as functions that are defined OUTSIDE an IF would be换句话说,它没有在正常的代码解释过程中定义为在 IF 之外定义的函数

So if you want to define functions inside IF's and I would recommend against it, you have to define them before you call them as if the interpreter were only single pass.因此,如果您想在 IF 中定义函数并且我不建议这样做,则必须在调用它们之前定义它们,就好像解释器只是单次传递一样。

Example: This works示例:这有效

function x($p = false)
{
    function xx($pp)
    {
        echo $pp;
    }

    if ($p) {
        xx($p);
    }

}

x('hello');

RESULT结果

hello你好

And this does not而这并没有

function x($p = false)
{

    if ($p) {
        xx($p);
    }
    function xx($pp)
    {
        echo $pp;
    }    
}

x('hello');

RESULT结果

Fatal error: Uncaught Error: Call to undefined function xx()致命错误:未捕获错误:调用未定义的函数 xx()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM