• Hi, my client would like to set a word limit restriction on ads. Using the ACF addon, I see you can set a min. and max. for characters but not for words. Is there a way to do this? Thanks!

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

    (@gwin)

    Hi,

    we do not have functionality that would limit the description by the number of words, but you can add the code below in your theme functions.php file it will add it

    class Ad_Description_Word_Limiter {
    public function __construct() {
    add_action( "init", [$this, "init"] );
    add_filter( "adverts_form_load", [$this, "form_load"] );
    }

    public function init() {
    if( ! defined( "ADVERTS_PATH" ) ) {
    return;
    }
    adverts_form_add_validator("my_word_limiter", array(
    "callback" => [$this, "validate"],
    "label" => "Word Limiter",
    "params" => array( "max" => 10 ),
    "default_error" => __( "Your description has too many words. Max %max% words.", "wpadverts" ),
    "validate_empty" => false
    ));
    }

    public function validate( $data, $params ) {
    $text = strip_tags( $data );
    $word_count = str_word_count( $text );
    $max_words = absint( $params["max"] );

    if( $word_count > $max_words ) {
    return "invalid";
    }

    return true;
    }

    public function form_load( $form ) {
    if( $form["name"] != "advert" ) {
    return $form;
    }
    foreach( $form["field"] as $k => $f ) {
    if( $f["name"] == "post_content" ) {
    $form["field"][$k]["validator"][] = [
    "name" => "my_word_limiter",
    "params" => [ "max" => 5 ]
    ];
    }
    }
    return $form;
    }
    }
    $Ad_Description_Word_Limiter = new Ad_Description_Word_Limiter();

    In the code look for [ "max" => 5 ] this limits the max words number to 5 if you want to allow more words increase the number.

Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.