Support » Fixing WordPress » str_replace on variable for $post->post_title

  • I currently have ‘front-end’ delete links on my blog for logged in Administrators with a double javascript check popup.

    In the popup I’ve got the post title coming up. It works fine if I have an apostrophe in the title, but not if I have speechmarks / quotation marks

    I’m currently using the following:

    $variable = $post->post_title;
    $variable = str_replace(array("'",""), "\'", $variable);

    Adding the following to cater for speechmarks does not work:

    $variable = str_replace(array('"',''), '\\"', $variable);

    Can anyone help me please?

Viewing 9 replies - 1 through 9 (of 9 total)
  • esmi


    Forum Moderator

    Am I missing something? Why can’t you just use $variable = str_replace( '"', '\\"' ,$variable);

    It doesn’t fix it. When there’s speechmarks in the title, it screws up the html.

    I thought it would work too.



    Can you give me an example of what you want to happen? esmi’s suggestion will replace quotation marks with slash-quotation marks.

    Start string: mac’s “title”
    Result string: mac\’s \”title\”

    Is that not what you want?

    That doesn’t work. I currently have this:

    $variable = $post->post_title;
    $variable = str_replace( "'", "\'", $variable);
    $variable = str_replace( '"', '\\"', $variable);
    	$link = "<a onclick=\"return checkStuff('$variable');\" href='" . wp_nonce_url( get_bloginfo('url') . "/wp-admin/post.php?action=delete&post=" . $post->ID, 'delete-post_' . $post->ID) . "'>".$link."</a>";

    I added the following title – Title 1 ” Title 2 “

    and the html for the delete link it gave me was this:

    <a onclick="return checkStuff('Test 1 \" test="" 2="" \="" );="" href="URL HERE">Delete</a>

    but when I add the following title: Title 1 ‘ Title 2’ it gives me the following correctly commented html:

    <a onclick="return checkStuff('Title 1 \' Title 2 \' ')" href="URL HERE">Delete</a>

    Is using the_title filter an option?

    $yourvar = apply_filters( 'the_title' , $post->post_title );

    I can’t explain it unless $post->post_title doesn’t contain what you think it does. Maybe it is getting changed somehow before you retrieve it. The str_replace suggested by esmi works as advertised for me. Have you dumped out $variable before the replaces to be sure what it contains?

    Little late to post maybe, but did you try using htmlspecialchars?

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘str_replace on variable for $post->post_title’ is closed to new replies.