On my WordPress page I sometimes have the issues, that WordPress puts

tags around elements, where I don't want them. For instance, I have this "invisible anchor" on many pages:

<a class="anchor" id="some-id"></a>

WordPress puts a

tag around it, which inserts a space, where I don't want one.

I know I can filter the content with something like this:

function filter_ptags($content) {
    return preg_replace('/<p(.)*(anchor)(.)*<\/p>/', '<a class="anchor"></a>', $content);

add_filter('the_content', 'filter_ptags');

However, the problem with this is, that the anchor has an individual ID of course, which I don't know. How can I solve this?

  • preg_replace( '/\<a class=["|\'|]anchor["|\'|] id=["|\'|].+["|\'|]\>(.*)?\<\/a\> ?/si', '', $content ); can filter it away, but you need to SSH into your server and find which plugin/file is triggering that by grep -Rn '<a class="anchor"' or downloading the site locally and searching with a text editor such as Sublime Text.
    – Ismail
    Commented Sep 16, 2017 at 20:58
  • That is no problem, I have all the content locally. On my pages I sometimes have custom HTML-structures and WordPress puts these unwanted <p> tags around it. Commented Sep 29, 2017 at 10:47

1 Answer 1


Seems you have something plugin, that causes the problem, because WP doesnt put hidden anchors in the content. have you double checked the post-editor textarea (html/visual) to determine when that ..anchor.. appears?

however, i dont recommend filtering, instead, save the CORRECT CONTENT in the back-end, instead of filtering the incorrect content on front-end.

use something like this:

add_action('save_post', 'my_filter_func',1);
function my_filter_func($post_id){
    if ( ! wp_is_post_revision( $post_id ) ){
       $_POST['post_content'] = str_replace('<a class="anchor"> OR WHATEVER SHOULD BE THERE,  '',  $_POST['post_content']);
  • I placed the anchors myself, they are not there by mistake. Commented Sep 29, 2017 at 10:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.