eliminate 3 backslashes with stripslashes

829 Views Asked by At

Out of some reasons after updating several things, on our wordpress site in the custom fields are automatically addes 3 backslashes before every apostrophe. Example: src="abc" will result in src=\\\"abc\\\"

I've got a function in functions.php where I hook into the website. Now I need to remove those backslashes. This is the orignal function:

add_action('woocommerce_before_single_product', 'headline_placeholder');
function headline_placeholder () {
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'productheadline', true);
wp_reset_query();
}

This is what I tried to remove the backslashes, but its only removing 2 backslashes, not all 3.

function removeslashes($string)
{
    $string=implode("",explode("\\",$string));
    return stripslashes(trim($string));
}

add_action('woocommerce_before_single_product', 'headline_placeholder');
function headline_placeholder () {
global $wp_query;
$postid = $wp_query->post->ID;
$meta = get_post_meta($postid, 'productheadline', true);
echo removeslashes($meta);
wp_reset_query();
}

Where is the mistake?

2

There are 2 best solutions below

1
darode On

Maybe you can use str_replace, something like this:

function removesalshes($text) {
    return str_replace ( "///" , "", $text);
}

Hope it helps!

7
Nadir Latif On

It seems you are removing forward slash. Try the following:

function removesalshes($text) {
    return str_replace ( '\\\' , "", $text);
}