WordPress.org

Ready to get started?Download WordPress

Forums

Auto Page Links (request) (7 posts)

  1. sccr410
    Member
    Posted 7 years ago #

    I'm looking to have a plugin that does the following:

    Automatically creates links to pages from my post with exact matches for the page name within the text. Example: "Toby is a great dog". If one of my pages had the title "Toby", then a link would automatically be created for the word "Toby" to the corresponding page.

    I tried this myself, but realized I am no good whatsoever at regular expression matching. I also cannot find a way to build the page url (since you can change the URL path in the Permalink Options). I also realized that with the method I was using, it performed a query for every post to get the list of pages (ideally the list of pages should only be grabbed once and then used for every post).

  2. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    I would do it as a filter on the_content. You'd get the list of pages, and do a replace for each page name, replacing it with the link to the page. You're right about only wanting to get the pages once, but that's fairly simple. You also would only want to build the links to the pages once, since that takes time too, although it's not a database hit, admittedly.

    I have not tested this, so it may have typos and such:

    function add_page_links_to_content($content)
    {
    global $allpages;
    global $pagelinks;
    if (!isset($allpages)) {
    $allpages = get_pages();
    foreach ($page in $allpages) {
    $pagelinks[$page->ID] = get_page_link($page->ID);
    }
    }
    foreach ($page in $allpages) {
    $content = str_replace($page->post_title, '<a href="' . $pagelinks[$page->ID] . '">' . $page->post_title . '</a>', $content);
    }
    }
    add_filter('the_content','add_page_links_to_content');

  3. Chris_K
    Member
    Posted 7 years ago #

    I'm sure there are plugins that do this already if Otto's approach is daunting. ;-)

    See http://codex.wordpress.org/Plugins#Posts for Post related plugins and poke around in the various categories there.

  4. Dgold
    Member
    Posted 7 years ago #

    There's a plugin called, coincidentally "Auto Links"

    It doesn't automatically recognize your Page titles, but you can enter any term that you want it to automatically recognize and link.

    So if you don't mind typing your Page titles into the plugin options, you'll have what you seek.

  5. Samuel Wood (Otto)
    Tech Ninja
    Posted 7 years ago #

    I'm sure there are plugins that do this already if Otto's approach is daunting. ;-)

    Oh ye of little faith... :D

  6. Chris_K
    Member
    Posted 7 years ago #

    Lots of faith!

  7. daveklassix
    Member
    Posted 6 years ago #

    where is this "Auto Links" plugin refered to above? I don't see it anywhere on the site.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags