1

I have a plugin and I need to tweak one function. Specifically the 'class' => ''.

This is the code in the plugin:

function message_to( $atts, $content = null, $tag = '' ) {
    $atts = shortcode_atts( array(
        'to'        => '{current-post-author}',
        'subject'   => '{current-post-title}',
        'text'      => __( 'Contact','front-end-pm' ),
        'class'     => 'fep-button',
        'fep_mr_to' => false, // Comma separated list of user ids (used in PRO version)
    ), $atts, $tag );
    if ( '{current-post-author}' == $atts['to'] ) {
        $atts['to'] = get_the_author_meta( 'user_nicename' );
    } elseif ( '{current-author}' == $atts['to'] ) {
        if ( $nicename = fep_get_userdata( get_query_var( 'author_name' ), 'user_nicename' ) ) {
            $atts['to'] = $nicename;
        } elseif ( $nicename = fep_get_userdata( get_query_var( 'author' ), 'user_nicename', 'id' ) ) {
            $atts['to'] = $nicename;
        }
        unset( $nicename );
    } elseif ( '{um-current-author}' == $atts['to'] && function_exists( 'um_profile_id' ) ) {
        $atts['to'] = fep_get_userdata( um_profile_id(), 'user_nicename', 'id' );
    } else {
        $atts['to'] = esc_html( $atts['to'] );
    }
    if ( false !== strpos( $atts['subject'], '{current-post-title}' ) ) {
        $atts['subject'] = rawurlencode( str_replace( '{current-post-title}', get_the_title(), $atts['subject'] ) );
    } elseif ( ! empty( $atts['subject'] ) ) {
        $atts['subject'] = rawurlencode( $atts['subject'] );
    } else {
        $atts['subject'] = false;
    }
    if ( empty( $atts['to'] ) && empty( $atts['fep_mr_to'] ) ) {
        return '';
    }
    return '<a href="' . fep_query_url( 'newmessage', array( 'fep_to' => $atts['to'], 'message_title' => $atts['subject'], 'fep_mr_to' => $atts['fep_mr_to'] ) ) . '" class="' . esc_attr( $atts['class'] ) . '">' . esc_html( $atts['text'] ) . '</a>';
}

The original function specifies the class:

'class'     => 'fep-button',

I need to change it to something else via child theme:

'class'     => 'something-else',
0

The way the function is written, the default values are not editable from within the plugin as there is no filter hook to alter them.

However, this is a specific "attribute" of the shortcode. shortcode_atts() is a function that defines the shortcode's attributes (which can be passed through the shortcode itself) and sets their default values. The default is only used if the attribute is not specified in the shortcode. When specified, this passed value of the attribute will be used instead.

So if you want to pass a different class, you would do it when using the shortcode:

[name_of_shortcode class="something-else"]

(Note: I used "name_of_shortcode" since you did not include the specific tag of this shortcode - edit accordingly)

  • Works like a charm and quite an easy solution - thank you! – jade newport Nov 3 '19 at 18:27

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.