Add additional empty attribute to existing HTML element - php

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);

Related

"the_title" changes the menu items also

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;
}

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 identify single instance of Wordpress default text widget?

I wanted to use the default WordPress text widget to the sidebar of pages where the title of the widget would dynamically change to add the title of the pages.
So I used the following code in my functions file:
function my_widget_title($title, $instance, $id_base) {
if ( is_singular() && 'text' == $id_base) {
return get_the_title($post->ID).__(' Custom Tour Inquiry');
}
else {
return $title;
}
}
add_filter ( 'widget_title' , 'my_widget_title', 10, 3);
This surely does what it is supposed to do, but it changes the titles of all other text widgets I try to use as well.
Is there any way I can pinpoint to one particular widget where this code will apply while the other text widgets will "behave normally"?
You could give the widget a specific name like "My Dynamic Text Widget". Then add detection of that title as a condition in your code like the following:
function my_widget_title($title, $instance, $id_base) {
if ( is_singular() && 'text' == $id_base && $title == 'My Dynamic Text Widget') {
return get_the_title($post->ID).__(' Custom Tour Inquiry');
}
else {
return $title;
}
}
add_filter ( 'widget_title' , 'my_widget_title', 10, 3);
Please replace your code with following one. I just added global $post to your code to get access to the specific post.
function my_widget_title($title, $instance, $id_base)
{
global $post;
if ( is_singular() && 'text' == $id_base) {
return get_the_title($post->ID).__(' Custom Tour Inquiry');
}
else
{
return $title;
}
}
add_filter ( 'widget_title' , 'my_widget_title', 10, 3);
Hope this will work for you as well.
Here is complete solution according to your situation. Kindly replace your old code with this new code and get your required result.
This will add checkbox into text widget and if you check this it will replace the title otherwise normal title will be render.
function kk_in_widget_form($t,$return,$instance){
if($t->id_base == "text"){
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '', 'float' => 'none') );
?>
<p>
<input id="<?php echo $t->get_field_id('change_title'); ?>" name="<?php echo $t->get_field_name('change_title'); ?>" type="checkbox" <?php checked(isset($instance['change_title']) ? $instance['change_title'] : 0); ?> />
<label for="<?php echo $t->get_field_id('change_title'); ?>"><?php _e('Change Title'); ?></label>
</p>
<?php
$retrun = null;
}
return array($t,$return,$instance);
}
function kk_in_widget_form_update($instance, $new_instance, $old_instance){
$instance['change_title'] = isset($new_instance['change_title']);
return $instance;
}
add_action('in_widget_form', 'kk_in_widget_form',5,3);
//Callback function for options update (priorität 5, 3 parameters)
add_filter('widget_update_callback', 'kk_in_widget_form_update',5,3);
function my_widget_title($title, $instance, $id_base) {
if ( is_singular() && 'text' == $id_base && $instance['change_title']) {
return get_the_title($post->ID).__(' Custom Tour Inquiry');
}
else {
return $title;
}
}
add_filter ( 'widget_title' , 'my_widget_title', 10, 3);
Here is backend checkbox screen

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