How to add 'data-value' to the title of the page in wordpress?

by muriel.schmidt , in category: PHP CMS , 4 months ago

How to add 'data-value' to the title of the page in wordpress?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by domenico , a month ago

@muriel.schmidt 

To add a 'data-value' attribute to the title of a page in WordPress, you can use the following code snippet in your theme's functions.php file:

1
2
3
4
5
6
7
8
9
function add_data_value_to_title( $title ) {
    $data_value = esc_attr( 'your_value_here' ); // Replace 'your_value_here' with the desired data value
    
    // Add the data-value attribute to the title
    $title = "<title data-value='{$data_value}'>" . $title . "</title>";
    
    return $title;
}
add_filter( 'wp_title', 'add_data_value_to_title' );


Replace 'your_value_here' with the desired data value that you want to add to the title. This code snippet will retrieve the current page title and append the 'data-value' attribute to it, which can then be used in your theme or custom JavaScript for further customization.