Jump to content

Remove child links from a parent page


Dave Nichols
 Share

Recommended Posts

@Dave NicholsI too am not clear, but if you want to remove anchor tags from a body I would do something like this:

 

in ready.php

wire()->addHookBefore('Pages::saveReady', function(HookEvent $event) {
    $page = $event->arguments(0);

    if ($page->name == 'parent-name') {

        $body = $page->body;

        // Use regex to remove anchor tags but keep the inner text
        $bodyWithoutAnchors = preg_replace_callback(
            '/<a[^>]*>(.*?)<\/a>/is', // Match <a> tags with content
            function ($matches) {
                return $matches[1]; // Return the inner text
            },
            $body
        );

        // Update the page body
        if ($body !== $bodyWithoutAnchors) {
            $page->body = $bodyWithoutAnchors;
        }
    }
});

 

Backup your database & test with care as the above pattern will unlink ALL "<a>" tags. you may want to adjust the pattern to the children accordingly.

Link to comment
Share on other sites

  • 2 weeks later...
On 12/28/2024 at 12:10 PM, poljpocket said:

Hi Dave, can you elaborate a bit on what your goal is?

I assume, you have the links to the child pages somewhere on the website (the part your visitors see), right?

What do you mean by "unlink them"?

I'm referring to the settings tab in the page edit form, in there is the 'Parent' field. When a new page is created this is blank (according to its template), but once populated you cannot make it blank again.
I wanted to make it blank so that it no longer appeared as child in the parent page. I didn't want it as a child to anything as I had tweaked my system so that this parent found its children programmatically.

My eventual workaround was to create a dummy hidden parent and link the relevant children to that.

Setting the template to not allow children did not work retrospectively, so that was not a solution.

My context was this:
I had a site with a parent page (a race list) with lots of children (individual races), but each time a child page was added it had to be manually linked to its parent. Lots of different people where adding child pages and often 'forgot' to add the link. The child pages were all of a particular template type so I tweaked the parent page template code to find them dynamically. This resulted in duplication of the existing child links. So I wanted to clear them and found I could not. This is a link to the parent page - https://merciafr.club/races/ - the 'All Races' list is what is now created dynamically.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...