【发布时间】: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 功能都已准备就绪。 -
@helgatheviking 都不起作用
-
它是相同,因为您试图在 WordPress 之外的文件
template.php上调用 WordPress 函数,因此没有加载 WordPress。所以解决方案是停止加载template.php并尝试让它运行WordPress功能。 -
请在发布赏金之前添加更多相关信息。一个回复有很多细节,但不是很具体,因为我们不确定您要完成什么。
标签: php wordpress woocommerce