Filtering a function in WP - wordpress

I have a child theme and I am trying to filter the following:
// add the function to the init hook
add_action( 'init', 'options_typography_get_google_fonts' );
// add a font to the $google_fonts variable
function options_typography_get_google_fonts() {
// Google Font Defaults
global $google_faces;
$google_faces = array(
'Great Vibes, cursive' => '*Great Vibes'
);
return $google_faces;
}
What is the best way to filter this so I can add more google fonts?

Your question is a bit vague, so here are some options:
(1) If the parent theme checks if the options_typography_get_google_fonts function exists (with function_exists) you can override the function by redeclaring it in your child theme.
(2) If the parent theme does not, you should be able to remove the action in your childtheme:
remove_action( 'init', 'options_typography_get_google_fonts' );
and build your own (similar but extended) function based on the function you just removed.
(3) If you're building a child theme, and would like users to be able to filter the list of fonts, you should return a filterable array:
return apply_filters( 'my_options_typography_get_google_fonts_filter', $google_faces );

Related

Remove description meta box from custom taxonomy

I'm trying to remove these areas from one of my custom taxonomies.
I've built them using the two plugins: Custom Post Types UI (to add them) and Advanced Custom Fields (to add fields to the taxonomy).
I can't see anything in the plugin settings to remove these things, but I'm not sure if I'm missing something.
I'm assuming I might need to add a function to the functions.php file. I've seen that hiding things using jQuery is a possibility, but I hear that this might show it initially on load and then hide it, so id like to learn how to doit properly.
I managed to get there with the link above and help from another website.
This function removed the two instances of the 'description' box:
function hide_description_row() {
echo "<style> .term-description-wrap { display:none; } </style>";
}
add_action( "measure_sectors_edit_form", 'hide_description_row');
add_action( "measure_sectors_add_form", 'hide_description_row');
and this one removed the column from the right hand side:
add_filter('manage_edit-measure_sectors_columns', function ( $columns ) {
if( isset( $columns['description'] ) )
unset( $columns['description'] );
return $columns;
});
To use with other taxonomies, just replace 'measure_sectors' with your own taxonomy slug

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

Wordpress Child Theme Enqueue and Dequeue and Localize

I am trying to enqueue an edited js file from my child theme and dequeue the original one from the parent. It should be simple, however, the parent theme is calling a function where all enqueues are made. I managed to enqueue it but not to dequeue. In addition, the original enqueue is followed by wp_localize_script() function.
If I copy the whole function to my child it works, but I am looking for a cleaner and better way to achieve that.
Here is how the original code is set up (Parent Theme):
In function.php this function is called
add_action('wp_enqueue_scripts', 'wpestate_scripts');
The wpestate_scripts function is found in another file, css_js_include.php
function wpestate_scripts() {
// A bunch of files being enqueued and some variables being assigned
wp_enqueue_script('wpestate_property', trailingslashit( get_stylesheet_directory_uri() ).'js/property.js',array('jquery','wpestate_control'), '1.0', true);
wp_localize_script('wpestate_property', 'property_vars',
array(
// Variables being localized
)
);
}
I have already added wp_dequeue_script('wpestate_property') and wp_deregister_script('wpestate_property') to my child function.php. And it did not work.
Any help is appreciated.
You need to make sure the function you're calling is fired after the script is enqueued by the parent. Typically this is done by adding a high integer value to the $priority argument for add_action().
add_action( 'wp_enqueue_scripts', 'modify_wpestate_scripts', 99 );
function modify_wpestate_scripts() {
wp_dequeue_script('wpestate_property');
// Enqueue your custom script instead
wp_enqueue_script( 'custom-wpestate_property', 'custom-wpep.js', [], '1.0', true );
wp_localize_script('custom-wpestate_property', 'property_vars',
array(
// Variables being localized
)
);
}
This adds it to the same action hook (wp_enqueue_scripts) as the parent function, but the priorty is set to 99 so it runs later (the default priority is 10).

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.

remove activity feed from Wordpress admin

I am looking for a way to hide activity feed from the dashboard using a function. Does anyone know how to do this? I want to completely remove it. I want to achieve this without a plugin.
You can use remove_meta_box() like;
function remove_dashboard_widgets(){
remove_meta_box('dashboard_activity', 'dashboard', 'normal');
}
add_action('wp_dashboard_setup', 'remove_dashboard_widgets');
add above code to functions.php
Dashboard widgets and other meta boxes can also be removed by using the unset function. You might need to play around with the array keys, or use var_dump() to find the path for the widget you're looking for.
// Removes dashboard activity widget.
function remove_dashboard_activity_widget() {
global $wp_meta_boxes;
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_activity']);
}
// Triggers dashboard widgets removal.
add_action('wp_dashboard_setup', 'remove_dashboard_activity_widget');
Additionally like Hüseyin BABAL mentioned, meta boxes can also be removed like this:
function remove_dashboard_widgets(){
remove_meta_box('dashboard_activity', 'dashboard', 'normal');
}
add_action('wp_dashboard_setup', 'remove_dashboard_widgets');
You'll either have to create a plugin or add a function to your theme functions.php file.
function remove_activity_dashboard_widget() {
remove_meta_box( 'dashboard_activity', 'dashboard', 'side' );
}
// Hook into the 'wp_dashboard_setup' action to register our function
add_action('wp_dashboard_setup', 'remove_activity_dashboard_widget' );
Here is the codex page on the subject:
http://codex.wordpress.org/Dashboard_Widgets_API#Advanced:_Removing_Dashboard_Widgets

Resources