janboddez / share-on-mastodon

Easily share WordPress posts on Mastodon.
https://jan.boddez.net/wordpress/share-on-mastodon
GNU General Public License v3.0
39 stars 5 forks source link

Custom Post Type posts again when updated #54

Closed crimann closed 1 year ago

crimann commented 1 year ago

I use a custom post type to post photos on my site, which then get posted to mastodon automatically. Problem:

I also use a post category for "notes" and for normal blogposts that get posted automatically, those work fine and also have the IDs saved.

Here's the filter for the format, that I use on my site:

/**
 * Customize Posts for Mastodon Sharing
 */
function claudio_customize_mastodon_post( $status, $post ) {

    // Share content + url, if it is a photo post_type
    if ( $post->post_type == 'photos' ) {
        $status = wp_strip_all_tags( $post->post_content );
        $status .= "\n\nšŸ”— " . get_permalink( $post->ID );
        return $status;
    }

    // Share only the post content if it is in category "note"
    if ( has_category( 'notes', $post ) ) {
        $status = wp_strip_all_tags( $post->post_content );
    }

    // Share title + tags + url, if it is a regular post
    else {
        $status = wp_strip_all_tags( get_the_title( $post->ID ) );
        $tags = get_the_tags( $post->ID );
        if ( $tags ) {
            $temp = '';
            foreach( $tags as $tag ) {
                $temp .= '#' . preg_replace( '/\s/', '', $tag->name ) . ' ';
            }
            $status .= "\n\n" . trim( $temp );
        }
        $status .= "\n\nšŸ”— " . get_the_permalink( $post->ID );
    }

    return $status;

}
add_filter( 'share_on_mastodon_status', 'claudio_customize_mastodon_post', 10, 2 );

Any ideas what I'm doing wrong?

janboddez commented 1 year ago

I think the issue is we check post_type_supports( $post->post_type, 'custom-fields' ) before we save the URL/ID (or an error), whereas we don't do that check before saving _share_on_mastodon.

I.e., you could probably solve this by explicitly adding custom field support to your CPT.

And I think it's safe to remove this check from the plugin (as post meta gets saved even if the CPT "does not support" custom fields, i.e., post meta, but that's going to be for the next version.

janboddez commented 1 year ago

According to https://developer.wordpress.org/reference/functions/post_type_supports/, post_type_supports() is used all over the place, but (probably) not when saving post meta. It should be safe to remove. (I mean, it's kind of obvious, _share_on_mastodon does get saved okay.)

Still, it doesn't hurt to explicitly declare support for custom fields (i.e., post meta) when defining your CPT. Other plugins might run similar checks.

Quick example:

register_post_type( 'my_custom_post_type', array(
  'labels'            => array(
    // And so on ...
  ),
  'public'            => true,
  'has_archive'       => true,
  'show_in_nav_menus' => true,
  'supports'          => array( 'author', 'title', 'editor', 'thumbnail', 'custom-fields' ), // Notice the `'custom-fields'` there.
  'capability_type'   => 'post',
  'map_meta_cap'      => true,
  // Etc.
) );
janboddez commented 1 year ago

@crimann (What's hopefully the fix was) delivered in v0.10.1.

(Unrelated, but me know if you have update issues, not sure what happened on one of my sites but I had to deactivate before it would update. Or maybe it just took a bit of time for WordPress's servers to get up to speed.)

crimann commented 1 year ago

Ah nice catch ā€“ makes sense.

I'll add the custom fields support just in case, but the update fixed it ā€“ Thanks!