Wordpress child-theme functions.php Read-More excerpt link - php

I'm trying to change the "read more" link in a child theme.
From the codex, I've added the following to the functions.php file of the parent theme (twentyseventeen)
// Replaces the excerpt "Read More" text by a link
function new_excerpt_more($more) {
global $post;
return ' <a class="moretag" href="'.
get_permalink($post->ID) . '"> Read the full article...</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');
...and it does what I'd like it to do.
However, I don't want it to be wiped out if the theme is ever updated.
So I've created a child-theme.
However when I add the code to the child theme functions.php file, it does not work.
My child-theme functions.php file looks like this:
<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:
if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
function chld_thm_cfg_parent_css() {
wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css', array( ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 10 );
if ( !function_exists( 'child_theme_configurator_css' ) ):
function child_theme_configurator_css() {
wp_enqueue_style( 'chld_thm_cfg_separate', trailingslashit( get_stylesheet_directory_uri() ) . 'ctc-style.css', array( 'chld_thm_cfg_parent','twentyseventeen-style' ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'child_theme_configurator_css' );
function my_scripts_method() {
wp_enqueue_script(
'custom-script',
get_stylesheet_directory_uri() . '/custom_script.js',
array( 'jquery' )
);
}
add_action( 'wp_enqueue_scripts', 'my_scripts_method' );
// END ENQUEUE PARENT ACTION
// Replaces the excerpt "Read More" text by a link
function new_excerpt_more($more) {
global $post;
return ' <a class="moretag" href="'. get_permalink($post->ID) . '"> Read the full article...</a>';
}
add_filter('excerpt_more', 'new_excerpt_more');

I was able to get this to work with some information from this question:
Wordpress functions.php child theme [Resolved]
In my child-theme functions PHP, I checked to see if the original twentyseventeen_excerpt_more() function has executed, and then give my alternative readmore() function a higher priority. With the higher priority (15), add_filter( 'excerpt_more', 'readmore', 15 ); my function runs first, and the original read more function is ignored in the parent theme.
This is what the function looks like in my child-theme functions.php file:
if ( ! function_exists ( 'twentyseventeen_excerpt_more' ) ) {
function readmore( $link ) {
if ( is_admin() ) {
return $link;
}
$link = sprintf( '<p class="link-more">%2$s</p>',
esc_url( get_permalink( get_the_ID() ) ),
/* translators: %s: Name of current post */
sprintf( __( '[Read More...]<span class="screen-reader-text"> "%s"</span>', 'twentyseventeen' ), get_the_title( get_the_ID() ) )
);
return ' … ' . $link;
}
add_filter( 'excerpt_more', 'readmore', 15 );
}
BTW, the above could also be inserted into a plugin .php file if you would prefer.

Related

Where to insert php code into the functions.php file of my child theme

First of all, I'm not a developer. I've managed to insert some functions for my WooCommerce webshop by inserting code snippets into the functions.php file of my parent theme. I've made a child theme in case I have to update the theme. I'm afraid these functions get lost otherwise.
I've used the Child Theme Configurator plugin to create the child theme. Now I'm trying to insert the same code snippets into the functions.php file of the child theme. I however, break my site every time. Before adding the code I've removed the functions from the parent theme, otherwise an overlap might excist.
I think I'm adding these code snippets at the wrong location within the file. Below you see the code from the child theme:
<?php
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
// BEGIN ENQUEUE PARENT ACTION
// AUTO GENERATED - Do not modify or remove comment markers above or below:
if ( !function_exists( 'chld_thm_cfg_locale_css' ) ):
function chld_thm_cfg_locale_css( $uri ){
if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . '/rtl.css' ) )
$uri = get_template_directory_uri() . '/rtl.css';
return $uri;
}
endif;
add_filter( 'locale_stylesheet_uri', 'chld_thm_cfg_locale_css' );
if ( !function_exists( 'child_theme_configurator_css' ) ):
function child_theme_configurator_css() {
wp_enqueue_style( 'chld_thm_cfg_child', trailingslashit( get_stylesheet_directory_uri() ) . 'style.css', array( 'hello-elementor','hello-elementor','hello-elementor-theme-style' ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'child_theme_configurator_css', 10 );
// END ENQUEUE PARENT ACTION
This is the code I've successfully added to the parent theme. Now I want to add it to the child theme:
add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );
function woo_remove_product_tabs( $tabs ) {
unset( $tabs['description'] ); // Remove the description tab
unset( $tabs['additional_information'] ); // Remove the additional information tab
return $tabs;
}
/* melding uitschakelen: toegevoegd aan winkelwagen */
add_filter( 'wc_add_to_cart_message_html', 'ql_remove_add_to_cart_message' );
function ql_remove_add_to_cart_message( $message ){
return '';
}
Where and how do I add it?
Thanks for the help in advance! :)

Prevent clickable image in product page, disable product-image link

With Woocommerce and my Storefront child theme, I am trying to prevent the fact that when clicking on a single-product image, it opens a new page with the full size image.
I want the image to be unclickable so nothing happens if the user clicks on the image in the product page.
In my child-theme functions.php, the following code prevents zooming and opening the image in a lightbox, but I want to fully deactivate the linking.
add_action( 'after_setup_theme', 'remove_hemen_theme_support', 100 );
function remove_hemen_theme_support() {
remove_theme_support( 'wc-product-gallery-zoom' );
remove_theme_support( 'wc-product-gallery-lightbox' );
}
How can I achieve this?
Add this filter, it should remove your hyperlink
function e12_remove_product_image_link( $html, $post_id ) {
return preg_replace( "!<(a|/a).*?>!", '', $html );
}
add_filter( 'woocommerce_single_product_image_thumbnail_html', 'e12_remove_product_image_link', 10, 2 );
you can overwrite the template woocommerce/single-product/product-thumbnails.php in your theme.
it runs the:
apply_filters(
'woocommerce_single_product_image_thumbnail_html',
sprintf(
'%s',
esc_url( $props['url'] ),
esc_attr( $image_class ),
esc_attr( $props['caption'] ),
wp_get_attachment_image( $attachment_id, apply_filters( 'single_product_small_thumbnail_size', 'shop_thumbnail' ), 0, $props )
),
$attachment_id,
$post->ID,
esc_attr( $image_class )
);
How I did it with the same theme.
I created a simple plugin (but you can use functions.php) with functions I need for override the theme and inside I have this code:
add_action( 'wp', 'ts_remove_zoom_lightbox_gallery_support', 99 );
function ts_remove_zoom_lightbox_gallery_support() {
remove_theme_support( 'wc-product-gallery-zoom' );
remove_theme_support( 'wc-product-gallery-lightbox' );
}
The difference between our function is that I am using "wp" instead "after_setup_theme".
Try it and let me know if is work.

Enqueuing multiple style sheets in Wordpress child theme

I have several Wordpress sites set up and have child themes created for all of them. One of the sites is going to have multiple subsites that will all include customized branding. I have a style sheet enqueued in my child theme with the following code in my functions.php file.
if ( !function_exists( 'chld_thm_cfg_locale_css' ) ):
function chld_thm_cfg_locale_css( $uri ){
if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . '/rtl.css' ) )
$uri = get_template_directory_uri() . '/rtl.css';
return $uri;
}
endif;
add_filter( 'locale_stylesheet_uri', 'chld_thm_cfg_locale_css' );
if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
function chld_thm_cfg_parent_css() {
wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'style.css', array( 'font-awesome','swiper','perfect-scrollbar','animate' ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 10 );
This works fine and my style sheet is functioning as expected. The issue I'm running in to is as I add new subsites with customized branding, the size of this style sheet, style.css, is getting harder to manage. What I want to do is enqueue another style sheet in my child theme for the subsites. I tried to replicate the code above to enqueue the new style sheet, but it does not work. Here is what I added in my functions.php file immediately below the code above:
if ( !function_exists( 'coastal_thm_cfg_locale_css' ) ):
function coastal_thm_cfg_locale_css( $uri ){
if ( empty( $uri ) && is_rtl() && file_exists( get_template_directory() . '/rtl.css' ) )
$uri = get_template_directory_uri() . '/rtl.css';
return $uri;
}
endif;
add_filter( 'locale_stylesheet_uri', 'coastal_thm_cfg_locale_css' );
if ( !function_exists( 'coastal_thm_cfg_parent_css' ) ):
function coastal_thm_cfg_parent_css() {
wp_enqueue_style( 'coastal_thm_cfg_parent', trailingslashit( get_template_directory_uri() ) . 'coastal-style.css', array( 'font-awesome','swiper','perfect-scrollbar','animate' ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'coastal_thm_cfg_parent_css', 11 );
I thought this would work but it does not. I understand child themes but must admin the whole enqueuing process is quite confusing to me. I used a plugin, Child Theme Configurator, to enqueue the primary child theme style sheet.
I've looked through several blog posts and questions in Stackoverflow, but cannot find an answer to the specific problem.
Is enqueuing multiple style sheets like this possible?
If so, what am I doing wrong?

Use Wordpress Enqueue Array to Load Stylesheet Last

I want the modified child theme site-banner1.css stylesheet to load after the parent theme so it overwrites the parent file with the changes. I am using the enqueue array() to try and push site-banner1.css to the end of the queue by inputting the last style id (CSS from a plugin) into the array function. Here is the code from functions.php:
<?php
if ( !defined( 'ABSPATH' ) ) exit;
if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
function chld_thm_cfg_parent_css() {
wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit(
get_template_directory_uri() ) . 'style.css', array( ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 10 );
function my_theme_enqueue_styles() {
$parent_style = 'tesseract-site-banner';
wp_enqueue_style( $parent_style,
get_template_directory_uri() . '/css/site-banner.css' );
wp_enqueue_style( 'child-style',
get_stylesheet_directory_uri() . '/site-banner1.css',
array( $parent_style, 'tt-easy-google-font-styles')
); }
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles',99999 );
Currently site-banner1.css is visible in the source code but it is not the last stylesheet to load and so the changes are not visible. The parent site-banner.css is still visible in the dev tools with the styles. By adding the last CSS id into the array can I force site-banner1.css to load after it, I think code is missing from the array but not sure what? If this is incorrect what is the way to force the sheet to load at the end of the queue?
I managed to fix the problem using wp_deregister_style:
<?php
if ( !defined( 'ABSPATH' ) ) exit;
if ( !function_exists( 'chld_thm_cfg_parent_css' ) ):
function chld_thm_cfg_parent_css() {
wp_enqueue_style( 'chld_thm_cfg_parent', trailingslashit(
get_template_directory_uri() ) . 'style.css', array( ) );
}
endif;
add_action( 'wp_enqueue_scripts', 'chld_thm_cfg_parent_css', 10 );
add_action( 'wp_enqueue_scripts', 'load_alta_styles', 200 );
function load_alta_styles() {
wp_dequeue_style( 'tesseract-site-banner' );
wp_deregister_style( 'tesseract-site-banner' );
wp_enqueue_style( 'child-style', get_stylesheet_directory_uri() .
'/site-banner1.css' );
}

Override WooCommerce public function in child theme

In WooComerce file class-wc-form-handler.php
I wanted to override in my child theme just one line where the variable $redirect.
What is the best practice in this case ?
class-wc-form-handler
public function process_registration() {
if ( ! empty( $_POST['register'] ) ) {
...
// Redirect
if ( wp_get_referer() ) {
$redirect = esc_url( wp_get_referer() );
} else {
//$redirect = esc_url( get_permalink( wc_get_page_id( 'myaccount' ) ) );
$redirect = home_url() . $_SERVER["REQUEST_URI"];
}
wp_redirect( apply_filters( 'woocommerce_registration_redirect', $redirect ) );
exit;
}
You don't need to override any method. As you can see on line where wp_redirect there is filter hook. Read more about apply_filters.
You can add your own custom filter to the tag woocommerce_registration_redirect using add_filter.
Heres how you can do it:
add_filter( 'woocommerce_registration_redirect', 'my_custom_redirect' );
function my_custom_redirect()
{
return esc_url( get_permalink( wc_get_page_id( 'myaccount' ) ) );
}
Place the above code in your child theme functions.php file.

Categories