0

We had a theme developed that has a custom widget class.

class ams_button_widget extends WP_Widget {
...
    public function widget( $args, $instance ) { 
        $btn_text = apply_filters( 'button_text', $instance[ 'button_text' ] );
        $btn_link = apply_filters( 'button_link', $instance[ 'button_link' ] );
        $btn_color_class = apply_filters( 'button_color_class', $instance[ 'button_color_class' ] );

        if( isset( $args[ 'before_widget' ] )) { echo $args['before_widget']; }

        if( isset( $btn_text ) && !empty( $btn_text )) { 
            if( isset( $args[ 'before_button_text' ] )) { echo $args[ 'before_button_text' ]; }
?>
            <a href="<?php echo $btn_link; ?>" class="button rounded <?php echo $btn_color_class; ?>"><?php echo $btn_text; ?></a>
            <?php
            if( isset( $args['after_button_text'] )) { echo $args[ 'after_button_text' ]; }
            if( isset( $args['after_widget'] )) { echo $args[ 'after_widget' ]; }
        } // End if
...

We have two instances of the Button Widget in the Top Bar Widgets of the theme, but the one I need to modify is:

<section id="ams_button_widget-2" class="widget ams_button_widget"> <a href="/help-center/download-update/" class="button rounded yellow">Download  Latest Update</a></section>

I have been searching and reading through WordPress codex, trying to find an exmaple of what I'm needing to do: append the value of a variable to the btn_text.

In the Widgets section, this particular btn_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 default btn_text, ie. Download Latest Update **v1.0.11(2019)**, but am not having any luck.

Do I modify the class or – as would seem more appropriate – create a function in my functions.php that handles it, and if so, can someone offer some explanation?

Thanks for the input!

2
  • Where is your function that collects version information?
    – anton
    May 2 '19 at 5:38
  • @aton The function that collects this is in the functions.php file which includes the /library/custom-widgets.php
    – Greg
    May 3 '19 at 18:53
0

In the Widgets section, this particular btn_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 default btn_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 classes or other code.

Option 2: Enable shortcodes inside the button text

  1. Enable shortcodes:

    add_filter( 'button_text', 'do_shortcode' );
    
  2. 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! :)
    } );
    
  3. 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 );
3
  • @Greg, I saw your deleted comments, so are you still having problems with customizing the button text? But for options 1 and 2, the code should go into the theme functions.php file. And if option 3 doesn't work for you, then perhaps you should post the full widget code - and your custom function which outputs the version information? Alternatively, there is a fourth option.. you could create a custom widget which extends the ams_button_widget widget.. but I'd need to see the full widget code.
    – Sally CJ
    May 5 '19 at 11:49
  • I did get it working, thank you! I ended up using Option 2 but had to get the path correct.
    – Greg
    May 6 '19 at 12:52
  • Alright then. I'm glad you got it working. :)
    – Sally CJ
    May 7 '19 at 2:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.