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

Requires manual update to toot #58

Closed guusdk closed 1 year ago

guusdk commented 1 year ago

I'm trying to combine your plugin with another plugin: WP Job Manager.

Using that plugin, my WordPress site provides a listing of job offers. Users can add a new job offer through the site. This adds a new post of a custom type ("job"). Job offers are to be reviewed by an admin before they get published.

As soon as a new job offer is reviewed, I would like to have the share-on-mastodon plugin to toot about the new offer. Sadly, that doesn't happen. When looking at the Job in an editor, it does have an "Share on Mastodon" checkbox (that has a check), but only after I manually update the Job using that editor, a toot is sent out (and the job then also gets a link to Mastodon in its attributes).

Is there a way to automate this?

janboddez commented 1 year ago

The checkbox is ticked by default, but the value is only saved when the "post" (or job, etc.) is saved through WP-Admin.

There is a way around this, however: https://jan.boddez.net/wordpress/share-on-mastodon#share_on_mastodon_enabled.

In your case, I believe you may be able to do something like:

add_filter( 'share_on_mastodon_enabled', function( $is_enabled, $post_id ) {
  $post = get_post( $post_id );

  if ( 'job_listing' === $post->post_type ) {
    return true; // _Always_ syndicate "jobs" to Mastodon, even if sharing was not explicitly set.
  }

  return $is_enabled;
}, 10, 2 );

You'd add this using in a site-specific plugin, for instance (or your child theme's functions.php). WP Job Manager apparently suggests using the Code Snippets plugin instead; that would also work.

guusdk commented 1 year ago

Thanks for the speedy response. That snippet solved the problem!

janboddez commented 1 year ago

Perfect. Upcoming release will have a "proper" setting to always (i.e., also if posts were created outside WP-Admin) syndicate (all of) the selected post types.

But a filter like this will always allow for more fine-grained control. E.g., if, say, you also write "normal" blog posts and would like to choose for each one if it gets posted on Mastodon or not, then this filter would allow that (because it only affects "job listings").