admin管理员组文章数量:1334671
I found a great solution for this issue in this article: How to add wordpress username after url?
/*
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="/?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;
}
My only problem, that the button is active only on the homepage and inactive on all of the other pages on the website (for example mydomain/documents).
Please be so kind and help me to fix it.
Thank you very much!
I found a great solution for this issue in this article: How to add wordpress username after url?
/*
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/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;
}
My only problem, that the button is active only on the homepage and inactive on all of the other pages on the website (for example mydomain/documents).
Please be so kind and help me to fix it.
Thank you very much!
Share Improve this question edited Jun 5, 2020 at 14:56 Baikare Sandeep 3371 silver badge9 bronze badges asked Jun 5, 2020 at 14:10 Andrea HugyakAndrea Hugyak 1 2 |1 Answer
Reset to default 0You can try this shortode in this plugin file or you can paste this method to bottom of active theme's functions.php
. If you are new to WordPress and need a help you can visit here for steps to add code in function.
add_shortcode( 'my-awesome-button', 'wp_my_awesome_bytton' );
function wp_my_awesome_bytton( $atts ) {
$button_html = '';
if ( is_user_logged_in() ) {
$current_user = wp_get_current_user();
$button_html = '<a href="https://direktoriku/shopping/?user=' . esc_attr( $current_user->user_login ) . '"><button>Click me</button></a>';
}
return $button_html;
}
// Use this shortcode anywhere in page/post content like this: [my-awesome-button]
it will replace your shortcode to html code. More information about shortcode visit here.
本文标签: url rewritingHow to add logged in username after WordPress URL
版权声明:本文标题:url rewriting - How to add logged in username after WordPress URL? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742372252a2462446.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
[my-awesome-button]
. Try by adding this shortcode on another page content and check it. – Baikare Sandeep Commented Jun 5, 2020 at 14:35