最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugin development - Override class inside $atts shortcode_atts in WordPress

programmeradmin1浏览0评论

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',

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',
Share Improve this question edited Oct 29, 2019 at 17:25 butlerblog 5,1213 gold badges28 silver badges44 bronze badges asked Oct 27, 2019 at 14:11 jade newportjade newport 251 silver badge5 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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)

发布评论

评论列表(0)

  1. 暂无评论