Wordpress Plugin Dev: Fatal error: function call undefined add_action ()

I am new to plugin development.

I am trying to create a custom Printable form page in wp-admin to create a client email address.

very similar This plugin

when the administrator clicks on "print address , a popup page template.php with the address and client information for the print address

enter image description here

The problem is this:

I get a fatal error when I click on the print anchor tag and I cannot run any wordpress action on template.php :

Fatal error: calling the undefined add_action () function 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 eg 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; } } ?> 
+5
source share
2 answers

The template.php file is missing in Wordpress, then we do not have access to the basic functions of Wordpress. Including this file in the main plugin works correctly, but when we access the file directly from the url, we cannot access the main functions of Wordpress, because we do not follow Wordpress. In the table of the list of orders there is a button with the name url that somehow generates http: //localhost/wp-content/plugins/address%20generator/template.php? Order = 5147 . When we access this error, we get the following error: "Fatal error: function call undefined add_action () in .."

First write this line in your main plugin file.

  // require_once('template.php'); 

Changes to the 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 multi-threaded Wordpress solution. @Helgatheviking is the best solution for this.

+1
source

Since I don’t know what you are trying to accomplish, I can only offer the following as an improvement on how you run your plugin and how you display a custom column.

 /** * 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 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; } } 

EDIT 2 . We will create a link to the interface so that we can load the custom template using template_include . It should be adequately protected so that it is limited only to the appropriate users.

 // 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'; } } 

Then a /templates/my-plugin.php file in your plugin folder:

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

I dropped the my_init() function in favor of my_template() , which will now load the user template ( /templates/my-template.php ) through the template_include filter. This template is loaded by WordPress and has all the available WordPress features for you.

+4
source

All Articles