WP link back to Discourse not showing when using custom code

Hi there,

I have an issue where on the WP side there is no link available to the Discourse forum. Just some answers on forum topics i found about this:

  • All ’ Text Content’ fields are filled in in the WP admin → Plugin section.
  • The sync runs; i can see the comments on both sides.
  • The URL string is available in the _postmeta db table

The comments section shows me Notices:

**Notice**: Trying to access array offset on value of type null in **/home/u48255p44743/domains/fxweb.snowfreakz.nl/public_html/wp-content/plugins/wp-discourse/lib/discourse-comment-formatter.php** on line **90**
**Notice**: Trying to access array offset on value of type null in **/home/u48255p44743/domains/fxweb.snowfreakz.nl/public_html/wp-content/plugins/wp-discourse/lib/discourse-comment-formatter.php** on line **106**
**Notice**: Trying to access array offset on value of type null in **/home/u48255p44743/domains/fxweb.snowfreakz.nl/public_html/wp-content/plugins/wp-discourse/lib/discourse-comment-formatter.php** on line **127**
**Notice**: Trying to access array offset on value of type null in **/home/u48255p44743/domains/fxweb.snowfreakz.nl/public_html/wp-content/plugins/wp-discourse/lib/discourse-comment-formatter.php** on line **127**
**Notice**: Trying to access array offset on value of type null in **/home/u48255p44743/domains/fxweb.snowfreakz.nl/public_html/wp-content/plugins/wp-discourse/lib/discourse-comment-formatter.php** on line **148**
**Notice**: Trying to access array offset on value of type null in **/home/u48255p44743/domains/fxweb.snowfreakz.nl/public_html/wp-content/plugins/wp-discourse/lib/discourse-comment-formatter.php** on line **148**

Which are all related to the $URL in the file. I am calling the comments with:

<?php
global $post;
use WPDiscourse\Utilities\Utilities as DiscourseUtilities;
$discourse_comments = DiscourseUtilities::get_discourse_comments($post->ID);
echo $discourse_comments;
?>

Example URLs:

<? ` echo get_post_meta( $post->ID, 'discourse_permalink', true ); ` ?>

Placing the above direct call gives me no content back as link (found it in another topic)

Running Apache / PHP7.4 / WP 6.0.2
After some hours troubleshooting i am out of options. Maybe someone has a clue !?
Thanks in advance,

Joep

Hey Joep,

If both of these things are true, then there is some other issue (non WP Discourse) with your site.

Has this just started recently? What else has changed on your WP installation recently? Other plugins, themes, caching etc.

Hi Angus,

Thnx for reaching out … it’s a “fresh” Wordpress installation running Bricks template builder and just installed WPdiscourse for the connection. Besides mod_headers server caching there are no other aggressive plugins altering any scripts.

Just added the direct PHP line again and … magically the URL is there. No idea what i did different.

<?php
global $post;
use WPDiscourse\Utilities\Utilities as DiscourseUtilities;
$discourse_comments = DiscourseUtilities::get_discourse_comments($post->ID);
echo $discourse_comments;

echo get_post_meta( $post->ID, 'discourse_permalink', true );
?>

But i still have the Notices which relate to code lines where the URL should be constructed and the link after " Continue the discussion at" is not showing.

Joep

Ok. Why are you loading the comments and permalink using your own custom code? The plugin handles this for you.

Hi,

When I include the “main” wordpress comments (which is a bricks template element) it shows the “normal WP comment form” which is not replaced by the Discourse reviews plugin. See the above URL; i changed the lay-out adding the WP comments below the Blog.

Backend settings

Therefore i added the code manually as that showed the reviews and probably overrides any Bricks builder incompatibilities.

Joep

1 Like

Ok. There’s probably some kind of load priority issue with the template builder you’re using here. I’ll try and recreate it tomorrow. Sit tight.

Can i sent you the template builder for refference?

There will be various updates to the get_discourse_comments utility function in the next release (2.4.6), which is currently being prepared for release. The updated utility method signature will look like this

/**
* Get the Discourse comment HTML so that it can be displayed without loading the comments template.
*
* @param int $post_id The post ID to display the comments for.
* @param bool $perform_sync Determines whether a comment sync is maybe performed when loading comments.
* @param bool $force_sync Determines whether comment sync cache is bypassed when loading comments.
* @param string $comment_type Type of comment display.
*
* @return string
*/
public static function get_discourse_comments( $post_id, $perform_sync = true, $force_sync = false, $comment_type ) {

@Joep_Kannegieter This will have a few upshots for your case (and a few other cases):

  • The comment display will work
  • You’ll control the comment display type (i.e. all comments or comment link) programmatically
  • You’ll control the comment cache programmatically

See further

1 Like

Thnx! Gonna install and try it. Will report back the results.

It’s not released yet. Wait untill you see version 2.4.6 of the plugin ready for update in your Wordpress admin panel (I’ll ping you here too).

Hi Angus,

Coming back here; basically “i get it working” although not with the Bricks Builder template.
When i switch to twentytwenty theme the Discourse integration shows below the blog post, replacing the Comments field
image

When using Bricks Builder there is an element “Comments” which loads the WP comments; which is not replaced by the plugin. Neither programmatically called with:

Programmatically called it throws the error:

Error: Too few arguments to function WPDiscourse\Utilities\Utilities::get_discourse_comments(), 1 passed in /home/u43655p432443/domains/domain.nl/public_html/wp-content/themes/bricks/includes/elements/code.php(159) : eval()'d code on line 4 and exactly 4 expected

So its Bricks related. Gonna ask the Bricks commmunity as well.

Joep

For now i just use PHP to call the comments in the template, using

<?php comments_template(); ?>

Which skips Bricks comments element ans uses core WP; that works.

Joep

This error is telling you that your use of the utility function get_discourse_comments needs updating. Here’s an example of how you could use it.

global $post;
use WPDiscourse\Utilities\Utilities as DiscourseUtilities;
$discourse_comments = DiscourseUtilities::get_discourse_comments($post->ID, true, true, 'display-comments');
echo $discourse_comments;