php - WordPress 插件开发 : Fatal error: Call to undefined function add_action()

标签 php wordpress woocommerce

我是插件开发的新手。

我尝试在 wp-admin 中创建自定义可打印表单页面以创建客户邮政地址。

非常相似This plugin

当管理员点击“打印地址”链接时,弹出template.php页面,其中包含客户地址和打印地址信息

enter image description here

问题是:

当我点击 print order anchor 标签时出现 fatal error ,我无法在 template.php 上运行任何 wordpress 操作:

fatal error :在第 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;


        }
    }

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;
}

}
?>

最佳答案

由于我不知道您要完成什么,我只能建议以下内容作为您启动插件的方式和显示自定义列的方式的改进。

/**
 * Plugin Name: Custom Shop Column Link
 * Plugin URI: http://stackoverflow.com/a/39280792/383847
 * Description: Link for shop column to display billing address
 * Version: 1.0.0
 * Author: helgatheviking
 * Author URI: http://kathyisawesome.com/
 * Text Domain: your-plugin
 * Domain Path: /languages
 *
 * Copyright: © 2015 Kathy Darling and Manos Psychogyiopoulos
 * License: GNU General Public License v3.0
 * License URI: http://www.gnu.org/licenses/gpl-3.0.html
 */


// add all your hooks only when woocommerce has fully loaded it's files
add_action( 'woocommerce_loaded', 'custom_address_generator_init' );
function custom_address_generator_init(){
    add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column',11);
    add_action( 'manage_shop_order_posts_custom_column', 'cbsp_credit_details',11);
}

// add your custom column
function custom_shop_order_column($columns)
{
   //add columns
    $columns['my-column1'] = __( 'چاپ آدرس', 'your-plugin');
   return $columns;

}

// adding the data for each orders by column (example)
function cbsp_credit_details( $column )
{
    global $the_order;
    $order_id = $the_order->id;

    switch ( $column )
    {
        case 'my-column1' :
            $myVarOne = get_post_meta( $order_id, '_the_meta_key1', true );
            echo $myVarOne;
            $url = add_query_arg( array( 'order_id' => $order_id, 'my-action' => 'do-something-cool', ), wp_nonce_url( admin_url(), 'my_order_nonce', 'my_nonce' ) );
            printf( '<a class="custom-class" href="%s" data-order_id="%s">%s</a>', $url, $order_id, __( 'Print Address', 'your-plugin' ) );
            break;
    }
}

编辑 2 我们将创建一个到前端的链接,以便我们可以通过 template_include 加载自定义模板。它应该具有足够的安全性以使其仅限于适当的用户。

// load a custom template when special link is clicked
add_action( 'template_include', 'my_template', 1 );
function my_template(){

    if( isset( $_GET['my-action'] ) && $_GET['my-action'] == 'do-something-cool' && isset( $_GET['order_id'] ) && current_user_can( 'edit_shop_order', $_GET['order_id'] ) && wp_verify_nonce( $_GET['my_nonce'], 'my_order_nonce' ) ){

        return untrailingslashit( plugin_dir_path( __FILE__ ) ) . '/templates/my-template.php';

    }
}

然后在您的插件文件夹中创建一个 /templates/my-plugin.php 文件:

<?php

$order_id = intval( $_GET['order_id'] );
$order = wc_get_order($order_id);

if( is_a( $order, 'WC_Order' ) ){
    $address    = $order->get_formatted_billing_address ();
    if( $address ){
        printf( '%s, %s', __( 'Hello', 'your-plugin' ), $address );
    } else {
        _e( 'No billing address', 'your-plugin' );
    }
} else {
    _e( 'Not a valid order ID', 'your-plugin' );
}

我放弃了 my_init() 函数,取而代之的是 my_template(),它现在将加载自定义模板 (/templates/my-template. php) 通过 template_include 过滤器。该模板由 WordPress 加载,具有所有 WordPress 功能供您使用。

关于php - WordPress 插件开发 : Fatal error: Call to undefined function add_action(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39279237/

相关文章:

php - 带有 codeigniter 问题的数据库缓存

javascript - 输入类型范围输出不适用于动态 ID

php - 如何将 WordPress 帖子循环分成两列?

wordpress - 是否可以在 WooCommerce API 订单端点中选择多个状态?

php - Woocommerce 中产品批量保存完成后可用哪个钩子(Hook)

php - 向非 Facebook 用户显示 Facebook 帖子

php - 为什么 codeigniter2 不以更安全的方式(例如 session )存储 csrf_hash?

php - wordpress查询所有以数字开头的帖子

php - jQuery "waypoints"不起作用

php - 在自定义 WooCommerce 报告中获取产品评论平均评级