• Resolved wpusrnm

    (@wpusrnm)


    Hi, now when user is posting an add, there is default category selected and i want to force the user to select it just to avoid posts in the wrong category. How I achieve that?
    Thanks.

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

    (@gwin)

    Hi,
    you can set default value for advert_category field by adding the code below in your theme functions.php file

    
    add_action( "adverts_form_bind", "default_value_for_adverts_add", 10, 2 );
    function default_value_for_adverts_add( $form, $data ) {
        if( ! isset( $data['advert_category'] ) ) {
            $form->set_value( "advert_category", 25 );
        }
        return $form;
    }
    

    Just replace 25 with actual ID (numeric) of the taxonomy you would like to use as a default.

    Thread Starter wpusrnm

    (@wpusrnm)

    ok, I see I wasn’t clear; my bad.
    anyway, what I want to achieve is to set the categpry to none and not leave the user to post advert until he sellect a category.

    • This reply was modified 6 years, 5 months ago by wpusrnm.
    Plugin Author Greg Winiarski

    (@gwin)

    Ohh ok, in this case you will need to use a different code

    
    add_filter( "adverts_form_load", "single_select_advert_category" );
    function single_select_advert_category( $form ) {
      if( $form['name'] != "advert" ) {
        return $form;
      }
      foreach( $form["field"] as $key => $field ) {
        if( $field["name"] == "advert_category" ) {
            $form["field"][$key]["empty_option"] = true;
            $form["field"][$key]["max_choices"] = 1;
            $form["field"][$key]["validator"] = array();
            $form["field"][$key]["validator"][] = array( 
                "name" => "max_choices",
                "params" => array( "max_choices" => 1 )
            );
        }
      }
      return $form;
    }
    
    Thread Starter wpusrnm

    (@wpusrnm)

    thanks man, that solved it!
    just in case someone wants to implement it.
    you must set the filter that makes the category required after this one.
    big thanks!!!

    • This reply was modified 6 years, 5 months ago by wpusrnm.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Change default category’ is closed to new replies.