这是我为你制作的一个小插件。将此代码放在php文件中,并将其作为插件上传,然后激活它。然后在帖子或页面中输入短代码[我的真棒按钮],它就会出现在那里。如果访问者未登录,则不会显示任何内容。
<?php
/*
Plugin Name: My Awesome Button
Description: The shortcode [my-awesome-button] will be replaced with the HTML code for a button for logged-in users and for guests it will be replaced with an empty string (so it will be removed). This will work for posts and pages.
Author: Nikolay Nikolov
Version: 1.0.0
*/
add_filter( \'the_content\', \'my_awesome_button_function\', 99999999999 );
function my_awesome_button_function( $content ) {
if ( strpos( $content, \'[my-awesome-button]\' ) !== false ) {
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$button_html = \'<a href="https://direktoriku.com/shopping/?user=\' . esc_attr( $current_user->user_login ) . \'"><button>Click me</button></a>\';
$content = str_replace( \'[my-awesome-button]\', $button_html, $content );
} else {
$content = str_replace( \'[my-awesome-button]\', \'\', $content );
}
}
return $content;
}
顺便说一下,我们可以很容易地修改它,只显示用户名,而不是整个按钮。这样你就可以把用户名放在帖子或页面中你想放的任何地方。如果你需要帮助,请告诉我。