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

    (@gwin)

    Hi, this will require quite a lot of programming and digging into the WPAdverts source code.

    The code below will register a new field type

    
    add_action( "init", "my_init" );
    function my_init() {
      adverts_form_add_field("my_adverts_field", array(
        "renderer" => "my_adverts_field",
        "callback_save" => "adverts_save_single",
        "callback_bind" => "adverts_bind_single",
      ));
    }
    function my_adverts_field( $field ) {
       return sprintf('<input type="text" name="%s", value="%s" />', $field["name"], $field["value"] );
    }
    

    Update the my_adverts_field() function to display your own HTML code for the field.

    Now if you would like to use your custom field to display the Phone Number field you could use the code below

    
    add_filter( "adverts_form_load", "customize_adverts_add" );
    function customize_adverts_add( $form ) {
      if( $form['name'] != "advert" ) {
        return $form;
      }
      foreach( $form["field"] as $key => $field ) {
        if( $field["name"] == "adverts_phone" ) {
            $form["field"][$key]["type"] = "my_adverts_field";
        }
      }
      return $form;
    }
    
    Thread Starter hammouche

    (@hammouche)

    I’ve add this in default.php but he doesn’t work

    adverts_form_add_field("my_adverts_field_date", array(
        "renderer" => "my_adverts_field_date",
        "callback_save" => "adverts_save_single",
        "callback_bind" => "adverts_bind_single",
    ));
    function my_adverts_field_date( $field ) {
        return sprintf('<input type="date" name="%s", value="%s" />', $field["name"], $field["value"] );
    }
    
    Plugin Author Greg Winiarski

    (@gwin)

    The code you should add in your child-theme functions.php file or create a new plugin and add it there. If you modify default WPAdverts files then your changes will be overwritten on the next update.

    The first code snippet on its own does not do anything, do you have the second code snippet added somewhere and customized to your needs?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Create new Field Types’ is closed to new replies.