【问题标题】:Wordpress Plugin Dev : Fatal error: Call to undefined function add_action()Wordpress 插件开发:致命错误:调用未定义函数 add_action()
【发布时间】:2016-09-01 19:13:22
【问题描述】:

我是插件开发的新手。

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

非常相似This plugin

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

问题是:

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

致命错误:在第 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;
}

}
?>

【问题讨论】:

  • 请解释一下template.php文件是做什么的?目前尚不清楚您想要达到的目标。为什么不能将它与插件文件放在同一个文件中?我还建议您将整个插件加载到 woocommerce_loaded 挂钩上,这样您就可以确保所有 WooCommerce 功能都已准备就绪。
  • thisthis 的可能重复
  • @helgatheviking 都不起作用
  • 相同,因为您试图在 WordPress 之外的文件 template.php 上调用 WordPress 函数,因此没有加载 WordPress。所以解决方案是停止加载template.php并尝试让它运行WordPress功能。
  • 请在发布赏金之前添加更多相关信息。一个回复有很多细节,但不是很具体,因为我们不确定您要完成什么。

标签: php wordpress woocommerce


【解决方案1】:

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

/**
 * 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(),它现在将通过 template_include 过滤器加载自定义模板 (/templates/my-template.php)。该模板由 WordPress 加载,具有所有 WordPress 功能供您使用。

【讨论】:

  • 问题是没有添加列,工作正常,问题是我的代码的第二部分,当&lt;a target='_blank' href='".plugins_url( 'template.php' , __FILE__ )."?order=$order_id'&gt;Print Address&lt;/a&gt;被点击时
  • 其实我想知道为什么第二页上的功能"template.php" 不起作用
  • 当您点击template.php 页面时,不会加载 WP。这就是为什么您会遇到致命错误以及您的方法有问题的原因。我会用一个建议进行编辑,但由于我不知道你要做什么,这只是一个建议,而不是一个剪切粘贴的解决方案。
  • 感谢您的帮助,让我解释一下,我的插件文件夹中的template.php,所以我想创建一些这样的地址标签插件:wordpress.org/plugins/woocommerce-order-address-print
  • 我测试了它并且它确实有效,所以我不确定你做错了什么。获取原始插件并从编辑 2 添加 my_template() 函数。然后确保您的 /templates/my-template.php 在正确的位置(在您的新插件文件夹中)。尝试打开WP_DEBUG_LOG 看看是否有任何错误。
【解决方案2】:

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

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

 // require_once('template.php');

template.php 文件的变化。

<?php

require('../../../wp-load.php');


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

但这不是 Wordpress 搁浅的解决方案。用户@helgatheviking 提供了最好的解决方案。

【讨论】:

  • 不幸的是,@helgatheviking 解决方案不适用于我
猜你喜欢
  • 2014-07-11
  • 2011-04-26
  • 2016-08-27
  • 1970-01-01
  • 2015-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多