unable to obtain buddypress user avatar url - wordpress

I am working on a plugin, inside the plugin, I have made a Javascript file in which I want to have the specific user BuddyPress avatar URL assigned to a variable.
My javascript file inside the plugin folder is named: myscript.js.php (so PHP can be executed inside this file).
The code inside the javascript file:
<?php
header('Content-type: text/javascript');
$home_dir = preg_replace('^wp-content/plugins/[a-z0-9\-/]+^', '', getcwd());
include($home_dir . 'wp-load.php');
$ucurrentid = $current_user->ID;
$member_id = bp_core_get_userid( $ucurrentid );
$uphoto = bp_core_fetch_avatar ( array(
'item_id' =>$ member_id,'html'=>false
) );
?>
var uid = <?php echo $current_user->ID ?>;
var uphoto = <?php echo $uphoto ?>;
alert(uphoto);
This return the error :
PHP Fatal error: Call to undefined function bp_user_avatar()
Why is the function undefined, which BuddyPress file should I include into the code so the function works?

This works for me:
http://viviendoenlaeradelaweb20.blogspot.com/2013/03/buddypress-avatar-url.html
I hope will be useful :-)

You can try :
$avatar_link = bp_core_fetch_avatar(array('html' => false, 'item_id' => $user_id));
$avatar = '<img title="admin" src="' . $avatar_link . '"></img>';
// Display image
echo $avatar
You need user ID. Inside loop, you can do :
$user_id = bp_get_member_user_id();

Related

Header location doesn't work in my wordpress plugin.

I'm writing an Wordpress plugin. With this plugin I update some data. The query and updating works fine, but my header("location: url"); doesn't work. If I place an echo, it won't give any error that the headers already send. It looks it doesn't do anything with those lines. My code...
<?php require_once('../../../wp-config.php');
$baanstatus_table=$wpdb->prefix . 'baanstatus';
$id = $_GET['id'];
$bijgewerkt =$_GET['bijgewerkt'];
$baanstatus= $_GET['baanstatus'];
$handicarts = $_GET['handicarts'];
$trolleys = $_GET['trolleys'];
$winterontheffing = $_GET['winterontheffing'];
$zomergreens = $_GET['zomergreens'];
$qualifying = $_GET['qualifying'];
$onderhoud_greens = $_GET['onderhoud_greens'];
$onderhoud_anders = $_GET['onderhoud_anders'];
$opmerkingen = $_GET['opmerkingen'];
global $wpdb;
$data_array =array('id' => $id,
'bijgewerkt' => $bijgewerkt,
'baanstatus' => $baanstatus,
'handicarts' => $handicarts,
'trolleys' => $trolleys,
'winterontheffing' =>$winterontheffing,
'zomergreens' =>$zomergreens,
'qualifying' =>$qualifying,
'onderhoud_greens' =>$onderhoud_greens,
'onderhoud_anders' =>$onderhoud_anders,
'opmerkingen' =>$opmerkingen
);
$where =array('id' => $id);
$wpdb->update( $baanstatus_table, $data_array, $where );
header("location:http://almeerderhout.fcklap.com/wp-admin/options-general.php?page=my-unique-identifier");
exit();
?>
Perhaps you should try the javascript, instead of PHP location.
<?php
echo '<script>location.href="http://almeerderhout.fcklap.com/wp-admin/options-general.php?page=my-unique-identifier";</script>';
?>
The below code will help you
<?php
wp_redirect( $location, $status );
exit;
?>
The above wordpress function will use to redirect Codex Link function reference
You should hook your plugin to a proper Wordpress action to avoid the "headers already sent error" when trying to redirect.
I found that a good place to perform redirects is the template_redirect action, so you can write something like this:
function do_something_then_redirect() {
// do something with $_GET or $_POST data
// then redirect to some url defined in the $redirect_url variable
wp_redirect($redirect_url);
die;
}
add_action('template_redirect', 'do_something_then_redirect');

Wordpress grab page attributes

Is there a Wordpress function for grabbing the page attributes? I need to be able to check which templates are being used on which pages.
I have tried the get_post and get_pages but neither one outputs the page attributes.
Thanks in advance!
solution:
$ids= get_all_page_ids();
foreach ($ids as $id){
$meta = get_metadata('post', $id);
//var_dump($meta);
$template = $meta['_wp_page_template'][0];
echo $template;
echo "<br>";
}
Try using get_all_metadata. That will fetch all the meta records for a given object.
<?php
$post_id = 123;
$meta = get_metadata('post', $post_id);
echo $meta['my_custom_field_key'];
The docs are a good place to look: Function Reference « WordPress Codex
i.e.: Function Reference/get page template which
Displays the filename of the page template used to render a Page
(printed within an HTML comment, in this example) :
<?php echo '<!-- ' . basename( get_page_template() ) . ' -->'; ?>
And,
global $wp_query;
$template_name = get_post_meta( $wp_query->post->ID, '_wp_page_template', true );
will give you the template file name. Use str_replace() to strip the .php from the end.
`

Adding "Add Friend Button" anywhere in the buddypress site

I'm trying to place an "Add Friend" button outside of the members page, inside a loop, so the button appears next to the author's avatar in a post.
I tried adding ;
<?php if ( bp_is_active( 'friends' ) ) { ?>
<?php bp_add_friend_button( $user_ids['users'][$i]->id ) ?>
<?php } ?>
Inside the loop but it didn't return any results.
Then I placed the following action hook in functions.php, it displayed the button inside the loop but once clicked, all the buttons in the post list got clicked as well.
add_action( ‘the_content’, ‘bp_add_friend_button’, 5 );
So now I'm stuck. I thought, adding a template tag inside the template would work since it had worked for "Send Message" button.
try
<?php if ( function_exists( 'bp_add_friend_button' ) ) : ?>
<?php bp_add_friend_button() ?>
<?php endif; ?>
Here is an example that works for me, hope its helps.
$id_or_email = $result -> user_ID;
$avatar_default = get_avatar($id_or_email, $size, $default = '', $alt = '', $args = null);
$potential_friend_id = $id_or_email;
$friend_status = false;
$friend_button = bp_add_friend_button($potential_friend_id, $friend_status);
echo '<li>'. $avatar_default. $user_info->user_nicename . $friend_button . '</li> ';
There may be a plugin that solve this, but to followup on Rafa's solution.
This complete code worked for me.
In bp-custom.php ( this is a file you need to create)
function my_id_text_processor($friend_id)
{
//Return string id without 'uid-' attached
$search = 'uid-';
$replace_with = '';
return str_replace($search, $replace_with, $friend_id);
}
In the file you want the button to appear put this code.
< ?php $friend_id = my_id_text_processor(bp_get_group_invite_item_id()) ? >
$avatar_default = get_avatar($friend_id, $size, $default = '', $alt = '', $args = null);
$friend_status = false;
$friend_button = bp_add_friend_button($friend_id, $friend_status);
echo '<li>'. $avatar_default,bp_group_invite_user_link(),$friend_button . '</li> ';

Wordpress Settings API error

Hi I am trying to creating some custom options for a template I am developing but I seem to be getting an error:
Warning: Illegal string offset 'show_header' in C:\xampp\htdocs\wordpress\wp-content\themes\01MyWork\includes\theme-options.php on line 62
This is the line that seems to be throwing the error:
$html = '<input type="checkbox" id="show_header" name="thanathos_theme_display_options[show_header]" value="1" ' . checked(1, $options['show_header'], false) . '/>';
And this is the entire code:
<?php
function thanatos_theme_menu(){
add_theme_page(
"Thanathos Theme Options",
"Thanathos Theme",
"administrator",
"thanathos_theme_options",
"thanathos_theme_display_callback"
);
}
add_action('admin_menu' , 'thanatos_theme_menu');
function thanathos_theme_display_callback(){
?>
<div class="wrap">
<div id="icon-themes" class="icon32"></div>
<h2>Sandbox Theme Options</h2>
<?php settings_errors(); ?>
<!--Create the form that will be used to render our options-->
<form method="post" action="options.php">
<?php settings_fields('thanathos_theme_display_options'); ?>
<?php do_settings_sections( 'thanathos_theme_display_options' ); ?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
add_action('admin_init' , 'thanatos_initializa_theme_options');
function thanatos_initializa_theme_options(){
if( false == get_option( 'thanathos_theme_display_options' ) ) {
add_option( 'thanathos_theme_display_options' );
}
add_settings_section(
'general_settings_section',
'Thanatos Options',
'thanatos_general_options_callback',
'thanathos_theme_display_options'
);
add_settings_field(
'show_header',
'Header',
'thanathos_field_header_callback',
'thanathos_theme_display_options',
'general_settings_section',
array( // The array of arguments to pass to the callback. In this case, just a description.
'Activate this setting to display the header.'
)
);
register_setting('thanathos_theme_display_options', 'thanathos_theme_display_options');
}
function thanatos_general_options_callback(){
echo 'mergem la mare';
}
function thanathos_field_header_callback($args){
// First, we read the options collection
$options = get_option('thanathos_theme_display_options');
// Next, we update the name attribute to access this element's ID in the context of the display options array
// We also access the show_header element of the options collection in the call to the checked() helper function
$html = '<input type="checkbox" id="show_header" name="thanathos_theme_display_options[show_header]" value="1" ' . checked(1, $options['show_header'], false) . '/>';
// Here, we'll take the first argument of the array and add it to a label next to the checkbox
$html .= '<label for="show_header"> ' . $args[0] . '</label>';
echo $html;
}
?>
Yes, that's the problematic part:
if( false == get_option( 'thanathos_theme_display_options' ) ) {
add_option( 'thanathos_theme_display_options' );
}
That's the initial if statement at the beginning of the thanatos_initializa_theme_options() function.
You can find the solution in the very neat Theme Options API tut at http://wp.tutsplus.com/tutorials/theme-development/the-complete-guide-to-the-wordpress-settings-api-part-4-on-theme-options/#post-684925289
Or, more exactly more exactly in this article's comments section.
I'm pasting the Steve Bondy's smart solution here because for some reason making the page scroll to the appropriate comment doesn't work for me (in Chrome at least).
START quote
(...)One little issue I had - I followed along through reordering the code, up to just before adding the Social options. At that point I discovered that the code was broken. I would get an error like
Warning: Illegal string offset 'show_header' in ...\themes\Sandbox\functions.php
Warning: Illegal string offset 'show_content' in ...\themes\Sandbox\functions.php
Warning: Illegal string offset 'show_footer' in ...\themes\Sandbox\functions.php
It turns out that this error was caused by adding 'sandbox_theme_display_options' to the options table without giving it a value. If you change sandbox_initialize_theme_options as follows it will create and initialize the options, avoiding the error experienced by myself and others.
function sandbox_initialize_theme_options() {
// If the theme options don't exist, create them.
if( false == get_option( 'sandbox_theme_display_options' ) ) {
$options = array("show_header" => TRUE, "show_content" => TRUE, "show_footer" => TRUE);
add_option( 'sandbox_theme_display_options', $options);
} // end if
(...)
If the old code has been run the empty 'sandbox_theme_display_options' value must be deleted from the database first. Alternatively, the following code will also detect this case and correct it.
function sandbox_initialize_theme_options() {
// See if the options exist, and initialize them if they don't
$options = get_option( 'sandbox_theme_display_options' );
if( false == $options or $options == "" ) {
$options = array("show_header" => TRUE, "show_content" => TRUE, "show_footer" => TRUE);
update_option( 'sandbox_theme_display_options', $options);
} // end if
(...)
This checks for non-existant or empty options values and initializes the options using update_option instead of add_option.
EOF quote
You are using name="thanathos_theme_display_options[show_header]" in plain HTML. Probably you want to parse by PHP the string [show_header].
You may have to run:
delete_option('thanathos_theme_display_options');
during 'admin_init' if you think you have old plugin information mucking about in the database and need a fresh start.

Need help calling up Custom Fields/Values in Wordpress theme

here's the correct code: (Thanks!) (Just had to add '. .' and the value in between when echo)
Uploaded by YouTube user: <?php
$custom_fields = get_post_custom();
$my_custom_field = $custom_fields['ytuser'];
foreach ( $my_custom_field as $key => $value )
echo '' . $value . "<br /><br/>";
?>
If you still want to read the problem, here it is below.
Problem:
Currently, I have a custom field created called "ytuser" on one of my posts. Inside that field, I have typed in a youtube username (for example: youtubeuser1). What I am trying to do now is put that value at the end of a href like this: <a href="http://www.youtube.com/$value"/> so that it actually returns the following address: http://www.youtube.com/youtubeuser1 and the text then links to that user's page.
So far I have this:
Uploaded by YouTube user: <?php
$custom_fields = get_post_custom();
$my_custom_field = $custom_fields['ytuser'];
foreach ( $my_custom_field as $key => $value )
echo '' . $value . "<br /><br/>";
?>
But that only gives me a link to "http://www.youtube.com/"
I can't add $value directly to it...
Is there a way I can include the $value as part of the href? So that it can make the text link go to http://www.youtube.com/$value ?
I know this is a noob question since it can't be that hard.
I would just do $ytuser = get_post_custom_values("ytuser"); and then call the variable $ytuser[0]; where you need it like echo "<a href='http://www.youtube.com/".$ytuser[0]."'>".$ytuser[0]."</a>";. That should work!
Also, in your code example, you want to switch the " and ' usage in your echo and make sure you echo the user after the YouTube URL and as the link itself (if that's what you want).

Resources