Buddypress get full name print to new wordpress page - php

Referring to How to get username/display name in Buddypress?
I have tried putting this code to an added new page
<?php
$string = bp_get_displayed_user_fullname();
echo $string;
?>
But when i tried previewing the page it prints empty only. Is there something I missed on the plugin to make it working? TIA

bp_get_displayed_user_fullname() will work only on pages, that are displaying some user. That means your profile or profiles of other users. On all other pages you will always see an empty string (forums, ordinary WordPress pages and posts, groups etc).
To display a name of any user on any page you need to use something like bp_core_get_user_displayname( $user_id ), where $user_id is, obviously, the ID of a user you want the name of.

Related

Wordpress: Display different content based on loged in user

I'm running a simple wordpress site where there is a possibility for users to log in. I'm now at a point where I want to display different HTML content based on the currently logged in user.
I know that this can be solved by using PHP and I even have a basic knowledge in using PHP but I just don't know where and how to start.
Thanks for every input and best regards
It is quite simple to do this, once you get to know your way around PHP. You can use this by using shortcodes (which, in HTML, you can call with do_shortcode term) so you will be able to use it with shortcodes and in HTML if needed.
You will first have to register a shortcode first. As you said, you only want a different HTML content for logged in users. To create a shortcode open your functions.php file and copy/paste this code:
add_shortcode('EXAMPLE','show_user_content');
function show_user_content($atts,$content = null){
global $post;
if (!is_user_logged_in()){
return "You aren’t allowed to read this. " . wp_login_url( get_permalink($post->ID) ) . ' to view the content';
}
return $content;
}
You can edit the part which returns the error if someone, who isn't logged, wants to view the content and also the EXAMPLE part in the first row - that is the name of your shortcode, so make it unique. This won't go by roles (admin, mod, editor etc) but by the state if someone is logged in.
After that just use [EXAMPLE] Some content [/EXAMPLE] in your post editor. Be aware that you have to use the text editor of the post, not the visual editor (as it doesn't recognize shortcodes).
If you want to implement this in HTML, just use the do_shortcode function:
<?php echo do_shortcode('[EXAMPLE]Hello world![/EXAMPLE]'); ?>
And just put content inside the 'Hello world!' part.

Add multiple Instagram feeds to theme from username or user ID

I need to be able to let users put in either their Instagram username or user ID into a custom field. I need the feed to export just the recent images, and I found a plugin that works well for the most part. I am using Instagram Feed by Smash Balloon, and I have found that I can use the following code in my theme to display a feed the way I need to:
<?php echo do_shortcode("[instagram-feed id='username']"); ?>
I need to be able to replace the username with the User Id that a user can insert into the Advanced Custom Field text field I have created. I know I can echo that code with this:
<?php the_field('instagram_field'); ?>
How can I take what is echoed from my the_field parameter and insert it into the username space in my shortcode?
If there is a better way to allow for multiple feeds to be added into Custom Post Types I am open to it, but this solved all other issues I had with styling, needing multiple feeds, etc.
The answer is close to correct.
The issue is with declaring the field, the answer provided uses the_field and not get_field. Which should be used for storing variables.
$custom_acf_field = get_field('instagram_field');
echo do_shortcode("[instagram-feed id='$custom_acf_field']");
This should work out ok if not you may wish to declare the shortcode seperately:
$custom_acf_field = get_field('instagram_field');
$insta_code = [instagram-feed id='$custom_acf_field'];
echo do_shortcode($insta_code);
You can assign a variable to your advanced custom field for it to be echoed within the Instagram Shortcode username attribute.
$custom_acf_field = the_field('instagram_field');
echo do_shortcode("[instagram-feed id=$custom_acf_field]");
To then allow multiple feeds/Shortcodes you could wrap the above in a foreach loop with some arguments relevant to each user.

Bio line beneath pp in BuddyPress?

I’m new to BP and WP! I have a quick question:
How can I show a one-line bio beneath usernames in the list of members in the ‘Members’ page of my BuddyPress website? For example, Facebook has a similar feature; it shows a name with a description beneath, e.g. ‘John Smith’ with ‘Engineer at Nuts & Bolts Co’ right beneath it.
I found the following line of code in member-header.php:
<h2 class=”user-nicename”>#<?php bp_displayed_user_username(); ?></h2>1
I thought about adding another line beneath for the bio field, but I’m not sure what to replace “bp_displayed_user_username();” with – I don’t think there is a “bp_displayed_user_bio();”. Should I create it? If so, how?
Thank you for your time!
'member-header.php' is for single member pages.
If you want the bio to display on singe member pages, create a template overload.
Then add this:
<?php
$bio = xprofile_get_field_data('Bio');
//If the field is not called Bio, then change it above
if( !empty( $bio )
echo 'Bio: ' . $bio;
?>
If you want Bio to display on the members page that shows all members, then create a template overload of this page:
\buddypress\bp-templates\bp-legacy\buddypress\members\members-loop.php
And look for this: bp_member_profile_data( 'field=the field name' );
Uncomment it and change to to, for example, bp_member_profile_data( 'field=Bio' );

Content must be able to be shown if one is login page - wordpress

this is how I'm going with wordpress forum
it is such that even if you do not log into the site you can see what you can create, etc.
Create forum / post to the site just being tucked away so it is only possible to view it if you are login on the page.
Comment content must also be away but just get some text that you can not write content until he has sustained itself on the page.
I purchased this forum
http://themeforest.net/item/forumengine-flat-responsive-wordpress-forum-theme/5999646
WordPress comes with all the functionality needed for this its as simple as wrapping the content you want within an if statement for example
<?php if ( is_user_logged_in() ) {
echo "Member Content";
}else{
echo "Sorry Guest";
?>
id suggest something like above if you are working with template files you could always wrap the while loop with the above

Adjust Wordpress function to grab author ID of post

I use a point system plugin for Wordpress. By adding this code to the author.php page:
<?php cp_displayPoints($authordata->ID); ?>
It will echo X Points. This is the points of that respective author. When I add the same code to single.php (post page), it echos the logged in user's points, and if not logged in, it returns blank.
How can I alter this code so that it will function properly on the single.php page too? This would mean that it would echo the points of the author of that post.
Just call get_the_author_meta from within the loop.
So, you just need to test if you have a currently signed in user, if not use the post author instead. Something like this.
<?php
if(!$authordata->ID)
cp_displayPoints(get_the_author_meta('ID'));
else
cp_displayPoints($authordata->ID);
?>
EDIT:
To display only the post author's ID, just use
<?php cp_displayPoints(get_the_author_meta('ID')); ?>

Categories