Skip to content

Commit

Permalink
added filter for displayed text
Browse files Browse the repository at this point in the history
  • Loading branch information
Hube2 committed Oct 8, 2016
1 parent 2b29ba0 commit a437fd5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,21 @@ function my_input_counter_filter($ids=array()) {
return $ids;
}
```

### Filter the Display
To filter the display add a filter wherever you would add a filter
```
add_filter('acf-input-counter/display', 'my_acf_counter_filter');
function my_acf_counter_filter($display) {
$display = 'Characters = %%len%% of %%max%%';
return $display;
}
```
In the example string above `%%len%%` represents where you want the current character count shown and `%%max%%`
represents where you want the fields maximum length displayed. You can generate this string any way you want.
For example, you can translate it or use `sprintf()`, as long as it contains the two markers where the values
should be shown. If you do not include these markers then they simply will not be shown. It's up to you to
make sure they are present.

I've puzzled with how to allow this to be altered and this is the best I've been able to come up with.
If you have a better idea, let me know in the issues.
10 changes: 7 additions & 3 deletions acf-input-counter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Plugin Name: ACF Input Counter
Plugin URI: https://github.com/Hube2/acf-input-counter/
Description: Show character count for limited text and textarea fields
Version: 1.1.1
Version: 1.2.0
Author: John A. Huebner II
Author URI: https://github.com/Hube2/
GitHub Plugin URI: https://github.com/Hube2/acf-input-counter/
Expand All @@ -18,7 +18,7 @@

class acf_input_counter {

private $version = '1.1.1';
private $version = '1.2.0';

public function __construct() {
add_action('acf/render_field/type=text', array($this, 'render_field'), 20, 1);
Expand Down Expand Up @@ -99,10 +99,14 @@ public function render_field($field) {
if (!$insert) {
return;
}
$display = 'chars: %%len%% of %%max%%';
$display = apply_filters('acf-input-counter/display', $display);
$display = str_replace('%%len%%', '<span class="count">'.$len.'</span>', $display);
$display = str_replace('%%max%%', $max, $display);
?>
<span class="char-count">
<?php
echo 'chars: <span class="count">',$len,'</span> of ',$max;
echo $display;
?>
</span>
<?php
Expand Down

0 comments on commit a437fd5

Please sign in to comment.