Wordpress插件开发:致命错误:调用未定义的函数add_action()

Ham*_*ian 5 php wordpress woocommerce

我是插件开发的新手.

我尝试在wp-admin中创建自定义的Printable表单页面以创建Customer Postal Address.

非常相似这个插件

当管理员点击"打印地址"链接时,弹出template.php页面上有客户地址和打印地址信息

在此输入图像描述

问题是 :

我点击打印订单锚标签时出现致命错误,我无法执行任何wordpress操作template.php:

致命错误:在第4行的C:\ xampp\htdocs\wp-content\plugins\address generator\template.php中调用未定义的函数add_action()

 <?php
    /**
    * Plugin Name: Address Generator
    * Plugin URI: http://CGTV.ir
    * Description:Generate Postal Label for Parcel
    * Version: 1.0 or 
    * Author: Hamed Mayahian
    * Author URI: CGTV.ir
    * License: A "Slug" license name e.g. GPL12
    */
    // ADDING COLUMN TITLES (Here 2 columns)
    /*define( 'MY_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
    include( MY_PLUGIN_PATH . 'template.php');
    */      

      require_once(ADDRESS__PLUGIN_DIR .'template.php');




    add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column',11);
    function custom_shop_order_column($columns)
    {
       //add columns
        $columns['my-column1'] = __( '??? ????','theme_slug');
       return $columns;

    }

    // adding the data for each orders by column (example)
    add_action( 'manage_shop_order_posts_custom_column' , 'cbsp_credit_details', 10, 2 );
    function cbsp_credit_details( $column )
    {
        global $post, $woocommerce, $the_order;
        $order_id = $the_order->id;

        switch ( $column )
        {
            case 'my-column1' :
                $myVarOne = wc_get_order_item_meta( $order_id, '_the_meta_key1', true );
                echo $myVarOne;
                echo "<a target='_blank' href='".plugins_url( 'template.php' , __FILE__ )."?order=$order_id'>Print Address</a>";
                break;


        }
    }
Run Code Online (Sandbox Code Playgroud)

的template.php

<?php


add_action('init', 'my_init', 1);
function my_init(){

    global $post, $woocommerce, $the_order;

    $id = $_GET['order'];
    $order = new WC_Order($id);
    $address    = $order->get_billing_address();

    $customer_id = get_current_user_id();
if($_GET['order'] == "") {
  // no username entered
  echo "???? ???? ???";
} else {
  echo "Hello, " . $address;
}

}
?>
Run Code Online (Sandbox Code Playgroud)

use*_*704 3

template.php 文件不在Wordpress 中,那么我们就无法访问Wordpress 核心功能。将此文件包含在主插件中可以正常工作,但是当我们直接通过 url 访问此文件时,我们将无法访问 Wordpress 核心功能,因为我们没有遵循 WordPress 搁浅。订单列表表有名为 url 生成的按钮,例如http://localhost/wp-content/plugins/address%20generator/template.php?order=5147。当我们访问此错误时,会出现以下错误“致命错误:调用未定义的函数 add_action() in..”

\n\n

首先在主插件文件中注释这一行。

\n\n
 // require_once(\'template.php\');\n
Run Code Online (Sandbox Code Playgroud)\n\n

template.php 文件中的更改。

\n\n
<?php\n\nrequire(\'../../../wp-load.php\');\n\n\n    $id = $_GET[\'order\'];\n    $order = new WC_Order($id);\n    $address    = $order->get_billing_address();\n\n    $customer_id = get_current_user_id();\nif($_GET[\'order\'] == "") {\n  // no username entered\n  echo "\xd8\xa2\xd8\xaf\xd8\xb1\xd8\xb3 \xd9\xbe\xdb\x8c\xd8\xaf\xd8\xa7 \xd9\x86\xd8\xb4\xd8\xaf";\n} else {\n  echo "Hello, " . $address;\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

但这并不是 WordPress 的一个搁浅解决方案。用户@helgatheviking为此提供了最佳解决方案。

\n