I’m afraid that’s not going in the right direction. I would like to add hashtags that have no relation to any objects in WordPress. They simply serve as general additional hashtags on every single post that is to be posted. How do I add them?
At first I thought I just had to make the output exactly like the shortcode [ap_hashtags]. It just generates links: https://github.com/Automattic/wordpress-activitypub/blob/trunk/includes/class-shortcodes.php#L41 – that’s exactly what I wrote in the field, but they seem to be ignored. What am I missing here?
You have to add the tags to the tags array I mentioned above! the array is the important resource, not what you add to the content. the search is not checking you content, but the meta data!
I see 🙂 I tried the following Transformer, unfortunately without success because once again nothing is transferred even after 30 minutes:
use Activitypub\Transformer\Post;
/**
* This is a transformer that extends the default transformer for WordPress posts.
*/
class mycustomhashs extends Post {
/**
* Returns a list of additional Tags, used in the Post.
*
* @return array The list of Tags.
*/
protected function get_tag() {
$tags = array(
array(
'type' => 'Hashtag',
'href' => 'customhash',
'name' => esc_hashtag( 'Customhash' ),
)
);
return array_merge( parent::get_tag(), $tags );
}
}
And:
add_action( 'plugins_loaded', function() {
add_filter(
'activitypub_transformer',
function ($transformer, $data, $object_class) {
if (
'WP_Post' === $object_class
) {
require_once __DIR__ . '/activitypub/class-mycustomhashs.php';
return new mycustomhashs($data);
}
return $transformer;
},
10,
3
);
});
My fault or my impatience?
Using version 3.3.3.
Be aware that the href has to be a URL!
I don’t have a URL for it. These are simply hashtags under which all posts should also be found. Can you just use the home URL for this?
With the above code, nothing is transferred to the Fediverse (even with the home URL as tag-href). As soon as I deactivate the Transformer it works again. Have I forgotten something?
I have no idea if the home URL is also working in your case, but I am sure that it has to be a URL!
By the way, I solved this problem as follows:
I created the desired additional tags as such. However, since I don’t want to assign them to every post, I used the following code:
add_filter( 'get_the_tags', function( $terms ) {
$tag1 = get_term_by( 'term_id', 42, 'post_tag' );
if( $tag1 instanceof WP_Term ) {
$terms[] = $tag1;
}
$tag2 = get_term_by( 'term_id', 43, 'post_tag' );
if( $tag2 instanceof WP_Term ) {
$terms[] = $tag2;
}
return $terms;
} );
This means that I add these two tags every time tags are read. That’s more than enough for my project. The additional tags appear as hashtags in the Fediverse and nowhere else. I use a redirection plugin to redirect the URLs of the tags to the home page.
Solved 🙂