Admin-side hooks don't work (WordPress) - wordpress

I want to send an email whenever a file is attached to a certain CPT, however I can't make add_attachment hook work. In fact I can't seem to make any dashboard hook (such as post_updated) work. The code below does nothing whenever a file is attached to a post or post gets updated:
add_action( 'add_attachment', 'goldorak' );
add_action( 'post_updated', 'goldorak' );
function goldorak() {
echo 'Fired!';
echo "<script>alert('Fired!');</script>";
}
Note: my attachment is a file field created with Advanced Custom Fields plugin.

I am not sure ACF fires the same actions as the normal wordpress. Here is the ACF version of your code:
add_action( 'acf/save_post', 'goldorak', 15 ); // The saving is done with priority 10, so 15 is after the save to DB, 5 before it.
function goldorak() {
die('test');
}
But in your case, the hook acf/update_value/type=file would simplify your task:
add_action('acf/update_value', 'acf_hook_update_value', 1, 3);
function acf_hook_update_value($new_value, $post_id, $field_options) {
$key = $field_options['key']; // internal key name
$name = $field_options['name']; // pretty name
$old_value = get_field($key, $this->post_id, false);
$new_value = stripslashes($new_value);
if ($new_value != $old_value) {
die('test'); // Do something ...
}
}

Related

How to set a WooCommerce email template as default for all emails

I’m looking for a way to send all WordPress emails using a custom WooCommerce template so all emails will look the same.
The path to the template would be:
woocommerce/emails/my-custom-woocommerce-template.php
Does it have to all be templatized in a single file? If not, a combination of these entry points can probably get you the standardization you're looking for:
email-header.php lets you customize the start of the email including the header image (if you need to do more than change its URL). It opens the layout tags for the rest of the email content
email-footer.php lets you customize the footer, and closes the layout tags started in the header.
email-styles.php or the woocommerce_email_styles filter let you customize the CSS (see some gotchas in my article here).
Various actions/filters are scattered throughout the emails for customizing individual parts.
You can use the below function. It is working
function myplugin_woocommerce_locate_template( $template, $template_name, $template_path ) {
global $woocommerce;
// List of all templates that should be replaced with custom template
$woo_templates = array(
'emails/admin-new-order.php',
'emails/admin-failed-order.php',
'emails/admin-cancelled-order.php',
'emails/customer-completed-order.php',
'emails/customer-new-account.php',
'emails/customer-note.php',
'emails/customer-on-hold-order.php',
'emails/customer-processing-order.php',
'emails/customer-refunded-order.php',
'emails/customer-reset-password.php',
);
//Check whether template is in replacable template array
if( in_array( $template_name, $woo_templates ) ){
// Set your custom template path
$template = your_template_path.'emails/my-custom-woocommerce-template';
}
// Return what we found
return $template;
}
add_filter( 'woocommerce_locate_template', 'myplugin_woocommerce_locate_template', 10, 3 );
add_filter( 'wp_mail', 'your_wp_mail_action' ); // $args = compact( 'to', 'subject', 'message', 'headers', 'attachments' )
function your_wp_mail_action( $args ) {
global $your_prefix_your_email_args; // the args you could use in my-custom-woocommerce-template file
$your_prefix_your_email_args = $args;
ob_clean();
get_template_part( 'woocommerce/emails/my-custom-woocommerce-template' );
$args['message'] = ob_get_clean();
// ... your logic
return $args;
}
To view and update email settings, log into your website dashboard. In the left-hand menu, click on WooCommerce → Settings.
There, you’ll find several options tabs at the top. Click Emails to view the following templates
you can custom all as you want

hijack get_template_part via plugin

I'm trying to do a plugin that will change the behavior of a theme.
In the theme file I have a get_template_part('libs/templates/user_menu');
I want to make my plugin to "force" the get_template_part return another slug file (a path to a file in plugin folder).
So far this is my code inside the plugin:
function wpse21352_template_part_cb( $slug )
{
if(slug == 'user_menu') {
return WP_PLUGIN_URL.'/'.$slug;
} else {
return $slug;
}
}
do_action( "get_template_part_user_menu", 'user_menu' );
add_action( 'wpse21352_template_part_cb', 'get_template_part_user_menu', 10, 1 );
First of all, get_template_part does not return anything. It loads a file from your theme based on the parameters you pass to it. The function does not support filtering, which means you can not actually overwrite what is outputted by get_template_part.
The only thing the action get_template_part_[slug] allows you to do is output something before the theme file is loaded. For example, using
function myplugin_before_login( $slug, $name ) {
echo 'Example';
}
add_action( 'get_template_part_login', 'myplugin_before_login', 10, 2 );
would output "Example" before the loading the theme file when calling get_template_part( 'login' );.
Actions and filters
In general, however, I believe you might misunderstand how actions and filters work. The WordPress Codex offers extensive information on their use and usage.

WooCommerce not letting me add custom column to shop_order post type

I just started writing a plugin and ran into a problem right away. I want to add a column to the order overview page in the WooCommerce admin. The straight forward filter below doesn't do anything. Replacing shop_order with post or product, however, does show the extra column on the respective overview page.
add_filter('manage_edit-shop_order_columns', 'add_sales_column');
function add_sales_column($columns) {
$columns['order_sales'] = "Sales";
return $columns;
}
Trying this on:
WC Version: 2.1.5
WP Version: 3.8.1
How to solve this?
I encountered a similar issue when adding a custom column to the WooCommerce Orders overview page from my theme's functions.php file. I was able to get a custom column to show up by increasing the filter priority above the default value of 10. Try replacing your code with the following:
add_filter('manage_edit-shop_order_columns', 'add_sales_column', 11);
function add_sales_column($columns) {
$columns['order_sales'] = "Sales";
return $columns;
}
Tested on WP 3.9.1 and WC 2.1.12.
Check out the WordPress Codex entry on add_filter for more details on the behavior of filters using the $priority parameter.
The problem is that we have to wait for WooCommerce to finish its setup. This goes two ways, first running all our hooks inside a plugins_loaded main hook, and then setting the priority of our hooks for something greater than WC's.
add_action( 'plugins_loaded', 'setup_so_22237380' );
function setup_so_22237380()
{
// Just to make clear how the filters work
$posttype = "shop_order";
// Priority 20, with 1 parameter (the 1 here is optional)
add_filter( "manage_edit-{$posttype}_columns", 'column_set_so_22237380', 20, 1 );
// Priority 20, with 2 parameters
add_action( "manage_{$posttype}_posts_custom_column", 'column_display_so_22237380', 20, 2 );
// Default priority, default parameters (zero or one)
add_filter( "manage_edit-{$posttype}_sortable_columns", 'column_sort_so_22237380' );
}
function column_set_so_22237380( $columns )
{
$columns['order_sales'] = "Sales";
return $columns;
}
function column_display_so_22237380( $column_name, $post_id )
{
if ( 'order_sales' != $column_name )
return;
$sales_information = 'Your custom get_order_sales_information($post_id)';
if ( $sales_information )
echo "<strong style='color:#f00;'> $sales_information </strong>";
}
function column_sort_so_22237380( $columns )
{
$columns['order_sales'] = 'order_sales';
return $columns;
}
Setting the column as sortable is optional (manage_edit-{$posttype}_sortable_columns) and needs an extra action hook to make it work (pre_get_posts). This may be a complex function to build and requires its own research.
It will working fine if you include all the order hook on
add_action( 'admin_init', 'setup_all_order_column_hook' );
function setup_all_order_column_hook(){
//Just to make clear how the filters work
$posttype = "shop_order";
//Priority 20, with 1 parameter (the 1 here is optional)
add_filter( "manage_edit-{$posttype}_columns", 'column_set_so_22237380', 20, 1 );
//Priority 20, with 2 parameters
add_action( "manage_{$posttype}_posts_custom_column",column_display_so_22237380', 20, 2 );
// Default priority, default parameters (zero or one)
add_filter( "manage_edit-{$posttype}_sortable_columns",'column_sort_so_22237380' );
}

How do you remove or change the functionality of the Publish button on a custom WordPress post?

I have a custom post type and need keep the post status from getting set to 'Published' when you click the Publish button. Instead, it should work like the Save Draft button. So I either need to figure out how to just remove the Publish button so the user's can only click Save Draft our preferably, update the Publish button functionality so it doesn't set the post to publish.
You can use wordpress action hooks to modify default behaviors.
http://codex.wordpress.org/Function_Reference/add_action
In your case, you want to use the 'publish_post' hook.
So you can do
function dont_publish( $post_ID )
{
if(get_post_type($post_ID) == 'your_custom_type'){
exit;
}
}
//the dont_publish function will be called after the publish button is clicked
add_action( 'publish_post', 'dont_publish' );
The way it is above, nothing will happen at all if the publish button is clicked, but you can play around with the dont_publish function to get the results you want.
#PhoenixWing156 was close but one little change so the the other post types get updated as usual.
function dont_publish( $data , $postarr ) {
if($data['post_type'] == 'custom_post_type') {
$data['post_status'] = 'draft';
}
return $data;
}
add_filter('wp_insert_post_data' , 'dont_publish' , '99', 2);
The wp_insert_post_data hook is called before information about a post is saved to the database.
http://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data
You can try:
function dont_publish( $data , $postarr )
{
if($data['post_type'] == 'custom_post_type'){
$data['post_status'] = 'draft';
return $data;
}
}
add_filter('wp_insert_post_data' , 'dont_publish' , '99', 2);
WordPress provides the remove_meta_box() function exactly for this purpose.Just add this below code:-
add_action( 'admin_menu', function () {
remove_meta_box( 'submitdiv', 'Your_custom_post_type', 'side' );
} );
You could also disable the default saving metabox and add you own.
This is not documented well in the developer docs of wordpress.
To do this you have to hook into the "add_meta_boxes"-hook and in the hooked function yo have to call remove_meta_box('submitdiv','your-cpt','side');
The code should be something like this:
function your_cpt_metaboxes(){
remove_meta_box('submitdiv','your-cpt','side');
...
}
add_action('add_meta_boxes','function your_cpt_metaboxes');
your-cpt has to be changed to the name of your cpt of course.
I was also searching for this handy snippet and found it in the plugin Awesome Support.
The original saving metabox code can be found in /wp-admin/includes/metaboxes.php .
Just search for post_submit_meta_box (in WP 5.4 on line 22).

Running a function in Wordpress when publishing a custom post type

I am trying to run the following function anytime a wordpress "Jobs" custom post is published. The code works (lines 2-7) when it is placed in my theme template, but it only runs when the post is viewed. I want the code to run when the post is published, and so I have tried adding the code in a function inside my functions.php, but nothing is happening when each custom post is published. Any suggestions?
function indeedgeo(){
$indeedgeo = get_post_meta($post>ID, indeedgeo, true);
$indeedgeos=explode(' ',$indeedgeo);
$_jr_geo_latitude = $indeedgeos[0];
$_jr_geo_longitude = $indeedgeos[1];
update_post_meta($post->ID, _jr_geo_latitude, $_jr_geo_latitude);
update_post_meta($post->ID, _jr_geo_longitude, $_jr_geo_longitude);
}
add_action('publish_Jobs', 'indeedgeo');
You should hook into one of the three actions;
do_action('edit_post', $post_id, $post);
do_action('save_post', $post_id, $post);
do_action('wp_insert_post', $post_id, $post);
that are run when a post is either saved, or has its status updated. Something like the following should do the trick.
function se_10441543_save_post($post_id, $post){
//determine post type
if(get_post_type( $post_id ) == 'your_post_type'){
//run your code
$indeedgeo = get_post_meta($post_id, indeedgeo, true);
$indeedgeos=explode(' ',$indeedgeo);
$_jr_geo_latitude = $indeedgeos[0];
$_jr_geo_longitude = $indeedgeos[1];
update_post_meta($post_id, _jr_geo_latitude, $_jr_geo_latitude);
update_post_meta($post_id, _jr_geo_longitude, $_jr_geo_longitude);
}
}
add_action('save_post', 'se_10441543_save_post', 10, 2);
http://codex.wordpress.org/Plugin_API/Action_Reference
Not exactly sure how your "publish_jobs" hook is working but right off the bat, if you are placing this function in your functions.php, you will need to give it context. Replace $post>ID with the post number (an integer). If this applies to many post you will probably want to use another method of querying post data: http://codex.wordpress.org/Class_Reference/WP_Query. Let me know if this helps.

Resources