Override themes functions - php

In my theme there is a functions-template.php file with a lot of functions. One of them echoes the category description on the site.
function woocommerce_taxonomy_archive_description() {
if ( is_tax( array( 'product_cat', 'product_tag' ) ) && get_query_var( 'paged' ) == 0 ) {
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_woocommerce_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_image_src( $thumbnail_id, 'full' );
$description = apply_filters( 'the_content', term_description() );
if ( $image && yit_get_option( 'shop-category-image' ) == 1 ) {
echo '<div class="term-header-image"><img src="' . $image[0] . '" width="' . $image[1] . '" height="' . $image[1] . '" alt="' . $cat->name . '" /></div>';
}
if ( $description ) {
echo '<div class="term-description">' . $description . '</div>';
}
}
}
I want to echo another variable instead without messing with the files. Is there a way to "override" an existing function?
I've been fiddling a bit with mu-plugins etc but with no success.
I always get the Fatal error: Cannot redeclare woocommerce_taxonomy_archive_description() (previously declared in error when adding the same function in my custom functions file..

Yes it can be overridden from a child theme. You can override the function from your child themes functions.php file.
See more about child theme https://codex.wordpress.org/Child_Themes
WordPress loads child theme first and then the parent theme. So if you create a function with same name in your child theme, then the if condition with ! function_exists will will be false and hence there this function won't be declared.
If you want to override from a plugin then you've to declare the function in earlier execution. Try declaring it in a init hook.
add_action('init', 'theme_func_override', 5);
function theme_func_override(){
function override_func(){
//code goes here
}
}
Update
If the function isn't declared with function_exists() check within a if condition then you can't override it!

Related

Show WooCommerce parent category thumbnail when viewing a child category

I have a function that returns the product Category Thumbnail on the Archive pages for WooCommerce. This is working great.
What I would like to do, is be able to return the Parent Category Thumbnail when viewing Child Categories.
Here is the code I've currently got:
function woocommerce_category_image() {
if ( is_product_category() ){
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_term_meta( $cat->term_id, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
if ( $image ) {
echo '<img src="' . $image . '" alt="' . $cat->name . '" />';
}
}
}
Can anybody help modify the query so that it shows the parent category image.
Ideally even better still would be to show the child thumbnail if there is one, and if there isn't, then drop back to the parent one and show that.
To avoid an empty image on the top level category use the following:
function woocommerce_category_image() {
if ( is_product_category() ){
$term = get_queried_object(); // get the WP_Term Object
$term_id = $term->parent > 0 ? $term->parent : $term->term_id; // Avoid an empty image on the top level category
$image_src = wp_get_attachment_url( get_term_meta( $term_id, 'thumbnail_id', true ) ); // Get image Url
if ( ! empty($image_src) ) {
echo '<img src="' . $image_src . '" alt="' . $term->name . '" />';
}
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Update (related to your comment)
Here if the queried product category has not an image set for it, the parent product category image will be displayed instead.
function woocommerce_category_image() {
if ( is_product_category() ){
$term = get_queried_object(); // get the WP_Term Object
$image_id = get_term_meta( $term->term_id, 'thumbnail_id', true );
if( empty( $image_id ) && $term->parent > 0 ) {
$image_id = get_term_meta( $term->parent, 'thumbnail_id', true );
}
$image_src = wp_get_attachment_url( $image_id ); // Get the image Url
if ( ! empty($image_src) ) {
echo '<img src="' . $image_src . '" alt="' . $term->name . '" />';
}
}
}
Code goes in functions.php file of your active child theme (or active theme). Tested and works.
Just change $cat->term_id by $cat->parent to get the parent thumbnail id.
Final code :
function woocommerce_category_image() {
if ( is_product_category() ){
global $wp_query;
$cat = $wp_query->get_queried_object();
$thumbnail_id = get_term_meta( $cat->parent, 'thumbnail_id', true );
$image = wp_get_attachment_url( $thumbnail_id );
if ( $image ) {
echo '<img src="' . $image . '" alt="' . $cat->name . '" />';
}
}
Hope this helps

Add an image on Woocommerce payment method title

In Woocommerce, I plan on adding images before bank name plugin BACS. at now I'm already input bank name and other setting and already try inputting HTML before bank name but it does not work.
You can easily add an icon (an image) to the payment gateways in checkout page.
But in Woocommerce this icon is located after the title.
To change it before the title you should need to edit the related template checkout/payment-method.php at line 27 from this:
<?php echo $gateway->get_title(); ?> <?php echo $gateway->get_icon(); ?>
to this:
<?php echo $gateway->get_icon(); ?> <?php echo $gateway->get_title(); ?>
and save … Please see: How to Override WooCommerce Templates via a Theme …
You will need to upload the image(s) in a folder in your theme as "assets" for example.
For each gateway you can enable a custom image, or return the default one, using this custom function hooked in woocommerce_gateway_icon action hook:
add_filter( 'woocommerce_gateway_icon', 'custom_payment_gateway_icons', 10, 2 );
function custom_payment_gateway_icons( $icon, $gateway_id ){
foreach( WC()->payment_gateways->get_available_payment_gateways() as $gateway )
if( $gateway->id == $gateway_id ){
$title = $gateway->get_title();
break;
}
// The path (subfolder name(s) in the active theme)
$path = get_stylesheet_directory_uri(). '/assets';
// Setting (or not) a custom icon to the payment IDs
if($gateway_id == 'bacs')
$icon = '<img src="' . WC_HTTPS::force_https_url( "$path/bacs.png" ) . '" alt="' . esc_attr( $title ) . '" />';
elseif( $gateway_id == 'cheque' )
$icon = '<img src="' . WC_HTTPS::force_https_url( "$path/cheque.png" ) . '" alt="' . esc_attr( $title ) . '" />';
elseif( $gateway_id == 'cod' )
$icon = '<img src="' . WC_HTTPS::force_https_url( "$path/cod.png" ) . '" alt="' . esc_attr( $title ) . '" />';
elseif( $gateway_id == 'ppec_paypal' || 'paypal' )
return $icon;
return $icon;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested on WooCommerce 3 and works.
How to get the gateway ID:
Go on WC Settings > Checkout (end of the page) listed in the Gateway ID column

WooCommerce - Conditional has_term doesn't work anymore after updating

So I'm using the conditional of tho code Snippet from this example, with this thread code with success:
BUT It's no longer working after updating my WordPress Core and WooCommerce Plugin.
if ( is_product() && has_term( 'sample-category', 'product_cat' ) ){
add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_button', 10, 0 );
function add_custom_button() {
global $products;
$product_link = get_permalink( $products->id );
$sample_link = substr($product_link, 0, -1) . '-swatch-card/';
echo '<a class="button alt btn-sample" href="' . esc_url( $sample_link ) .'">' . __( "Order a Sample", "my_theme_slug" ) . '</a>';
}
}
Child Plugin still has the proper code in the function.php file.
How can I solve this issue please?
Thanks
Try this way may be, using $post global object and embedding the conditional inside your function:
add_action( 'woocommerce_after_add_to_cart_button', 'add_custom_button', 10, 0 );
function add_custom_button() {
global $post;
if ( has_term( 'collection', 'product_cat', $post->ID ) ) {
$product_link = get_permalink( $post->ID );
$sample_link = substr($product_link, 0, -1) . '-swatch-card/';
echo '<a class="button alt btn-sample" href="' . esc_url( $sample_link ) .'">' . __( "Order a Sample", "my_theme_slug" ) . '</a>';
}
};
The conditional has_term() needs sometimes it's third argument to work… In function.php it can't find the current post So in this case is better to embed it inside the function after $post or $product global object.

Add WP permalink to PHP function

The code below adds an image into my wordpress RSS feed. I am wanting to make it so that the image is automatically hyperlinked back to the corresponding post. The code is in my theme's functions.php
function wcs_post_thumbnails_in_feeds( $content ) {
global $post;
if( has_post_thumbnail( $post->ID ) ) {
$content = get_the_post_thumbnail( $post->ID ) . '<span class="text">' . $content . '</span>';
}
return $content;
}
add_filter( 'the_excerpt_rss', 'wcs_post_thumbnails_in_feeds' );
add_filter( 'the_content_feed', 'wcs_post_thumbnails_in_feeds' );
Can I change this so that the post_thumbnail is automatically wrapped with a link to the post?
How can I wrap the get_the_post_thumbnail( $post->ID ) part of the code with a link? Thanks
You can use the get_permalink function and pass it the post ID.
function wcs_post_thumbnails_in_feeds( $content ) {
global $post;
if( has_post_thumbnail( $post->ID ) ) {
$content = '' . get_the_post_thumbnail( $post->ID ) . '<span class="text">' . $content . '</span>';
}
return $content;
}

Return ACF field as a Shortcode using Wordpress Functions.php file

How do I use an Advanced Custom Field as a Shortcode. Ive used the following code in the Wordpress functions.php file but no luck.
Here is my Code:
function location_date_func( $atts ){
return "<?php the_field('location_date', 658); ?>";
}
add_shortcode( 'location_date', 'location_date_func' );
You need to register the shortcode properly, and make it return the data to display, not return a string with php code in it:
function location_date_func( $atts ){
//return string, dont echo it, so use get_field, not the_field
return get_field('location_date', 658);
}
//create function to register shortcode
function register_shortcodes(){
add_shortcode( 'location_date', 'location_date_func' );
}
// hook register function into wordpress init
add_action( 'init', 'register_shortcodes');
Or if you are using php 5.3+, you can use anonomous functions to acheive the same result:
add_action('init', function(){
add_shortcode('location_date', function(){
return get_field('location_date', 658);
});
});
Got it to work!
function location_date_func( $atts ){
return apply_filters( 'the_content', get_post_field( 'location_details', 658 ) );
}
add_shortcode( 'location_date_sc', 'location_date_func' );
If you want to return the value of an ACF field using the_field(), there is already a built in shortcode to do that.
[acf field="location_date" post_id="658"]
If you would like to reproduce it using the [location_date] shortcode, you need to use get_field() to return rather than echo the value. Syntax-wise, the only problem with your code is that you do not need the double quotes or <?php tags, since it should already be inside a PHP block. It will be functionally the same as the [acf] shortcode, but does not accept the post_id argument. This example will be hard coded to post ID 658 unless you modify it to accept an ID as part of the $atts or use the global $post;
function location_date_func( $atts ){
return get_field( 'location_date', 658 );
}
add_shortcode( 'location_date', 'location_date_func' );
add_shortcode('location_start_your_application_group', 'start_your_application_group');
function start_your_application_group() {
$start_your_application_group = '';
$start_your_application_group .= '<section class="start-your-application">';
if ( have_rows( 'start_your_application_group', 'option' ) ) :
while ( have_rows( 'start_your_application_group', 'option' ) ) : the_row();
$heading = get_sub_field( 'heading' );
$content = get_sub_field( 'content' );
if ( $heading !== '' ) {
$start_your_application_group .= '<h3 class="start-your-application__heading">' . $heading . '</h3>';
}
if ( $content !== '' ) {
$start_your_application_group .= '<div class="start-your-application__content">' . $content . '</div>';
}
$image = get_sub_field( 'image' );
if ( $image ) {
$start_your_application_group .= '<div class="start-your-application__image-container"><img class="start-your-application__image" src="' . $image['url'] .'" alt="' . $image['alt'] . '" /></div>';
}
endwhile;
endif;
$start_your_application_group .= '</section>';
return $start_your_application_group;
}

Categories