# WP Discourse: Erweiterte benutzerdefinierte Filter für discourse\_publish\_format\_html

**URL:** <https://meta.discourse.org/t/wp-discourse-advanced-custom-filter-for-discourse-publish-format-html/106059>\
**Category:** WordPress\
**Created:** [9. Januar 2019 um 14:35 UTC](https://meta.discourse.org/t/wp-discourse-advanced-custom-filter-for-discourse-publish-format-html/106059 "2019-01-09T14:35:02Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![Kayla](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/kayla/32/128523_2.png) [@Kayla](https://meta.discourse.org/u/Kayla)\
**Post date:** [9. Januar 2019 um 14:35 UTC](https://meta.discourse.org/t/wp-discourse-advanced-custom-filter-for-discourse-publish-format-html/106059/1 "2019-01-09T14:35:02Z")

</div>

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):

> [@WP Discourse: can't access editor for post types created with Toolset Types plugin](https://meta.discourse.org/t/wp-discourse-cant-access-editor-for-post-types-created-with-toolset-types-plugin/105725):
>
> Hi, I am using the [WP Discourse](https://github.com/discourse/wp-discourse) plugin on a Wordpress 5.0.2 multisite, and have successfully had multiple sites on the network integrated with our Discourse instance. This works for default posts and pages, as well as for custom post types created by podcasting and event plugins. However, on custom post types created with the [Toolset Types](https://wordpress.org/plugins/types/) plugin, I am not able to access the editor when enabling the WP-Disource plugin (v. 1.79 and 1.8). Unselecting [WP Discourse](https://github.com/discourse/wp-discourse) for those content types restores …

Also, because this is a multisite where we want the same [WP Discourse](https://github.com/discourse/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](https://github.com/discourse/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:

> [@Customize the structure of WP Discourse templates](https://meta.discourse.org/t/customize-the-structure-of-wp-discourse-templates/50754/1):
>
> #### Publish the WordPress post as a onebox on Discourse
> 
> This customization will cause the WordPress post to be published as a onebox on your forum.
> 
> _Before making this customization, check that oneboxes are created on your forum when you add the URL of one of your WordPress posts to Discourse._
> 
> ```plaintext
> function your_namespace_publish_format_html() {
> ob_start();
> the_permalink(); // The post's WordPress permalink
> $output = ob_get_clean();
> 
> return $output;
> }
> add_filter( 'discourse_publish_format_html', 'your_namespace_publish_format_html' );
> 
> ```

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

```plaintext
// 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:

```plaintext
// 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' );

```

---

_[View the full topic](https://meta.discourse.org/t/wp-discourse-advanced-custom-filter-for-discourse-publish-format-html/106059)._
