How to insert custom linked icon to the template

I’m trying to create a “Feature” star icon that appears next to each listing (just like when using the HivePress Paid Listings extension).

Instead of integrating with the actual paid listing flow, I simply want to display the star icon button for each listing, which links to a custom URL format like: /pricing?{listing_id}

Is there a recommended PHP snippet or filter hook that I can use to append this custom action link without modifying template files?

Thanks for your guidance! :folded_hands:

Hi,

Thanks for the details. You need to use hook hivepress/v1/templates/listing_edit_block to add any custom content without overwriting the entire template. You can view examples at this link. Also, please see the example of how this is done in Paid Listings: hivepress-paid-listings/includes/components/class-listing-package.php at master · hivepress/hivepress-paid-listings · GitHub.

I hope it helps

1 Like

Hey Andrii

Thank you for the amazing explanation :slight_smile:

This is what I managed to come up with. Is this correct?

add_filter(
    'hivepress/v1/templates/listing_edit_block',
    function( $template ) {
        return hivepress()->helper->merge_trees(
            $template,
            [
                'blocks' => [
                    'listing_actions_primary' => [
                        'blocks' => [
                            'listing_feature_link' => [
                                'type' => 'content',
								'content' => '<a href="" title="Feature" class="hp-listing__action hp-listing__action--feature hp-link"><i class="hp-icon fas fa-star"></i></a>',
								'_order'  => 10,
                            ],
                        ],
                    ],
                ],
            ]
        );
    },
    1000
);

I’m still struggling to get the desired URL (/pricing?{listing_id}) within the href tag.

Can you please help me with this :folded_hands:

Hi,

Yes, everything seems to be correct. Additionally, we recommend that you also use the get_the_ID() function, which will return the listing ID, and you can use it in the href.

Thanks for the help again, Andrii :slight_smile:

I managed to rewrite the code to get the listing id. Can you please have a look at it?

Is it the right way to do this? Or am I missing something/doing something wrong?

add_filter(
    'hivepress/v1/templates/listing_edit_block',
    function ( $template ) {
        $listing_id = get_the_ID();
        
        if ( ! $listing_id || 'hp_listing' !== get_post_type( $listing_id ) ) {
            return $template;
        }

        $feature_url = add_query_arg(
            'listingid',
            absint( $listing_id ),
            home_url( '/checkout/' )
        );

        return hivepress()->helper->merge_trees(
            $template,
            [
                'blocks' => [
                    'listing_actions_primary' => [
                        'blocks' => [
                            'listing_feature_link' => [
                                'type'    => 'content',
                                'content' => sprintf(
                                    '<a href="%s" title="%s" class="hp-listing__action hp-link"><i class="hp-icon fas fa-star"></i></a>',
                                    esc_url( $feature_url ),
                                    esc_attr__( 'Feature' )
                                ),
                                '_order'  => 10,
                            ],
                        ],
                    ],
                ],
            ]
        );
    },
    1000
);

Thanks again :slight_smile:

Hi,

Yes, this snippet should work correctly. Thank you for sharing it, it will be useful for our community.

1 Like