WordPress removes url queries on links for the_content()

  • Unknown's avatar

    So I have this code

    function add_cid_to_content_links($content) {

        $cid = getCid();

        // Create a DOMDocument instance

        $dom = new DOMDocument();

        libxml_use_internal_errors(true); // Disable warnings for HTML5 tags

        $dom->loadHTML(mb_convert_encoding($content, ‘HTML-ENTITIES’, ‘UTF-8’));

        // Find all anchor elements in the content

        $anchors = $dom->getElementsByTagName(‘a’);

        foreach ($anchors as $anchor) {

            $href = $anchor->getAttribute(‘href’);

            // Check if the href attribute is not empty

            if (!empty($href)) {

                // Check if the href already contains a query string

                if (strpos($href, ‘?’) !== false) {

                    $anchor->setAttribute(‘href’, $href . ‘&cid=’ . $cid);

                } else {

                    $anchor->setAttribute(‘href’, $href . ‘?cid=’ . $cid);

                }

            }

        }

        // Save the modified content

        $modified_content = $dom->saveHTML();

        return $modified_content;

    }

    add_filter(‘the_content’, ‘add_cid_to_content_links’);

    this adds a cid on every link on the_content().

    but after 2-5 seconds of loading the article the url parameters of links gets removed.

    is this a security feature from wordpress?
    if so, is there a way we can get around through this?

    i have also tried it in javascript but got the same result.

    Cheers.

    The blog I need help with is: (visible only to logged in users)

  • Welcome to the WordPress.com Education Community Forum @earljohnsabalo

    Through this forum, we help students who are working through our courses and webinars found at https://wordpress.com/learn

    It also looks like you are on a free plan with WordPress.com and it’s not possible to add code to a site with a free plan. It’s certainly possible to add extra code to the Creator plan but not to free plan sites. Here is a link where you can add a Creator plan to your account.

    If your site isn’t hosted at WordPress.com then it would be worth reaching out to your host to see what support they can offer on this.

  • Unknown's avatar

    Sorry, we already have a website but i cannot disclose it.

  • Sorry, we already have a website but i cannot disclose it.

    Understood! Since these forums are aimed at websites that are specifically hosted here on WordPress.com, we may not be the best resource for help if your WordPress website is hosted with a different company.

    I can definitely suggest checking in with the community forums of WordPress.org, which is dedicated to all things relating to the WordPress software in general:
    https://wordpress.org/support/forums/

  • The topic ‘WordPress removes url queries on links for the_content()’ is closed to new replies.