Custom avatar function - php

Hello I work on WordPress and I have a premium theme which is not supported from the creator anymore so I am trying to make a change; I am not sure that this is called a bug. On comments when a visitor is place a comment; his avatar is not the default 'mystery' in this case (no_avatar.gif) but it automatically takes the avatar of the current post author. I like to shows the default for unregistered users. I would like to notice that the theme is use it's own avatars. I target the code on functions but I am not able to change it, any help would be appreciated, thanks in advance here is the code:
function tgt_get_avatar_link($user_id = ""){
if (!empty($user_id)){
$avatar = get_the_author_meta('tgt_image', $user_id);
}
else
$avatar = get_the_author_meta('tgt_image');
if (!$avatar){
return TEMPLATE_URL . '/images/no_avatar.gif';
}
return TEMPLATE_URL . $avatar;
}
Update: The function is calling tgt_get_avatar_link but it seems that is:
function tgt_ad_comment($comment, $args, $depth){
$GLOBALS['comment'] = $comment;
global $helper;
?>
<li>
<div class="comment" id="comment-<?php comment_ID()?>">
<?php //echo get_avatar($comment) ?>
<?php echo $helper->image(tgt_get_avatar_link($comment->user_id), $comment->comment_author, array('title' => $comment->comment_author, 'width' => '58px', 'height' => '58px')) ?>
<div class="comment_content">
<strong><?php echo get_comment_author_link() ?></strong> <?php _e('Say ','ad')?>(<?php comment_time('F j, Y \a\t g:i a') ?>)
<br/>
<?php comment_text() ?>
</div>
</div>
The function is calling $user_id but it seems that is:
function tgt_get_avatar_link($user_id = ""){
if (!empty($user_id)){
$avatar = get_the_author_meta('tgt_image', $user_id);
}
else
$avatar = get_the_author_meta('tgt_image');
if (!$avatar){
return TEMPLATE_URL . '/images/no_avatar.gif';
}
return TEMPLATE_URL . $avatar;
}

If the comment author has no defined avatar, you'd like to show a default image. This seems to be the else in your conditional:
else { $avatar = get_the_author_meta('tgt_image'); }
Without a $user_id to pass to get_the_author_meta, it assumes the user ID of the current post author. This is likely causing the behavior you describe.
Try to remove the else statement, skipping straight from if (!empty($user_id)){$avatar = get_the_author_meta('tgt_image', $user_id);} to if (!$avatar){.... Now, when the function is passed a null user_id, it should return TEMPLATE_URL . '/images/no_avatar.gif';

Related

Other way of displaying image, when only having the link from php

I am making a tool to convert steam profile urls to the different steam ids, whatever.
I have the different functions, triggered when submitting the URL of the profile with a form.
One of the function are, that it gets the avatar of the regarding steam profile.
Actually it gets the link of the image. What I can do is, echo an img element with the link, this wont give me any flexibility since I wont be really able to style etc that afterwards.
Now, the function of getting the avatar, is in the function of the submit form.
I tried just inserting the image URL into an img element.
<img src"<?php echo $avatar ?>">
Well, then I get a error message, saying "$avatar" is undefined, even though I defined it in my main php tags. I think that is due to the fact that it is done inside the form function, could be wrong though.
My main question now is, what could be a different approach to this? I need that in the form function because it should only be called then.
Maybe I just have to use the inconvenient way of echoing an img element every time.
Here is the actual code.
<form method="post">
<input type="text" name="profile_url">
<input type="submit" value="click" name="submit">
</form>
<div id="avatar-div" style="height:100px; width:100px;">
<img src="<?php echo $avatar; ?>">
</div>
the main php part
if(isset($_POST['submit']))
{
display();
}
function display() {
require_once 'steamid.class.php';
$input = $_POST["profile_url"];
$api_key = "xxxxxxxxxxxxxxx";
$id = new SteamID($input,$api_key);
if(substr_count($input, ' ') === strlen($input)) {
echo "Enter URL";
} else {
if ($id->resolveVanity()) {
$avatar = $id->toAvatar();
$communityid = $id->toCommunityID();
echo $communityid . ", " . " ";
$steamid = $id->toSteamID();
echo $steamid . ", " . " ";
$userid = '[U:1:'.$id->toUserID().']';
echo $userid . ", " . " ";
} else {
echo "Profile wasnt found!";
}
}
}
I'm refactoring my answer based on the conversation we had. Here is the recommended PHP code.
function urlCheck() {
$input = $_POST["profile_url"];
if(empty($input)) {
echo "Enter URL";
return false;
} else {
return true;
}
}
function display() {
require_once 'steamid.class.php';
$api_key = "xxxxxxxxxxxxxxx";
$id = new SteamID($input,$api_key);
if (urlCheck()) {
if ($id->resolveVanity()) {
$communityid = $id->toCommunityID();
echo $communityid . ", " . " ";
$steamid = $id->toSteamID();
echo $steamid . ", " . " ";
$userid = '[U:1:'.$id->toUserID().']';
echo $userid . ", " . " ";
return $id->toAvatar();
} else {
echo "Profile wasn't found!";
}
}
}
You haven't mentioned where you're running display(), or where you're expecting the output (echo) to display. I can only assume you want it at the top of the page. Let me explain how to use this.
<head>
$avatar = display();
</head>
<body>
<div id="avatar-div" style="height:100px; width:100px;">
<img src="<?= $avatar ?>">
</div>
</body>
Basically the way this works, is that wherever you run display(), is where the echoes will output (in this case, before the body). Wherever you echo $avatar is where the id will be displayed. I hope this works for you.
Take a look at PHP's Variable Scope
Any variable used inside a function is by default limited to the local function scope.
To fix this problem, use the global keyword. This way you will explicitly set $avatar; as a global variable so it can be used outside of your display() function.
Here is how it would work for your example:
function display() {
global $avatar;
// Rest of your display() function
// Set $avatar somewhere in here
}
display(); // Actually call display so $avatar is set
<div id="avatar-div" style="height:100px; width:100px;">
<img src="<?=$avatar;?>">
</div>

Codeigniter - Working on a "Private message" feature - Sent messages not shown

I am setting up a blog from scratch and everything works great. Now I'm working on a "private message" feature. On my messages function in my controller profile.php, it shows all the messages that the user received. This seems to be working perfectly fine.
My view_message function shows the conversation between the user that is currently logged in and the user that he's talking to. I put the user's (who the logged in user is talking to) user ID in the uri segment.
So, $user_from = $this->uri->segment(3)
Here's my code first:
my profile.php controller (view_message function)
public function view_message() {
$data['page_title'] = 'Read Message';
$data['messages'] = $this->model_message->message();
$this->load->view('includes/header', $data);
if ( $data['messages']) {
$this->load->view('profile/view_message', $data);
} else {
$this->load->view('404/main');
}
}
My model_message.php Model:
public function message(){
$uid_to = $this->session->userdata('uid');
$uid_from = $this->uri->segment(3);
$this->db->select('*');
$this->db->from('messages');
$this->db->where('uid_to', $uid_to);
$this->db->where('uid_from', $uid_from);
$this->db->where('receive_delete', 0);
$this->db->join('users', 'users.uid = messages.uid_from');
$this->db->order_by("mid", "desc");
$query = $this->db->get();
$result = $query->result();
return $result;
}
My view_message.php view (I have removed most of the code and only left the basic code to show what I have basically):
<?php foreach ($messages as $message) :
$uid = $this->session->userdata('uid');
<div class="timeline-body-head">
<div class="timeline-body-head-caption">
<a href="javascript:;" class="timeline-body-title font-red-madison">
<?php if ($uid != $message->uid_to) {
echo 'You';
} else {
echo $message->first_name;
} ?>
</a>
<span class="timeline-body-time font-grey-cascade">
<?php echo date("g:i A - F j, Y ", strtotime($message->sent_date)) ?>
</span>
</div>
</div>
<div class="timeline-body-content">
<span class="font-red-cascade">
<?php echo $message->body ?>
</span>
</div>
I'm only able to see the "recieved" messages, not sent. I am not able to figure that out yet. Any help is appreciated. Thank you very much.

Pass arguments from php function issue

I am working on a wordpress plugin. In plugin, a user first add a slider and then add images of relevant slider.
First i make this with shortcode. User enter the short name of the slider in the shortcode and images slides of relevant slider like this [foo_slider slider=slider_one] or [foo_slider slider=slider_two].
Now i "also" want the snippet, that user can add snippet else shortcode in the code like this echo wp_foo_slider(slider_two). But i dont get that.
Please guide me, how can i do this.
Here is my Code That works for shortcode:
<?php
function wp_foo_sliders($atts) {
global $wpdb;
$tbl_sliders = $wpdb->prefix . "foo_sliders";
ob_start();
extract(shortcode_atts(array(
'slider' => '',
), $atts));
$get_sliders = $wpdb->get_results("Select * From $tbl_sliders Where slider_shortname = '$slider'");
?>
<div class="foo_main_slider">
<?php
foreach ($get_sliders as $get_slider) {
$slider_id = $get_slider->slider_id;
?>
<div class="foo_slider_img">
<?php
$get_slider_image = $wpdb->get_results("Select * From ".$wpdb->prefix."foo_images Where
slider_id = $slider_id Order By image_order ASC");
foreach ($get_slider_image as $foo_img) {
?>
<img src="<?php echo $foo_img->image_path . $foo_img->image_name; ?>" alt="">
<?php
}
}
return ob_get_clean();
}
add_shortcode("foo_slider", "wp_foo_sliders");
?>
I also try this by my own <?php echo wp_foo_sliders("slider_two") ?> or <?php echo wp_foo_sliders(slider_two) ?>, in code and when i refresh the browser only <div class="foo_main_slider"> </div> appears and no images show.
Edit: I want that user can use short code <?php echo do_shortcode('[foo_slider slider=slider_one]'); ?> or user can use snippet <?php echo wp_foo_sliders("slider_two") ?>, only shortcode is working, snippet not work.
What i make mistake please help me.
When you call the shortcode function directly, you are passing a string to it. When you use the shortcode way WordPress will convert the arguments into an associative array.
Try refactoring your code
if( is_array( $atts ) ) {
//Called using shortcode so $atts is an array
extract(shortcode_atts(array(
'slider' => '',
), $atts));
} else {
//Function called directly so $atts is a string
$slider = $atts;
}
So i get my solution by my own.
I make a new function for this:
<?php
function foo_sliders($foo_short_name) {
global $wpdb;
$tbl_sliders = $wpdb->prefix . "foo_sliders";
$get_sliders = $wpdb->get_results("Select * From $tbl_sliders Where slider_shortname = '$slider'");
?>
<div class="foo_main_slider">
<?php
foreach ($get_sliders as $get_slider) {
$slider_id = $get_slider->slider_id;
?>
<div class="foo_slider_img">
<?php
$get_slider_image = $wpdb->get_results("Select * From ".$wpdb->prefix."foo_images Where
slider_id = $slider_id Order By image_order ASC");
foreach ($get_slider_image as $foo_img) {
?>
<img src="<?php echo $foo_img->image_path . $foo_img->image_name; ?>" alt="">
<?php
}
}
ob_start();
return $foo_short_name;
return ob_get_clean();
}
?>
And in theme code:
<?php foo_sliders(short_name) ?>
and its work great

wordpress if not index echo something else

I'd like to give my title <?php echo get_the_title(); ?> conditional statement. If not HOMEPAGE just display whatever it is = <?php echo get_the_title(); ?>. if HOMEPAGE display THIS IS + <?php echo get_the_title(); ?>
so the syntax should be
<?php
$path = $_SERVER['REQUEST_URI'];
$page = basename($path);
$page = basename($path, '.php');
?>
<?php if($page == 'index')
{echo 'This is'. get_the_title(); }
else
{echo get_the_title();}
?>
the problem is I dont really know in wordpress do we use the same way or something smarter or easier.
OH, FORGOT!
Actually, my index/homepage is not real index, it is the page called "HOMEPAGE "
setting from Reading Settings -> a static page, front page => HOMEPAGE
Thanks for any advice.
place your code in functions.php
function title_edit($title){
if(!is_front_page() || get_post_type() != 'page') return $title;
$title = 'Home: '.$title;
return $title;
}
add_filter('the_title','title_edit');
Problem solved thanks #silentboy
<?php if(is_front_page())
echo 'This is'.get_the_title();
else echo get_the_title(); ?>

CakePHP: 3 levels deep associations not working properly

In my app a user has a profile and a user can post comments and posts.
When viewing a list of comments for a post I want to show the name of the person that posted the comment. I have tried the following:
<?php if ( ! empty($post['Comment']) ): ?>
<ul>
<?php foreach ($post['Comment'] as $comment): ?>
<li id="comment-<?php echo $comment['id']; ?>">
<h3><?php echo $this->Html->link($comment['User']['Profile']['firstname'] . ' ' . $comment['User']['Profile']['lastname'], array('controller'=>'profiles','action'=>'view','userName'=>$comment['User']['username'])); ?></h3>
<?php echo $comment['content']; ?>
<?php echo $comment['datetime']; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>No comments...</p>
<?php endif; ?>
But I get the following error: Undefined index: User [APP/View/Posts/view.ctp, line 37]
Any ideas on how to fix the issue?
I have the following for the controller method:
function view ( $id = null, $slug = null )
{
$post = $this->Post->find('first',array('contain'=>array('Comment','User'=>array('Comment','Profile')),'conditions'=>array('Post.id'=>Tiny::reverseTiny($id))));
if (!$post)
{
throw new NotFoundException('404');
}
else if($post['Post']['status'] == '0') // 0=draft 1=open 2=open
{
if($post['Post']['user_id'] == $this->Auth->user('id'))
{
$this->Session->setFlash('Your post has NOT been published yet');
}
else
{
throw new NotFoundException('404');
}
}
if (Inflector::slug($post['Post']['title']) != $slug || $slug = null)
{
$this->redirect(array('id'=>Tiny::toTiny($post['Post']['id']),'slug'=>Inflector::slug($post['Post']['title'])));
}
$this->set(compact('post'));
}
The model associations should all be correct as I can see the comments fine and see the profile info for the post itself, it's just the comments that don't show the profile info.
Thanks to all who can help.
You're setting $post['Comment'] as $comment in your foreach, whilst your user data isn't in $post['Comment']['User'] but in $post['User'], so your call with $comment['User'] won't work, since that index does not exist.
Use debug($var) in the future so you can see how your array structure looks like at any given moment.

Categories