Title: str_replace on variable for $post-&gt;post_title
Last modified: August 19, 2016

---

# str_replace on variable for $post->post_title

 *  [OthelloBloke](https://wordpress.org/support/users/othellobloke/)
 * (@othellobloke)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/str_replace-on-variable-for-post-gtpost_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](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/str_replace-on-variable-for-post-gtpost_title/#post-1311686)
 * Am I missing something? Why can’t you just use `$variable = str_replace( '"','\\"',
   $variable);`
 *  Thread Starter [OthelloBloke](https://wordpress.org/support/users/othellobloke/)
 * (@othellobloke)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/str_replace-on-variable-for-post-gtpost_title/#post-1311859)
 * It doesn’t fix it. When there’s speechmarks in the title, it screws up the html.
 * I thought it would work too.
 *  Thread Starter [OthelloBloke](https://wordpress.org/support/users/othellobloke/)
 * (@othellobloke)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/str_replace-on-variable-for-post-gtpost_title/#post-1311901)
 * ANYONE DAMNIT?
 *  Thread Starter [OthelloBloke](https://wordpress.org/support/users/othellobloke/)
 * (@othellobloke)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/str_replace-on-variable-for-post-gtpost_title/#post-1311911)
 * Someone?
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/str_replace-on-variable-for-post-gtpost_title/#post-1311912)
 * 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?
 *  Thread Starter [OthelloBloke](https://wordpress.org/support/users/othellobloke/)
 * (@othellobloke)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/str_replace-on-variable-for-post-gtpost_title/#post-1311929)
 * 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>`
 *  [Mark / t31os](https://wordpress.org/support/users/t31os_/)
 * (@t31os_)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/str_replace-on-variable-for-post-gtpost_title/#post-1311930)
 * Is using the_title filter an option?
 *     ```
       $yourvar = apply_filters( 'the_title' , $post->post_title );
       ```
   
 *  [vtxyzzy](https://wordpress.org/support/users/vtxyzzy/)
 * (@vtxyzzy)
 * [16 years, 5 months ago](https://wordpress.org/support/topic/str_replace-on-variable-for-post-gtpost_title/#post-1311931)
 * 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?
 *  [ouija](https://wordpress.org/support/users/ouija/)
 * (@ouija)
 * [15 years, 9 months ago](https://wordpress.org/support/topic/str_replace-on-variable-for-post-gtpost_title/#post-1311994)
 * Little late to post maybe, but did you try using htmlspecialchars?
 * [http://php.net/manual/en/function.htmlspecialchars.php](http://php.net/manual/en/function.htmlspecialchars.php)

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

The topic ‘str_replace on variable for $post->post_title’ is closed to new replies.

## Tags

 * [post title](https://wordpress.org/support/topic-tag/post-title/)
 * [str_replace](https://wordpress.org/support/topic-tag/str_replace/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 5 participants
 * Last reply from: [ouija](https://wordpress.org/support/users/ouija/)
 * Last activity: [15 years, 9 months ago](https://wordpress.org/support/topic/str_replace-on-variable-for-post-gtpost_title/#post-1311994)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
