gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:34:46 27 4
gpt4 key购买 nike

我是插件开发的新手。

我尝试在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com