• ResolvedModerator threadi

    (@threadi)


    Hi,

    For a project, I would like to add 1-2 individual hashtags in addition to the hashtags generated via [ap_hashtags] and [ap_hashcats]. I’ve tried a bit in the content field, but can’t find a solution.

    This is currently in the content field:

    [ap_hashtags] [ap_hashcats] <a class="hashtag u-tag u-category status-link" href="/tags/customhash" target="_blank">#customhash</a>

    #customhash arrives in the Fediverse, but is never linked – so nobody is informed about it.

    How would that be possible?

    I would also be open to a PHP solution, but when looking through the plugin I have not yet found a way to add such tags myself – or I am missing something.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Matthias Pfefferle

    (@pfefferle)

    Hey @threadi 👋

    you can write your own transformer and extend the tag list.

    Here is an example how to load a custom transformer: https://github.com/Automattic/wordpress-activitypub/blob/trunk/integration/load.php#L79

    And here is a custom transformer, that extends the base post one: https://github.com/Automattic/wordpress-activitypub/blob/trunk/integration/class-seriously-simple-podcasting.php

    This is the list you want to extend: https://github.com/Automattic/wordpress-activitypub/blob/trunk/includes/transformer/class-post.php#L730

    Moderator threadi

    (@threadi)

    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?

    Plugin Author Matthias Pfefferle

    (@pfefferle)

    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!

    Moderator threadi

    (@threadi)

    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.

    Plugin Author Matthias Pfefferle

    (@pfefferle)

    Be aware that the href has to be a URL!

    Moderator threadi

    (@threadi)

    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?

    Plugin Author Matthias Pfefferle

    (@pfefferle)

    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!

    Moderator threadi

    (@threadi)

    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 🙂

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Using custom hashtags’ is closed to new replies.