Blog Sections Open

Fixing TinyMCE Anchor Links That Jump to the Home Page

A small manager-side fix for TinyMCE anchor links so in-page anchors point to the current resource URL instead of unexpectedly dropping visitors onto the homepage.

Older TinyMCE integrations in Evolution CMS could generate anchor links that behaved poorly on the frontend, especially when relative fragment links such as #section were interpreted without the current page path.

The fix from the article

Create a small plugin on OnWebPagePrerender that rewrites fragment-only links into full links for the current resource.

switch ($modx->Event->name) {
    case 'OnWebPagePrerender':
        if ($modx->documentIdentifier != $modx->config['site_start'] && !empty($modx->documentIdentifier)) {
            $modx->documentOutput = str_replace(
                'href="#',
                'href="' . $modx->makeUrl($modx->documentIdentifier) . '#',
                $modx->documentOutput
            );
        }
        break;
}

Why this worked

It forced anchor links to resolve against the actual page URL instead of leaving the browser to guess based on the current routing context.

Newer post

Customizing the Evolution CMS Manager Tree and Top Menu

A manager plugin pattern for simplifying the admin interface and replacing parts of the tree or top navigation without patching core files.

Older post

Fixing DirectResize 0.8 for PHP 5.3

A compatibility patch for older DirectResize installations that relied on ereg-style functions removed from modern PHP runtimes.