簡體   English   中英

Wordpress插件開發:致命錯誤:調用未定義函數add_action()

[英]Wordpress Plugin Dev : Fatal error: Call to undefined function add_action()

我是插件開發的新手。

我嘗試在wp-admin中創建自定義可打印表單頁面,以創建客戶郵政地址。

非常相似的插件

當管理員單擊“打印地址”鏈接時,彈出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;
}

}
?>

由於我不知道您要完成什么,因此我只能提出以下建議,作為您如何啟動插件以及如何顯示自定義列的改進。

/**
 * 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加載自定義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功能供您使用。

template.php文件不在Wordpress中,因此我們無法訪問Wordpress的核心功能。 將該文件包含在主插件中是可以正常工作的,但是當我們直接通過url訪問此文件時,由於我們沒有遵循Wordpress擱淺的操作,因此無法訪問Wordpress核心功能。 訂單列表表中有一個名為url的按鈕,類似於http://localhost/wp-content/plugins/address%20generator/template.php?order = 5147 當我們訪問此錯誤時,將出現以下錯誤“致命錯誤:在未定義函數add_action()中的調用。”

首先在主插件文件中注釋此行。

 // 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為此提供了最佳解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM