Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

kerelka's avatar

How to auto embed other post link (title + excerpt) in current post body?

I do have concern about this, sometimes my client ask me if i can make it like wordpress, etc. but i still don't know how to make auto embed other post link into current post body without explicit write it when create post.

*im using TipTap editor

0 likes
4 replies
LaryAI's avatar
Level 58

Certainly! To auto-embed another post’s link with its title and excerpt into the current post body (like WordPress embeds), you’ll want to:

  1. Detect a pattern in the body, e.g. if a post URL is pasted or referenced (/posts/123).
  2. When displaying the post (not when writing/editing), parse the content server-side or client-side.
  3. Replace detected post links with an HTML snippet containing the title and excerpt of the referenced post.

Assumption: You're using Laravel with a Post model, and rendering post body as HTML.

1. Detect and Replace URLs on the Server

Create a helper to process the post body:

use App\Models\Post;

function parsePostEmbeds($body)
{
    // Pattern for your post URLs (adjust as needed)
    $pattern = '/\/posts\/(\d+)/';

    return preg_replace_callback($pattern, function ($matches) {
        $postId = $matches[1];
        $post = Post::find($postId);

        if (!$post) return $matches[0]; // Leave link as-is if post not found

        // Generate your embed HTML
        $embed = view('partials.post-embed', ['post' => $post])->render();

        return $embed;
    }, $body);
}

Example Blade partial (resources/views/partials/post-embed.blade.php):

<div class="post-embed">
    <a href="{{ route('posts.show', $post) }}">
        <strong>{{ $post->title }}</strong>
    </a>
    <p>{{ Str::limit(strip_tags($post->body), 100) }}</p>
</div>

2. Use the Helper When Rendering

In your show route/controller:

// In your controller
$parsedBody = parsePostEmbeds($post->body);

return view('posts.show', ['post' => $post, 'parsedBody' => $parsedBody]);

In your blade:

{!! $parsedBody !!}

3. Optional: TipTap Custom Extension

If you want to auto-convert URLs while editing in TipTap, you would write a custom TipTap extension/plugin in JavaScript. But for most Laravel setups, server-side parsing as above works well and is robust (and protects against broken/missing posts).


Summary:

  • Clients just paste the post URL.
  • When rendering, scan for /posts/{id} links and replace with embed box.
  • Keeps the writing flow simple, and presents consistent embeds.

Let me know if you need a TipTap extension example for the editor itself!

vincent15000's avatar

What do you mean when you say auto embed ? Can you give a concrete example ?

kerelka's avatar

i mean, in my content. i don't need to explicit embed ads or other link content on text editor.

Please or to participate in this conversation.