【问题标题】:Call function in includes folder that have php file into another php file - Wordpress Plugin调用函数包括将 php 文件放入另一个 php 文件的文件夹 - Wordpress 插件
【发布时间】:2020-12-17 17:40:09
【问题描述】:

这是关于 WordPress 插件的。

我看其他人的 WordPress 插件都有这样的代码:

example.php

<?php

    $display = get_option( 'display' );
    
    if(!defined('PLUGIN_DIR'))
        define('PLUGIN_DIR', dirname(__FILE__));
    
    function init() {
      load_plugin_textdomain('example', false, basename( dirname( __FILE__ ) ) . '/lang' );
    }
    add_action('plugins_loaded', 'init');
    
    include(PLUGIN_DIR . '/includes/display.php');

?>

然后有一个包含 display.php 的文件夹

有这样的代码: 显示.php

<?php

    function display(){
    
    //thecodes
    
    }
 ?>

我已经尝试过这个编码概念。从我的分析来看,如果我没记错的话,这个调用display.php中函数的代码,调用函数display(){}代码

这是我的结构:

>includes

>>functions.php

achan.php

index.php

而且,这是我的代码:

achan.php

<?php

    $test = get_option('test');
    
    define(PLUGIN_DIR . dirname(__FILE__));
    
    
    include(PLUGIN_DIR . 'includes/functions.php');
    
    function testing(){
        global $test;
        echo ($test);
    
    }
    add_action('bbp_theme_after_reply_author_details', 'testing');

?>

在包含文件夹中,有functions.php 我的代码是这样的: 函数.php

<?php

    function test(){
        echo "HELLO";
    }
 ?>

但不工作。我的代码有问题吗? Hello 未显示在 bbp_theme_after_reply_author_details。或者也许有更有效的方法来调用另一个文件夹 PHP 文件中的函数?谢谢你,网络开发者。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    您应该在代码中的某处调用 test,如下所示:

    <?php
    
        $test = get_option('test');
        
        define(PLUGIN_DIR . dirname(__FILE__));
        
        
        include(PLUGIN_DIR . 'includes/functions.php');
        
        function testing(){
            global $test;
            echo ($test);
            
            // You should call test like this.
            test();
    
        
        }
        add_action('bbp_theme_after_reply_author_details', 'testing');
    
    ?>
    

    function testing()中查看test()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-26
      • 2018-04-29
      • 1970-01-01
      • 2020-07-30
      相关资源
      最近更新 更多