# Plugin for embedding Discourse topics in wordpress pages or posts

**URL:** https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228
**Category:** Plugin
**Created:** [June 19, 2015, 9:52pm UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228 "2015-06-19T21:52:24Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Matthieu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/matthieu/32/107885_2.png) [@Matthieu](https://meta.discourse.org/u/Matthieu)
#### Post date: [June 19, 2015, 9:52pm UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228/1 "2015-06-19T21:52:24Z")

</div>

Hi,

I did a quick and dirty wordpress plugin so you can easily embed topic content in your wordpress pages using a shortcode. It is still beta AND I’m not a webdevelopper so be careful using it for the moment.

To use it, you just need to provide the url of your discourse forum in the plugin settings and then add this shortcode `[discourse topic_id='xxx']` in the editor.

It will include the first post of the topic as pure html in the wordpress page. I did it so our community can create and maintain their project presentation on the forum while it is nicely displayed in the main web site.

Here you have an example:  
[https://www.poppy-project.org/test-discourse-shortcode/](https://www.poppy-project.org/test-discourse-shortcode/)

The code is here:  
[https://github.com/matthieu-lapeyre/wp-discourse-topic-integration/](https://github.com/matthieu-lapeyre/wp-discourse-topic-integration/)

It stills miss some CSS and template but the base is here.  
Contributions and extensions are very welcome 🙂

---

<div class="post-metadata">

### Author: ![Stephen](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/stephen/32/95011_2.png) [@Stephen](https://meta.discourse.org/u/Stephen)
#### Post date: [June 19, 2015, 11:25pm UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228/2 "2015-06-19T23:25:17Z")

</div>

Very neat, this is close to something I’ve been looking for a solution for.

Would a switch, to show the entire thread, be easy to implement?

How easy would it be to reverse the post order?

---

<div class="post-metadata">

### Author: ![Matthieu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/matthieu/32/107885_2.png) [@Matthieu](https://meta.discourse.org/u/Matthieu)
#### Post date: [June 20, 2015, 7:21am UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228/3 "2015-06-20T07:21:51Z")

</div>

It is quite easy, Discourse give a very complete json file with everything you need and a data structure well thought.

I get the first post like this `$parsed_json['post_stream']['posts'][0]['cooked'];`

But if you want to display all posts, it should be something like this

```php
$all_posts = $parsed_json['post_stream']['posts'];
foreach ($all_posts as $post) {
    echo $post['cooked'];
}

```

If you want reverse order:

```php
$all_posts = $parsed_json['post_stream']['posts'];
foreach (array_reverse($all_posts) as $post) {
    echo $post['cooked'];
}

```

If you want to play with it, you just need to change this file:  
[https://github.com/matthieu-lapeyre/wp-discourse-topic-integration/blob/master/includes/discourse-topic-integration-shortcode.php](https://github.com/matthieu-lapeyre/wp-discourse-topic-integration/blob/master/includes/discourse-topic-integration-shortcode.php)

I will give a try if I find some time

---

<div class="post-metadata">

### Author: ![jetatomic](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jetatomic/32/42202_2.png) [@jetatomic](https://meta.discourse.org/u/jetatomic)
#### Post date: [June 21, 2015, 2:18am UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228/5 "2015-06-21T02:18:15Z")

</div>

Any ideas for resolving this issue: [The plugin makes the admin very slow](https://github.com/matthieu-lapeyre/wp-discourse-topic-integration/issues/2)?

---

<div class="post-metadata">

### Author: ![Matthieu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/matthieu/32/107885_2.png) [@Matthieu](https://meta.discourse.org/u/Matthieu)
#### Post date: [June 21, 2015, 7:30am UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228/6 "2015-06-21T07:30:36Z")

</div>

I have this problem with our big site but on a fresh local wordpress it is not significant.

Maybe using a profiler could help find it

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [June 21, 2015, 8:26am UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228/7 "2015-06-21T08:26:59Z")

</div>

This works very well. It would be nice to have an option on the Discourse side for including/excluding posts in a topic. This could be useful for writing documentation.

---

<div class="post-metadata">

### Author: ![Matthieu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/matthieu/32/107885_2.png) [@Matthieu](https://meta.discourse.org/u/Matthieu)
#### Post date: [June 21, 2015, 8:50am UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228/8 "2015-06-21T08:50:58Z")

</div>

Yep it is quite easy to do. I just began by my need but lot of people ask for displaying all posts too.

Yet it would be better to have a kind of template and how to do it is not clear to me. Is there some wordpress dev ?

---

<div class="post-metadata">

### Author: ![jetatomic](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jetatomic/32/42202_2.png) [@jetatomic](https://meta.discourse.org/u/jetatomic)
#### Post date: [June 21, 2015, 7:16pm UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228/9 "2015-06-21T19:16:06Z")

</div>

This is a very useful plugin and very easy to get going.

Displaying /uploads (images and linked files) is broken on my test install. The WP site has `https` and the Discourse is `http`, and the URLs are getting rewritten somewhere. I suspect this is no direct fault of the plugin (between Cloudflare and W3 Total cache I have some suspected culprits).

---

<div class="post-metadata">

### Author: ![jetatomic](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jetatomic/32/42202_2.png) [@jetatomic](https://meta.discourse.org/u/jetatomic)
#### Post date: [June 21, 2015, 8:13pm UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228/10 "2015-06-21T20:13:59Z")

</div>

First, I would love to see something like this pulled into the official WP-Discourse plugin. Some of what follows (API integration, etc.) is already part of its functionality.

I’m not a developer either, and wish I could help more. The following are some thoughts on it so far. Not trying to be critical, just thinking out loud and relaying experiences.

###Notes

- It would be extra interesting if this got a Discourse API key so WP could access private/group forums. I was thinking it would be cool to have pages a certain set of users could edit via wiki, and that would automatically populate the main site. There may be cases where discussion would be better restricted to logged-in or group users.
- Button 1 in the image has no http/https protocol on the link. Not sure if that’s from my WP settings or the plugin.
- Not sure where the theme is picking up button 2. It happens that I can and intend to turn the sharing section off on this page anyway, but it is a curiosity.
- Displaying the Discourse title and the WP title can be redundant. The ability to toggle this in the WP post edit window would be helpful. Less elegant, but hiding via CSS should more or less suffice.
- Displaying the author (in this example, `discourse`) might be a candidate for toggling as well. As with the title, CSS is probably an okay workaround.

![](https://global.discourse-cdn.com/meta/original/3X/3/1/310fd6d6075f233d6257f38ce74dadf81a9c91f8.PNG)

---

<div class="post-metadata">

### Author: ![Matthieu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/matthieu/32/107885_2.png) [@Matthieu](https://meta.discourse.org/u/Matthieu)
#### Post date: [June 21, 2015, 8:48pm UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228/11 "2015-06-21T20:48:39Z")

</div>

Where is uploaded your images ? Is it on the discourse server or on amazon ?

Normally this ugly function do the job:

```plaintext
function add_base_url_to_link($cooked, $base_url){
  $relative_image_path = 'src="/uploads';
  $absolute_image_path = 'src="'.$base_url.'/uploads';
  $new_content = str_replace($relative_image_path, $absolute_image_path, $cooked, $count);

  $relative_user_avatar_path = 'src="/user_avatar';
  $absolute_user_avatar_path = 'src="'.$base_url.'/user_avatar';
  $new_content = str_replace($relative_user_avatar_path, $absolute_user_avatar_path, $new_content, $count);

  $relative_user_path = 'href="/users';
  $absolute_user_path = 'href="'.$base_url.'/users';
  $new_content = str_replace($relative_user_path, $absolute_user_path, $new_content, $count);

  return $new_content;
}

```

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [June 23, 2015, 6:38am UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228/12 "2015-06-23T06:38:11Z")

</div>

Another way to do this is to create a wordpress custom-post-type for Discourse topics that takes the Discourse forum base\_url and topic\_id and uses them to fetch the content for the post. The benefit of this is that it allows the Discourse content to be edited in WordPress, and it allows the content to be cached. The downside is that you have to enable CORS on the forum. Also, the content on wordpress is no longer synced live with the content on the forum. That could be a good or a bad thing, depending on what you are trying to accomplish.

[GitHub - scossar/wordpress-discourse-topic: Saves a Discourse topic as a WordPress post · GitHub](https://github.com/scossar/wordpress-discourse-topic) (this works but isn’t ready for a production site)

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [June 23, 2015, 7:14am UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228/13 "2015-06-23T07:14:34Z")

</div>

I think this can be simplified quite a bit. There’s no need for a custom post type. By leaving it out, access to forum content could always be available through a meta box.

---

<div class="post-metadata">

### Author: ![Matthieu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/matthieu/32/107885_2.png) [@Matthieu](https://meta.discourse.org/u/Matthieu)
#### Post date: [June 28, 2015, 6:56am UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228/14 "2015-06-28T06:56:29Z")

</div>

## New feature: custom CSS

There is a field in the admin so you can change default CSS for the discourse integration (the class is discourse-topic-integration):

 ![](https://global.discourse-cdn.com/meta/original/3X/f/a/fa0971c9a3f8fb04734e5d390ee844a3b5520455.png) 

results of this amazing color choice:

 ![](https://global.discourse-cdn.com/meta/original/3X/0/c/0c7b547068311d732552677c06bc9de52637c043.png) 

## Looking for contributors

I’m already reaching the max of my wordpress developper skills. It would be great to have some people to work with on the development so it can become a real solution for prod.

---

<div class="post-metadata">

### Author: ![Matthieu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/matthieu/32/107885_2.png) [@Matthieu](https://meta.discourse.org/u/Matthieu)
#### Post date: [June 28, 2015, 8:23pm UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228/15 "2015-06-28T20:23:02Z")

</div>

## Release 0.3

There are now templates and you can choose which one to use using the new shortcode option `type="subject/full/reverse-full"` (requested by @steve\_pd) for example:

```plaintext
[discourse topic_id="xxx" type="reverse-full"]

```

You can download updates here:  
[https://github.com/matthieu-lapeyre/wp-discourse-topic-integration](https://github.com/matthieu-lapeyre/wp-discourse-topic-integration)

---

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [June 28, 2015, 9:00pm UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228/16 "2015-06-28T21:00:19Z")

</div>

I’d like to help if I can.

One problem I see with the ‘full’ option is that it is only downloading the first 20 (or whatever the forum chunk\_size is set at) posts in the topic. If there are more than 20 posts in the topic, should there be a ‘load more posts’ link at the bottom of the page?

---

<div class="post-metadata">

### Author: ![Matthieu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/matthieu/32/107885_2.png) [@Matthieu](https://meta.discourse.org/u/Matthieu)
#### Post date: [June 28, 2015, 10:09pm UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228/17 "2015-06-28T22:09:46Z")

</div>

Indeed … the json file only retrieves the first 20 posts BTW the reverse template does not work too.

People can contribute by raising or fixing issues:

> **[Issues · matthieu-lapeyre/wp-discourse-topic-integration](https://github.com/matthieu-lapeyre/wp-discourse-topic-integration/issues)**
>
> Building a plugin allowing the integration of Discourse topic on wordpress posts and pages. - Issues · matthieu-lapeyre/wp-discourse-topic-integration

---

<div class="post-metadata">

### Author: ![jetatomic](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jetatomic/32/42202_2.png) [@jetatomic](https://meta.discourse.org/u/jetatomic)
#### Post date: [June 29, 2015, 11:30pm UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228/18 "2015-06-29T23:30:26Z")

</div>

Version 0.3 dealt with most of my earlier hangups. Thanks, @Matthieu

The ability to give API credentials to allow the plugin to pull from private forums would be icing on the cake, but I understand that would add significant complexity.

Nice job.

---

<div class="post-metadata">

### Author: ![Matthieu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/matthieu/32/107885_2.png) [@Matthieu](https://meta.discourse.org/u/Matthieu)
#### Post date: [June 30, 2015, 7:55pm UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228/19 "2015-06-30T19:55:24Z")

</div>

> [@jetatomic](#):
>
> The ability to give API credentials to allow the plugin to pull from private forums would be icing on the cake, but I understand that would add significant complexity.

If there is a clear method on how to do it, it could be done…

---

<div class="post-metadata">

### Author: ![Matthieu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/matthieu/32/107885_2.png) [@Matthieu](https://meta.discourse.org/u/Matthieu)
#### Post date: [June 30, 2015, 9:44pm UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228/20 "2015-06-30T21:44:25Z")

</div>

## Release 4.1

- Remove author info in subject only template
- add author-info template if you want to include it

**If you want to see how it works, this release is deployed on our project website.**

Here two examples:

> **[Marionnettes électriques](https://poppy.discourse.group/t/marionnettes-electriques/1521)**
>
> Le projet Le projet “Marionnettes électriques” est une création de Thomas Peyruse comme auto-entrepreneur labellisée “Toulous’up” par la mairie de Toulouse. Le but de ce projet est de questionner et utiliser les dernières innovations en robotique...

==\> wordpress: [https://www.poppy-project.org/project/marionnettes-electriques/](https://www.poppy-project.org/project/marionnettes-electriques/)

> **[Mathematics: A Beautiful Elsewhere](https://poppy.discourse.group/t/mathematics-a-beautiful-elsewhere/1511)**
>
> The Ergo-Robot experiment is an art/science project where robotic creatures, endowed with algorithmic models of curiosity-driven learning and language formation, were hacked by moviemaker David Lynch. The artist designed a head for the robots, as...

==\> wordpress: [https://www.poppy-project.org/project/mathematics-a-beautiful-elsewhere/](https://www.poppy-project.org/project/mathematics-a-beautiful-elsewhere/)

I added custom CSS:

```plaintext
.discourse-button{
  text-align: center;
}
.discourse-topic-link {
  font-size: 20px;
  padding-bottom: 11px;
  padding-left: 24px;
  padding-right: 24px;
  padding-top: 11px;
  background-color: rgb(108, 108, 106) !important;
}

```

---

<div class="post-metadata">

### Author: ![meglio](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/meglio/32/71444_2.png) [@meglio](https://meta.discourse.org/u/meglio)
#### Post date: [July 11, 2015, 10:35am UTC](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228/21 "2015-07-11T10:35:30Z")

</div>

Sorry if this looks like cross-posting, but I think this topic is quite connected - I’d like to put a note here about a new alternative plugin that is also capable of displaying Discourse posts in WordPress:

> [@Announcing WordPress plugin to display any data from Discourse](https://meta.discourse.org/t/announcing-wordpress-plugin-to-display-any-data-from-discourse/30968):
>
> Finally, a universal WordPress plugin that fetches anything from Discourse and outputs it in your blog. Dear Discourse community! I have been using Discourse for almost 2 years, mostly combining it with WordPress. As a result, I ended up creating a WordPress plugin that lets me display any JSON data with using an easy to learn [Twig syntax](http://twig.sensiolabs.org/). Recently, I put some effort to teach the plugin fetching data from WordPress’ underlying MySQL database and use the same Twig syntax to output the data fet…

[Next page](https://meta.discourse.org/t/plugin-for-embedding-discourse-topics-in-wordpress-pages-or-posts/30228.md?page=2)
