beans_add_attribute why & # 039 ; instead of single quotes ?


Hello, Hi, everybody,

I am having trouble with beans_add_attribute : every single quote in the attributes is output in these entities & # 039 ;. The result is that the uikit components are almost all broken.

$toggle="second"; $target = "{target:#'".$toggle."', animation:'uk-animation-slide-top'}"; beans_add_attribute('id', 'data-uk-toggle', $target);


outputs : ```

`data-uk-toggle="{target:#'secondToggle', animation:'uk-animation-slide-top'}"

How should I wrote $target ? I tried esc_attr(), addslashes(), manual slashing, no quotes at all, ... with no results ... What am I missing ? Is there a hope ?


Well, I've found smething. THE TROUBLE WITH SINGLE QUOTE in WordPress and Beans

beans_add_attribute() uses beans_add_attributes() which uses beans_esc_attributes() which uses methods esc_html(, esc_js() et esc_attr() in a filter and passes by default uikit attributes like 'data-uk-toggle' in esc_attr() BUT esc_attr() uses _wp_specialchars with the following (awful) singularity :

// Back-compat.
    if ( 'single' === $_quote_style )
        $string = str_replace( "'", ''', $string );

SO I created a function 'no_fckng_escape' in my functions.php and told to beans_esc_attributes() (yes I modified the parent theme. Have I any Choice ? No, there is no conditionnal declaration in this function of Beans, it is not overwritable the wordpress way) that attribute 'data-uk-toggle' will be escaped by my function 'no_fckng_escape'.

Voilà. But I feel very bad. Any other best solution would be welcome.


Hi Fredde Battel,

Do not modify parent theme. Use Beans filter to extend escaping methods:

add_filter( 'beans_escape_attributes_methods', 'mytheme_filter_escape_methods' );
/*
 * Filter escape methods.
 */
function mytheme_filter_escape_methods( $args ) {

 $args['data-uk-toggle'] = 'no_fckng_escape'; // escape method.

  return $args;

}

Perphaps you may try esc_js instead?

I have not tested it, have a try!


Thank you, Josi B ! Thank you twice, because it works, and because you just taught me to use correctly a filter !!!

AND : esc_js didn't work, it added \ where they weren't needed.

Write a reply

Login or register to write a reply, it's free!