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.