Quick Question about Programmatic Link to Existing Topic

I’m using frontend forms to create posts in WordPress that need to be linked to existing Discourse topics

My plan is to enter the existing topic URL into an ACF when I add or edit the WP post

Next I would use

add_action('acf/save_post', 'my_acf_save_post', 25);

Inside my_acf_save_post function I would execute

protected function link_to_discourse_topic( $post_id, $topic_url )

Seen here

Am I on the right track?

1 Like

Yes, you’re on the right track. In broad strokes, what you need to do is

  1. Retrieve the topic details from discourse (unless you have these already)
  2. Save the relevant details to the right post meta fields

It’s those post meta fields that you see in that function you linked that create the connection.

I’ve also added this to my WP Discourse dev agenda: to make it easier to do this, i.e. using a single public utility function to achieve this goal. I’ve slotted this in for June.

Also take a look at the existing utility functions in lib/plugin_utilities.php and lib/utilities.php. And the concomitant link_topic method in admin/discourse-sidebar/discourse-sidebar.php.

Let me know how you go.

2 Likes

@Angus, thanks for your response. I’m in PHP kindergarten, so if you’re planning to work on this, I’ll wait.

Also, I’m getting a repeatable error with the WP plugin. Don’t know if you want a separate post or GitHub issue, but here’s what’s happening:

When I open a WP post that is linked to a forum topic, the link does not show in the Discourse block. It looks like there is no link.

Conditions:
This occurs whether the link was created by publishing a ‘New Topic’, or by ‘Link to Existing Topic’

My WP posts are CPTs.

My Commenting is set to “Display a link to the comments”

Other than that, there’s nothing special about my installation

Confirmation:
I can confirm that links that don’t show in the Discourse block do exist by seeing them in my Admin columns, and because I can click on the frontend link and go to the forum post.

I’ll be happy to help with more information or testing.

Cheers, Richard

1 Like

Have you added your custom post type in the “Post Types to Publish” pubishing setting? See further

1 Like

Yes. I should have mentioned, everything works at first: I can publish to a new topic, or link to an existing topic and the links work both ways …except

If I later re-edit a post on WP, the Discourse block looks like nothing is linked, even though my WP frontend link still goes to the correct forum topic, and the link in the forum topic still goes to the correct WP post.

So the problem only exists in the WP Discourse block while re-editing a post.

Let me know if you need anything else.

1 Like

Thanks, a few more questions

  1. Do you see any errors in your browser web inspector console?

  2. If you do a full refresh on the page while viewing the editor, what happens?

It sounds like there’s a javascript issue. This could be caused by various things, including other plugins or a browser extension. We need to narrow it down a bit more if possible.

1 Like

Console: Node cannot be found in the current page

First the Discourse block shows Publishing Options / Pin Topic and Publish to Discourse sections. Then the Category section pops in. Does not show existing links.

I turned off every plugin except WP-Discourse, CPT and ACF then tried it with a Brave browser and Edge browser, both with no extensions and shields off. Still can’t see existing links in the Discourse block.

I’m running a test between regular posts and CPT posts. I’ll let you know the results.

1 Like

Not sure why, but my links are now showing in the Discourse block. I’ll report back if I find a pattern to this issue. Thanks.

2 Likes