0

I'm looking for a solution (guess it has to be a wordpress plugin) in order to solve a problem.

I'm publishing lots of sites with WP - some of them have internal links (already inserted via html) to pages which aren't published yet.

My goal is that these links are not "active" from the point of publishing the URL (because then they would result in a 404 since the direction site is not online yet). I'd rather like them to be somehow inactive or deactivated until the "target" of the link is published.

I tried broken link checker but it doesn't work.

Regards

  • No plugin that I know of for this, you could write javascript as described in [this answer](https://stackoverflow.com/questions/3922989/how-to-check-if-page-exists-using-javascript) to check the links and then if it returns a response in the 400s remove the href from the link. – mrben522 Feb 24 '20 at 15:19
  • But I dont need the links to disappear completely - they are supposed to appear again when the target site is hosted. – Matthias Gengenbach Feb 24 '20 at 16:03
  • If you have javascript doing it then this will be happening on the front end when the browser renders the page. Every time someone loads a page it will check for dead links and remove them, once links are live they will automagically come back. You will also need to filter the links out to check internal links only, probably also only check links inside of whatever div you have wrapping `the_content()` – mrben522 Feb 24 '20 at 16:56
  • @mrben522 Thanks - seems like a good idea to check this everytime the page renders. Could you provide a piece of code that executes this function? – Matthias Gengenbach Feb 26 '20 at 09:40

1 Answers1

0

Something like this should work. Ideally you would have some way to change the wrapping class once you know all the links should work so that this happening forever. Replace #content and www.yourdomain.com with the appropriate values. Also I'm assuming that you have jQuery loaded already since this is a wordpress site. Also if you're using ES6 then convert to using let/const and arrow functions if you want.

jQuery(function ($) {
    $(document).ready(function() {
        $('#content').find('a[href*="www.yourdomain.com"]').each(checkLinkStatus(this));
    });

    function checkLinkStatus(linkObject) {
        var link = $(linkObject).attr('href');
        $.ajax({
            type: 'HEAD',
            url: link,
            success: function () {
                // page exists
            },
            error: function () {
                $(linkObject).attr('href', '#');
            }
        });
    }
});
mrben522
  • 417
  • 6
  • 18
  • Thanks!!! I'm just struggeling with the replacement of the #content part. What exactly do I replace it with? You said its somehow related to just checking internal links right? – Matthias Gengenbach Feb 27 '20 at 09:56
  • replace that with whatever the ID is of the div that your post content is in – mrben522 Feb 27 '20 at 17:47
  • Thanks - I tried it certain times but everytime an error occurred :( Is it necessary to specify the div? Can't I just do it for the whole content on the page? If external links would be broken, it is fine if they are disabled aswell. – Matthias Gengenbach Feb 27 '20 at 18:09
  • not necessary, just slower. what's the error you're seeing? – mrben522 Feb 28 '20 at 13:56