I’m having trouble with a custom filter for posting content to Discourse. My installation is the exact same website mentioned by Marco in the topic below (resolved):
Also, because this is a multisite where we want the same WP Discourse customizations across all, instead of using each individual theme’s functions.php file, I created a single file plugin exclusively for WP Discourse template customizations inside the /wp-contents/mu-plugins/ folder. This file includes custom output for discourse_replies_html
, discourse_no_replies_html
, and discourse_publish_format_html
, in that order. I don’t think this my issue, but this is where the code l want to talk about lives.
At the moment, I’m attempting to publish 2 different CPTs and WP native posts to discourse. Before incorporating the CPTs into the mix, I was using the following with expected results:
Now I am experiencing a couple of issues in getting my custom filter to work as I’d like with the CPTs. After referencing [link 1] and [link 2], I successfully added a check for if the post type was ‘event’ (generated by Events Manager) and outputting custom fields associated with that CPT. However, I wanted the default to remain oneboxed, but the aforementioned onebox code snippet returns nothing but a “See Full Post” link. (Clicking to see full post does load the content of the post, fwiw.)
If I replace the onebox code snippet with something else, like
// else return
ob_start();
?>
<small>Originally published at: {blogurl}</small><br><br>
<?php echo $image ?>
{excerpt}
<?php
$output = ob_get_clean();
return $output;
I’ll get the “Originally published at…” + the excerpt, but not $image
(based on [link 3] for checking whether a post in fact has a featured image). I want to have the onebox back if the post type doesn’t match my CPTs, but this curious lack of picking up the featured image in this experiment has me thinking I have a mistake in my logic. I will post the full code I am using at the end of this overview.
My second issue is that my Toolset-generated CPT ‘groups’ doesn’t publish with it’s associated if
post-type customizations. I have the correct post type name, as I’m using it in the theme’s functions.php for a different purpose but with expected results in that context. I based this if
section on my successful if ( 'event' === $post->post_type)
section, but referencing fields that pertain to the ‘groups’ CPT. Even if I have the custom field names wrong in this section, I expect to see at least the featured image and excerpt, etc, as I did when I was working out how to get the right fields for the ‘event’ CPT. Instead, this ‘groups’ CPT is using the // else return
output. So, for example, when I was attempting to use the onebox code snippet, it also only returned the “See Full Post” button (and would also successfully load the post content when clicked).
In conclusion, I’ll reiterate that the if ( 'event' === $post->post_type)
section totally works as intended and I’ve tested many different events with different date settings, with and without the Video Conference URL event attribute. It renders the $image
as expected. Given those hidden fields, it was all sorts of fun to work out, and I hope my code helps someone else struggling to integrate Events Manager with Discourse. However, something seems to be amiss in the combination of that with the rest of what I’m trying to do here, and I hope someone can see where I’m going wrong. Here’s the full function with the non-onebox default:
// when publishing a post to Discourse
function cosmos_custom_publish_format_html() {
global $post;
// if featured image is present, use it
if ( has_post_thumbnail() ) {
$image = "{thumbnail}<br><br>";
} else {
$image = "";
}
// if post is an event, get the custom field information
if ( 'event' === $post->post_type) {
$startdate = get_post_meta($post->ID, '_event_start_date', true);
$starttime = get_post_meta($post->ID, '_event_start_time', true);
$enddate = get_post_meta($post->ID, '_event_end_date', true);
$endtime = get_post_meta($post->ID, '_event_end_time', true);
$startdateformatted = date('j M Y', strtotime($startdate));
$starttimeformatted = date('g:i a', strtotime($starttime));
$enddateformatted = date('j M Y', strtotime($enddate));
$endtimeformatted = date('g:i a', strtotime($endtime));
$eventallday = get_post_meta($post->ID, '_event_all_day', true);
$eventtimezone = get_post_meta($post->ID, '_event_timezone', true);
$videoconferenceurl = get_post_meta($post->ID, 'Video Conference URL', true);
if ($startdate == $enddate && $eventallday !== '1') {
$eventinfo = "<strong>Date/Time:</strong> {$startdateformatted} @ {$starttimeformatted} - {$endtimeformatted} {$eventtimezone}";
}
if ($startdate !== $enddate && $eventallday !== '1') {
$eventinfo = "<strong>Date/Time:</strong> {$startdateformatted} @ {$starttimeformatted} - {$enddateformatted} {$endtimeformatted} {$eventtimezone}";
}
if ($startdate == $enddate && $eventallday == '1') {
$eventinfo = "<strong>Date/Time:</strong> {$startdateformatted} <i>All Day</i>";
}
if ($startdate !== $enddate && $eventallday == '1') {
$eventinfo = "<strong>Date/Time:</strong> {$startdateformatted} - {$enddateformatted}";
}
if ($videoconferenceurl !== '') {
$videoconferenceurlformatted = "<strong>Video Conference Link:</strong> {$videoconferenceurl}<br><br>";
}
if ($videoconferenceurl == '') {
$videoconferenceurlformatted = "";
}
ob_start();
?>
<small>Originally published at: {blogurl}</small><br><br>
<?php echo $image ?>
<?php echo $eventinfo ?><br>
<?php echo $videoconferenceurlformatted ?>
{excerpt}
<?php
$output = ob_get_clean();
return $output;
}
// if post is a group, get the custom field information
if ( 'groups' === $post->post_type) {
$groupstartdate = get_post_meta($post->ID, 'wpcf-start-date', true);
$groupenddate = get_post_meta($post->ID, 'wpcf-end-date', true);
$groupstartdateformatted = date('j M Y', strtotime($groupstartdate));
$groupenddateformatted = date('j M Y', strtotime($groupenddate));
$groupvideoconferenceurl = get_post_meta($post->ID, 'wpcf-video-conference-url', true);
if ($groupenddate !== '') {
$groupinfo = "<strong>Start Date:</strong> {$groupstartdateformatted}<br /><strong>End Date:</strong> {$groupenddateformatted}";
}
if ($groupenddate == '') {
$groupinfo = "<strong>Start Date:</strong> {$groupstartdateformatted}<br /><strong>End Date:</strong> TBD";
}
if ($groupvideoconferenceurl !== '') {
$groupvideoconferenceurlformatted = "<strong>Video Conference Link:</strong> {$groupvideoconferenceurl}<br><br>";
}
if ($groupvideoconferenceurl == '') {
$groupvideoconferenceurlformatted = "";
}
ob_start();
?>
<small>Originally published at: {blogurl}</small><br><br>
<?php echo $image ?>
<?php echo $groupinfo ?><br>
<?php echo $groupvideoconferenceurlformatted ?>
{excerpt}
<?php
$output = ob_get_clean();
return $output;
}
// else return
ob_start();
?>
<small>Originally published at: {blogurl}</small><br><br>
<?php echo $image ?>
{excerpt}
<?php
$output = ob_get_clean();
return $output;
}
add_filter( 'discourse_publish_format_html', 'cosmos_custom_publish_format_html' );