admin管理员组

文章数量:1428729

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!

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!

Share Improve this question asked May 1, 2019 at 20:13 GregGreg 152 bronze badges 2
  • Where is your function that collects version information? – anton Commented May 2, 2019 at 5:38
  • @aton The function that collects this is in the functions.php file which includes the /library/custom-widgets.php – Greg Commented May 3, 2019 at 18:53
Add a comment  | 

1 Answer 1

Reset to default 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 );

本文标签: filtersHow can I change the button text of a custom widget