In the
Widgets
section, this particularbtn_text
is set to
[and always will be]Download Latest Update
. I have a function
that collects some version information that I want to append after the
defaultbtn_text
, ie.Download Latest Update **v1.0.11(2019)**
The widget has a filter named button_text
which you could utilize to customize the button text without having to change the widget class
:
$btn_text = apply_filters( 'button_text', $instance[ 'button_text' ] );
Option 1: Check if the button text is exactly Download Latest Update
And if so, then call your function which collects the version information — be sure to change the version_func
to the correct function name:
add_filter( 'button_text', function( $text ){
if ( 'Download Latest Update' === $text ) {
$text .= ' **' . version_func() . '**';
}
return $text;
} );
PS: I suggest you to use a unique button text to avoid changing (or messing with) other buttons.. or whatever since the filter name is quite generic and might be applied by other widget class
es or other code.
Option 2: Enable shortcodes inside the button text
-
Enable shortcodes:
add_filter( 'button_text', 'do_shortcode' );
-
Create a shortcode which returns the version information:
add_shortcode( 'my-version-func', function(){ return version_func(); // I'm assuming that the function does NOT echo anything! :) } );
-
In the button text input (on the Widgets admin page), add
[my-version-func]
wherever you like; e.g.Download Latest Update **[my-version-func]**
.
Option 3: Modify the widget class
/code
I don’t recommend this option, but anyway, here’s an example:
Replace this:
$btn_text = apply_filters( 'button_text', $instance[ 'button_text' ] );
with this one:
$btn_text = trim( $instance['button_text'] );
if ( 'Download Latest Update' === $btn_text ) {
$btn_text .= ' **' . version_func() . '**';
}
$btn_text = apply_filters( 'button_text', $btn_text );