Support » Plugin: WPAdverts - Classifieds Plugin » Tags and Classes

  • Resolved joropress

    (@joropress)


    1. Is there a way to convert advert-tag into a class, in order to attach a badge to the ads with this tag, by using it as a class?

    2. How to automatically add a defined tag, if an ad is checked as a VIP/Featured?

Viewing 15 replies - 1 through 15 (of 30 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    hmm i am not exactly sure which tag you would like to convert and where (on the Ad details page or [adverts_list])?

    Could you add some more details? I should be able to help then, thanks!

    Thread Starter joropress

    (@joropress)

    You are so kind, Greg, as usual. I am totally impressed by the way you try to help anyone and anytime no matter paid or not paid support he uses. Great job and this could be seen all over the forum! Thank you for being what you are! 🙂

    My problem is related to the [adverts_list]. There I need some badges to be shown.
    Another problem is to show all added featured ads automatically in a carousel.
    I am using Wp Posts Carousel Plugin, but in order to work as expected I manually add a specific tag to every featured ad. It would be great if that process could become an automatic.

    Wp Posts Carousel Plugin can show posts in 2 ways – by tags or categories.
    So another option for me (if I do not manage to convert the tags to classes) is to stop using the tags and try putting the badges to the [adverts_list] and ads to the carousel by using a vip/featured category. But I have a feeling I would face the same problem this time with the need to convert the category to a class in order to attach the badges.

    I don’t know which one (the category or the tag solution method) would be easier to achieve and how to do it?

    I really hope so that you have not become much more confused after the detailed explanation 🙂 🙂

    Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    1. ok, the easiest way to display some additional content on the [adverts_list] is to customize the wpadverts/templates/list-item.php file.

    If you will decide to do that consider creating a child template for list-item.php file, so it will not be overwritten on WPAdverts update https://wpadverts.com/documentation/child-themes-and-templates/

    2. some tag can be added to an Advert automatically when saving it in the database, how to do that best depends on:
    – if you want to add the tag to free or paid ads?
    – if paid, then how the Ads are being paid with default Payments Module or with WC integration?

    Thread Starter joropress

    (@joropress)

    A child template is a good precaution – thanx for the advice!

    I would like to add the tag to paid Ads using default Payments Module.
    How could I do that?

    Plugin Author Greg Winiarski

    (@gwin)

    You should be able to do that by adding the code below in your theme functions.php file

    
    add_action("adverts_payment_completed", "tag_an_advert");
    function tag_an_advert( WP_Post $payment ) {
        $type = get_post_meta( $payment->ID, "_adverts_payment_type", true );
        if( $type && $type != "adverts-pricing" ) {
            return;
        } 
        $object_id = get_post_meta( $payment->ID, "_adverts_object_id", true );
        wp_set_object_terms( $object_id, "some-term", "tag", true );
    }
    

    The tag “some-term” should be added once the payment is approved.

    Thread Starter joropress

    (@joropress)

    Hi,
    It doesn’t work, Greg. After adding the code and payment is completed, nothing happens.
    I’ve checked the Ad’s details page in the admin view and no tag seems to be added.

    By the way is it possible if I manage to add the tag automatically when the Ad payment status is turned to “Completed”, then to be able to remove it the same way (automatically) once the Ad is expired?

    And is it possible the same tag to be added again if the Ad is renewed?

    Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    1. do you have the “tags” enabled for the Adverts? If not then this code will not do anything.

    2. you can use post status transitions to remove the tag when Ad changes status to expired https://codex.wordpress.org/Post_Status_Transitions

    
    add_action( 'expired_advert', 'on_expired_advert' );
    function on_expired_advert( $post_id ) {
      // remove the tag here.
    }
    

    3. the code i pasted above should be executed when both publishing a new Ad and when renewing it.

    Thread Starter joropress

    (@joropress)

    Hi,
    1. Yes, I have enabled the tags using this code:

    
    add_filter('adverts_post_type', 'my_adverts_post_type', 10, 2);
    function my_adverts_post_type($args, $type) {
      if($type != 'advert') {
        return $args;
      }
      $args["taxonomies"][] = "post_tag";
      return $args;
    }
    

    2. Thanx a lot!

    3. Great!

    As a matter of fact I am not sure yet why the tag isn’t added after payment being completed (can’t see it anywhere and there are no evidence of its existence) but I’m presuming that it could work after some more trials of me playing with the code : )

    Thanx Greg! If you have any other ideas please let me know if not feel free to mark this thread as resolved!

    Plugin Author Greg Winiarski

    (@gwin)

    Ok, so i understand you have the Tags widget visible in the wp-admin / Classifieds panel when editing the Advert?

    Also in one of the code snippets you do have the line

    
    wp_set_object_terms( $object_id, "some-term", "tag", true );
    

    changed to

    
    wp_set_object_terms( $object_id, "some-term", "post_tag", true );
    

    Finally, the post_tag taxonomy is registered only for posts and pages, to use it with adverts you would need to unregister it and register again for posts, pages and adverts.

    Thread Starter joropress

    (@joropress)

    Everything works almost perfect except that on Ad Renewal the tag does not attach again to the Ad. Am I doing something wrong or should I use the same transition but this time for the Renewal process?

    This is the whole code I use for this:

    add_filter('adverts_post_type', 'my_adverts_post_type', 10, 2);
    function my_adverts_post_type($args, $type) {
      if($type != 'advert') {
        return $args;
      }
      $args["taxonomies"][] = "post_tag";
      return $args;
    }
    add_action("adverts_payment_completed", "tag_an_advert");
    function tag_an_advert( WP_Post $payment ) {
        $type = get_post_meta( $payment->ID, "_adverts_payment_type", true );
        if( $type && $type != "adverts-pricing" ) {
            return;
        } 
        $object_id = get_post_meta( $payment->ID, "_adverts_object_id", true );
        wp_set_object_terms( $object_id, "vip", "post_tag", true );
    }
    add_action( 'expired_advert', 'on_expired_advert' );
    function on_expired_advert( $post_id ) {
      // remove the tag here.
      wp_remove_object_terms( $post_id, 'vip', 'post_tag' );
    }
    Plugin Author Greg Winiarski

    (@gwin)

    Try changing the line

    
    if( $type && $type != "adverts-pricing" ) {
    

    to

    
    if( $type && ! in_array( $type, array( "adverts-pricing", "adverts-renewal" ) ) ) {
    
    Thread Starter joropress

    (@joropress)

    Same result. It doesn’t work on renewal. There’s no tag added after Ad’s renewed. It works only when posting the Ad. I wonder whether I use the right way to remove the tag or there’s a conflict coming out of this?

    wp_remove_object_terms( $post_id, 'vip', 'post_tag' );

    I’m not sure if this is somehow connected but I noticed that on Payment Status bulk update it shows white/blank screen. And this is shown only when updating to the status: “Completed”. I have no problems to change at bulk the Ad’s status to any other except this one. At the same time everything works fine when I change the status to “Completed” not using the bulk option.

    I think this is not a memory issue because I have changed the memory to 256MB using this line in my wp-config.php file:

    define( 'WP_MEMORY_LIMIT', '256M' );

    So I am not sure what is going on – any ideas?

    Plugin Author Greg Winiarski

    (@gwin)

    You seem to be removing the tags properly.

    Regarding white screen, open the wp-config.php file, add there line define("WP_DEBUG", true); and try changing status for the payment to Completed, now aside of the white screen you should also see an error message which you can copy and post here i should be able to help then.

    Thread Starter joropress

    (@joropress)

    It looks like there are 2 errors connected to this – the first one is:

    Notice: Undefined variable: payment in my-site\wp-content\plugins\wpadverts\addons\payments\includes\admin-pages.php on line 378

    This is what I have on line 378 of the admin-pages.php file. You should see the same if you check the original file of the WPAdverts plugin. I didn’t change anything in this file. Is it possible to be a kind of a bug?

    do_action("adverts_payment_".$status_new, $payment);

    and the second:

    Catchable fatal error: Argument 1 passed to tag_an_advert() must be an instance of WP_Post, null given in my-site\wp-content\themes\store-child\functions.php on line 1030

    This is what I have on line 1030:

    function tag_an_advert( WP_Post $payment ) {

    and this is a line from this function:

    add_action("adverts_payment_completed", "tag_an_advert");
    function tag_an_advert( WP_Post $payment ) {
        $type = get_post_meta( $payment->ID, "_adverts_payment_type", true );
        if( $type && ! in_array( $type, array( "adverts-pricing", "adverts-renewal" ) ) ) {
            return;
        } 
        $object_id = get_post_meta( $payment->ID, "_adverts_object_id", true );
        wp_set_object_terms( $object_id, "vip", "post_tag", true );
    }
    • This reply was modified 5 years, 9 months ago by joropress.
    Plugin Author Greg Winiarski

    (@gwin)

    It is possible there is a bug, try changing the line

    
    do_action("adverts_payment_".$status_new, $payment);
    

    to

    
    do_action("adverts_payment_".$status_new, get_post( $id ) );
    

    in very least the fatal error and a notice should now show.

Viewing 15 replies - 1 through 15 (of 30 total)
  • The topic ‘Tags and Classes’ is closed to new replies.