Radian Radian - 2 months ago 21
PHP Question

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

I'm new on plugin development.

I'm try to create a custom Printable form page in wp-admin to create Customer Postal Address.

very Similar This plugin

when administrator click on "print Address" link , pop-up

template.php
page with customer address and information for print address

enter image description here

The Problem is :

I get fatal Error when click on print order anchor tag and i can't run any
wordpress
action on
template.php
:

Fatal error: Call to undefined function add_action() in C:\xampp\htdocs\wp-content\plugins\address generator\template.php on line 4

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

}
?>

Answer

template.php file is out of Wordpress then we are not access Wordpress core functions. Include this file in main plugin is works properly but when we access by url directly this file then we can't access Wordpress core functions because we are not follow Wordpress stranded. Order list table have button called url generate like something is http://localhost/wp-content/plugins/address%20generator/template.php?order=5147. When we access this get following error "Fatal error: Call to undefined function add_action() in.."

First comment this line in your main plugin file.

 // require_once('template.php');

Changes in template.php file.

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

But this is not a Wordpress stranded solution. User @helgatheviking is provide best solution for this.

Comments