add_action() but function is never called - wordpress

I've created a custom wordpress plugin in which I'm defining a custom post type and at one point I'm calling add_action to set custom column in the admin.
The 'manage_[custompostname]_posts_columns' is working great, but the 'manage_[custompostname]_posts_custom_column' do not.
add_filter('manage_bbc_cp_game_posts_columns', bbc_cp_game_table_head');
function bbc_cp_game_table_head( $defaults ) {
$columns = array(
'team_id' => 'Equipe',
'opponent_id' => 'Adversaire',
'gamedatetime' => 'Date / heure',
'score' => 'Score'
);
return $columns;
}
add_action( 'manage_bbc_cp_game_posts_custom_column', 'bbc_cp_game_table_content', 10, 2 );
function bbc_cp_game_table_content( $column, $post_id ) { die('it should work'); }
The second add_action returns true, but the function is never called.
I'd really appreciate if any of you had an idea of what could be the issue, or how to debug it.
Thanks,

Related

Wordpress wp_schedule_event not triggering at its custom interval

I am using an objected oriented design to build a plugin that will use a wp cron job to trigger a function every 10 seconds. However, I cannot get my wp cron job to trigger during this custom period (every 10 seconds).
My custom plugin class has a static activate function that successfully fires when the plugin is activated. This class is set out below.
class Datafetch {
public static function activate() {
update_option( 'rewrite_rules', '' );
require_once( DATAFETCH_PATH . 'schedule/class.datafetch-schedule.php' );
$Datafetch_Schedule = new Datafetch_Schedule();
$args = array();
add_filter( 'cron_schedules', $Datafetch_Schedule->set_seconds($args) );
if (!wp_next_scheduled('task_hook')) {
wp_schedule_event( time(), $args, array( 'Datafetch', 'task_hook' ) );
}
add_action( 'task_hook', $Datafetch_Schedule->begin_fetch_process(),10 );
}
The activate function instantiates the Datafetch_Schedule object. It then uses add_filter to call the method set_seconds, which sets the custom interval and then add_action to trigger the class's method begin_fetch_process. The Datafetch_Schedule class is set out below:
class Datafetch_Schedule{
public function __construct() {
}
public function begin_fetch_process() {
$args = [
'post_title' => 'Post every 10 seconds',
'post_content' => 'Test Post Content',
'post_status' => 'publish'
];
wp_insert_post($args);
}
function set_seconds( $schedules ) {
$schedules['ten_seconds'] = array(
'interval' => 10,
'display' => esc_html__( 'Every 10 Seconds' ), );
return $schedules;
}
}
The result is that one post with the title 'Post every 10 seconds' is posted at the time of the plugin's activation, but it does not post any more times. There is plenty of guidance on how to write a custom schedule, but not much of it centres around the use of classes. If any one can explain how I can successfully use the methods in the Datafetch class, from the main plugin class, to set the custom period and get my plugin to post every 10 seconds then I would be most grateful.

Woocommerce Rest APi get custom meta field

I'm trying to pull a products custom meta and custom taxonomy value from woocommerce using the rest api. But no matter what I try a can't seem to get it to work. Not sure what I'm missing.
Example, my products have a custom meta value "product_location". The post meta value exists and works because I can use this to pull the value into the woocommerce product page:
get_post_meta($product->get_id(), 'product_location', true );
In my functions.php file I've added:
add_action( 'rest_api_init', 'handle_location' );
function handle_location() {
register_rest_field( 'post', 'product_location', array(
'get_callback' => array( $this, 'get_the_product_location' ),
'schema' => null
));
}
function get_the_product_location( $post, $field_name, $request ) {
return get_post_meta( $post[ 'id' ], $field_name, true );
}
However, when I make a call say https://*******.com/wc-api/v2/products/302341/ the meta data isn't included. So basically how can I include the additional meta data? I plan on trying this for a custom taxonomy as well. Any help is appreciated - Thanks
Try changing your bottom function to
function get_the_product_location( $post, $field_name, $request ) {
return get_post_meta( $post[ 'id' ], 'product_location', $meta_value );
}

woocommerce BACS add custom field

I need to put custom BACS field in the Thank you page and also for the emails. My country needs 'variable symbol' for the BACS method, which would be the order number. I dont want to change core files.
This is what i do:
add_filter( 'woocommerce_bacs_account_fields', 'custom_bacs_account_field', 10, 2);
function custom_bacs_account_field( $account_fields, $order_id ) {
$account_fields['variable_symbol'] = array(
'label' => 'Variabilní symbol',
'value' => $order_id
);
return $account_fields;
}
For some reason this displays the variable symbol twice.
Thank you!
Alright so this works for me:
add_filter( 'woocommerce_bacs_account_fields', 'custom_bacs_account_field', 10, 2);
function custom_bacs_account_field( $account_fields, $order_id ) {
static $call_counter = 0;
if ( $call_counter>0 ) {
return $account_fields;
}
$account_fields['variable_number' ] = array(
'label' => 'Variabliní symbol',
'value' => $order_id
);
$call_counter++;
return $account_fields;
}

function to show purchase note in Woocommerce email deprecated

I am trying to show the purchase note beneath the product in the customer_processing_order email that Woocommerce generates.
I have added the following to my functions.php file:
function sww_add_images_woocommerce_emails( $output, $order ) {
// set a flag so we don't recursively call this filter
static $run = 0;
// if we've already run this filter, bail out
if ( $run ) {
return $output;
}
$args = array(
'show_purchase_note' => true,
);
// increment our flag so we don't run again
$run++;
// if first run, give WooComm our updated table
return $order->email_order_items_table( $args );
}
add_filter( 'woocommerce_email_order_items_table', 'sww_add_images_woocommerce_emails', 10, 2 );
This works, however it is printing an error message in the email stating the following:
"Notice: WC_Order::email_order_items_table is deprecated since version
3.0! Use wc_get_email_order_items instead. in /nas/content/staging/ishgamultisite/wp-includes/functions.php on line
3853"
if I change woocommerce_email_order_items_table to wc_get_email_order_items the function doesn't work.
I'm hoping someone can tell me how I should modify the code as I'm not sure?
Bit late but if anybody is still needing to show purchase notes, then there's an easier hook:
/**
* PURCHASE NOTE
* Edits the email order items args to show purchase notes
*/
function ag_add_wc_order_email_purchase_notes( $args ) {
$args['show_purchase_note'] = true;
return $args;
}
add_filter( 'woocommerce_email_order_items_args', 'ag_add_wc_order_email_purchase_notes', 10, 1 );
The filter woocommerce_email_order_items_args contains the arguments for what to display in the order emails.
$array — Optional. (callback) => array( 'order' => $order, 'items' => $order->get_items(), 'show_download_links' => $order->is_download_permitted() && ! $args['sent_to_admin'], 'show_sku' => $args['show_sku'], 'show_purchase_note' => $order->is_paid() && ! $args['sent_to_admin'], 'show_image' => $args['show_image'], 'image_size' => $args['image_size'], 'plain_text' => $args['plain_text'], 'sent_to_admin' => $args['sent_to_admin'], )
Source: http://hookr.io/filters/woocommerce_email_order_items_args/
Tested with WooCommerce 3.6.2 and works fine.
replace return $order->email_order_items_table( $args );
with
return wc_get_email_order_items( $order, $args );

Listing post with this way "domain.com/taxonomy/category

I am triying to modify wordpress (version 3.3) to list post with one category and with one taxonomy.
I have got a taxonomy called "location". If I do example.org/location/canada, it works. Now I want url rewrite for example.org/location/canada/category/dogs, but i cant achieve it.
I added to functions.php this code:
function eg_add_rewrite_rules() {
global $wp_rewrite;
$new_rules = array(
'location/(.+)/category/(.+)/?$' => 'index.php?location=' . $wp_rewrite->preg_index(1) . '&category_name=' . $wp_rewrite->preg_index(2)
);
$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
}
add_action( 'generate_rewrite_rules', 'eg_add_rewrite_rules' );
Also i have added this:
function eg_add_query_vars( $query_vars ) {
$new_vars = array( 'location' );
return array_merge( $new_vars, $query_vars );
}
add_filter( 'query_vars', 'eg_add_query_vars' );
Categories are taxonomy. So it depends on what taxonomy you want to use there. Tags, Categories, Custom Taxonomy.
If you want to create custom taxonomy, you can. Check out http://codex.wordpress.org/Taxonomies
Here is an example of registering custom taxonomy.
function people_init() {
// create a new taxonomy
register_taxonomy(
'people',
'post',
array(
'label' => __( 'People' ),
'rewrite' => array( 'slug' => 'person' ),
'capabilities' => array('assign_terms'=>'edit_guides', 'edit_terms'=>'publish_guides')
)
);
} add_action( 'init', 'people_init' );
You will then need to edit your permalinks. Go to settings > Permalinks. Using the custom, change the URL to reflect how you want your paths to display.
If you like, you may enter custom structures for your category and tag URLs here. For example, using topics as your category base would make your category links like http://example.org/topics/uncategorized/. If you leave these blank the defaults will be used.
Problem solved:
In functions.php (or in plugin if you want).
You must put this code to rewrite this url www.example.org/en/[mylocation]/de/[mycategory]
add_action('init', 'flush_rewrite_rules');
add_filter('category_rewrite_rules' , 'add_rules' ) ;
function flush_rules() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
function add_rules($rules)
{
/**
* Loop em.
* -------------------------------------------- */
$feed_rule = 'index.php?location=$matches[1]&category_name=$matches[2]&feed=$matches[3]';
$paged_rule = 'index.php?location=$matches[1]&category_name=$matches[2]&paged=$matches[3]';
$base_rule = 'index.php?location=$matches[1]&category_name=$matches[2]';
$rules['en/([^/]+)/de/([^/]+)/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = $feed_rule;
$rules['en/([^/]+)/de/([^/]+)/page/?([0-9]{1,})/?$'] = $paged_rule;
$rules['en/([^/]+)/de/([^/]+)/?$'] = $base_rule;
$feed_rule2 = 'index.php?location=$matches[1]&feed=$matches[2]';
$paged_rule2 = 'index.php?location=$matches[1]&paged=$matches[2]';
$base_rule2 = 'index.php?location=$matches[1]';
$rules['en/([^/]+)/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = $feed_rule2;
$rules['en/([^/]+)/page/?([0-9]{1,})/?$'] = $paged_rule2;
$rules['en/([^/]+)/?$'] = $base_rule2;
return $rules;
}

Resources