"the_title" changes the menu items also - php

add_filter( 'the_title', 'make_custom_title', 10, 2);
function make_custom_title {
return "MyTitle";
}
I am using the theme : https://wordpress.org/themes/greentech-lite/
I want to change MyTitle(both) which is infront of the blue background

You can use simple if block here.
add_filter( 'the_title', 'make_custom_title', 10, 2);
function make_custom_title( $title, $id ) {
global $post;
if( $post->ID == $id ) {
return 'MyTitle';
}
return $title;
}

Related

Add additional empty attribute to existing HTML element

I try to include content tracking to a site with the Matomo Tagmanager. According to their documentation this should be in the form of
<a href="/random" class="tfs-highlight-button"
data-track-content
data-content-name="Random click"
data-content-piece="Random newsletter">
Click for random
</a>
I want to add this through my functions.php with the following code
add_filter( 'nav_menu_link_attributes', function ( $atts, $item, $args ) {
if ( 'tfs-highlight-button' === $item->classes[0] ) {
$atts['data-track-content'] ;
$atts['data-content-name']="Random click";
$atts['data-content-piece']="Random newsletter";
}
return $atts;
}, 10, 3 );
Unfortunately the attribute data-track-content isn't added to the HTML element. How do I add an "empty" data-attribute to the element? I already tried $atts['data-track-content']='' and $atts['data-track-content']=NULL but this didn't work.
Wordpress removes empty/falsy attributes. I would do something like this:
add_filter('nav_menu_link_attributes', function ($atts, $item, $args) {
if (in_array('tfs-highlight-button', $item->classes)) {
$atts['data-track-content'] = '1';
$atts['data-content-name'] = 'Random click';
$atts['data-content-piece'] = 'Random newsletter';
}
return $atts;
}, 10, 3);
add_filter('walker_nav_menu_start_el', function($item_output, $item) {
if (in_array('tfs-highlight-button', $item->classes)) {
$item_output = str_replace('data-track-content="1"', 'data-track-content', $item_output);
}
return $item_output;
}, 10, 2);

Customize “one time option” string generated by WooCommerce Subscribe All The Things plugin

Based on Customize “subsription option” string generated by WooCommerce Subscribe All The Things plugin answer code, this is my attempt:
function filter_wcsatt_single_product_subscription_option_description(
$option_description, $sub_price_html, $has_price_filter, $force_subscription, $product, $subscription_scheme ) {
// Class
$option_price_class = 'subscription-option';
// New description
$option_description = '<span>Subscription: </span>
<span>' . $sub_price_html . '</span>';
return $option_description;
}
add_filter( 'wcsatt_single_product_subscription_option_description', 'filter_wcsatt_single_product_subscription_option_description', 10, 6 );
Any advice?
This should suffice, basically anything you assign to the $none_string variable will be displayed
function filter_wcsatt_single_product_one_time_option_description( $none_string, $product ) {
$none_string = $product->get_price_html();
return $none_string;
}
add_filter( 'wcsatt_single_product_one_time_option_description', 'filter_wcsatt_single_product_one_time_option_description', 10, 2 );
The main price above "choose a purchase plan" is added by WooCommerce, and not by the WCSATT plugin. So to hide it, you can use:
function filter_woocommerce_available_variation( $data, $product, $variation ) {
$data['price_html'] = '';
return $data;
}
add_filter( 'woocommerce_available_variation', 'filter_woocommerce_available_variation', 10, 3 );

Add Text to Wordpress Post Title, Exclude From Menu

I am using the Avada theme for WordPress and wanted to add "..." after each WordPress title, but the code I found adds "..." after the menu text. How can I exclude the menu from being added as well?
/* Adds ... After Blog Titles */
function add_suffix_to_title($title, $id = null){
if (! is_admin()) {
return ''.$title.'...';
}
return $title;
}
add_action( 'the_title', 'add_suffix_to_title', 10, 1 );
Thank you in advance!
You need to add some more condition to exclude menu, like bellow:
function add_suffix_to_title($title, $id = null){
if (! is_admin()) {
if(is_singular(array('post','page')) || is_archive() || is_home()){
if(in_the_loop()){
return $title . '...';
}
}
}
return $title;
}
add_action( 'the_title', 'add_suffix_to_title', 10, 1 );

how to exclude menu items in the_title filter wordpress

I am having hard time excluding menu items from the_filter function I have to modify the post titles.
I want to modify the single post title as well as the posts in my sidebar, using in_the_loop() is working fine but it is also excluding my sidebar/archive post titles, breadcrumbs etc.
is there a way to detect menu items and exclude them? other then using in_the_loop()
here is my code:
function update_the_title( $title, $post_id ) {
if (not menu items) {
return $title + 'some string';
} else {
return $title;
}
}
add_filter( 'the_title', 'update_the_title');
Thanks
you have to try like this
Not tested
function update_the_title( $title, $post_id ) {
$menuLocations = get_nav_menu_locations();
$menuID = $menuLocations['primary']; // Get the *primary* menu ID
$primaryNav = wp_get_nav_menu_items($menuID);
if (array_search($post_id, array_column($primaryNav, 'ID'))) {
return $title + 'some string';
} else {
return $title;
}
}
add_filter( 'the_title', 'update_the_title', 10, 2);

Disable add to cart button for an array of products IDs in WooCommerce

In WooCommerce, I'm trying to disable add to cart button for an array of product IDs but I can't find the problem.
I am trying to use this function:
add_filter('woocommerce_is_purchasable', 'my_woocommerce_is_purchasable', 10, 2);
function my_woocommerce_is_purchasable($is_purchasable, $product) {
$id=check(); // This function return an array of IDs
foreach ($id as $id_p){
return ($product->id = $id_p ? false : $is_purchasable);
}
}
And this is my check() function code (update):
function check() {
$listproduit = get_woocommerce_product_list();
$score = get_score_user();
foreach ($listproduit as $products) {
if ($products[1] >= 5000) {
$listid = $products[0];
return $listid;
// print_r($listid);
}
}
return $listid;
}
But this doesn't work.
What am I doing wrong?
Thanks
Updated for WooCommerce 3+
Use in_array() instead like:
add_filter( 'woocommerce_variation_is_purchasable', 'filter_is_purchasable', 10, 2 );
add_filter('woocommerce_is_purchasable', 'filter_is_purchasable', 10, 2);
function filter_is_purchasable($is_purchasable, $product ) {
if( in_array( $product->get_id(), not_purchasable_ids() ) {
return false;
}
return is_purchasable;
}
Where not_purchasable_ids() is the function that returns an array of non purchasable products Ids (here simplified):
function not_purchasable_ids() {
return array( 37, 53, 128, 129 );
}
This code goes in functions.php file of your active child theme (or active theme). Tested and works.
I struggled with this but eventually found the answer. Hope it helps:
add_action('woocommerce_single_product_summary',
'wp66176371_remove_product_description_add_cart_button', 1 );
function wp66176371_remove_product_description_add_cart_button() {
global $product;
if(in_array($product->get_id(), array(414, 427))){
remove_action( 'woocommerce_single_product_summary',
'woocommerce_template_single_add_to_cart', 30 );
}
}

Categories