Site which you use does not have feature X? Make it yourself!

Hi,

I am using one forum in which I post often and need to find if someone answered. But every time to use search function I have to type in my username, click search button. And I cannot save the search, because in that forum search has its own id. So in address bar query string is ending like this: ‘search.php?searchid=603939′ , and when I bookmark this – it only returns page of the results which were at the moment when it was bookmarked :)

So I did not like it. And one thought came – maybe I can somehow insert my own javascript. And googled – and found out – yeah :)

What I used is “Advanced Page Injector“, you can find it here https://chrome.google.com/webstore/detail/advanced-page-injector/hiadkoalmmkgennhcldmdlndfgagkcim?hl=lt

This is for google chrome, but I bet there are such type of extensions for other decent browsers as well.

So I added such script:

 
var username = 'SPeedFANat1c'; // change here your username
 
var get_param = window.location.search.replace( "?", "" );
 
if (get_param == 'searchbyuser=yes') {
 
var input = document.getElementsByName('searchuser');
input[0].value = username ;
 
var submit = document.getElementsByName('submit');
submit[0].click();
 
}

and voila – it works :) now just bookmark the link which is before script clicks submit button – this link has to end with this ‘?searchbyuser=yes’ string so script can recognize that we want to automatically search our username and it will not try on other searches.

I shared that in the forum, also shared on skype to my internet friend who uses also this forum a lot, he thanked me, he said it will save lot of time for him :) so this feels good :)

How to use database aggregate functions with condition (PostgreSQL) ?

Have you ever needed summing the column data, but not all of it – but depending on another column? You can think about using WHERE in the query. For example:


SELECT SUM(amount) 
  FROM items
     WHERE amount > 5

And it should work. But what if you need sum two times?


SELECT SUM(amount) AS more_than_5, SUM(amount) AS all
  FROM items
     WHERE amount > 5

This time WHERE keyword will not help us – we cannot differentiate.

What is the solution?

I will show practical example:


SELECT
   SUM( CASE WHEN cards_status_id = 4 THEN stake_amount
                  ELSE 0
           END) AS cancel_amount
       FROM cards

In this table there were card records. And I need cards with status canceled (id 4) and I needed also count of all cards (which are not selected this time). The example with CASE can give what we want without using WHERE keyword.

So the CASE gives all rows to the SUM function but, ones are with value ‘stake_amount’, others are with value 0. SUM function takes all rows because we did not use WHERE keyword but adding 0 will do the magic :)

I am not sure how it will work with other databases, but it works on PostgreSQL.

How to change comment reply link text in wordpress?

I had to do that and had spent some time till I found/figured out how to do it without modifying wordpress core files. There are some information on the internet, but I think I will write it in the way it is more understandable for me, maybe for someone it will be usefull as well.

I was using theme which had a file comments.php in its root directory. So I guess the good chance is that your theme also will have this.

In comments.php there is a function

wp_list_comments('avatar_size=70&callback=custom_comment&type=comment');

of course parameters could be different or the function can be even without them. As you can see there is one parameter called ‘callback’. This is the function which overrides the appearance of comment. In our case the function is named ‘custom_comment’ but you can use your own name.

Now we must have that function, or it could have came already with a theme. In my case it came with a theme and it was placed in themes directory includes/theme_comments.php. Probably in another theme this might be something similar. It looked something like this (now its modified by me):

// Fist full of comments
function custom_comment($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>
 
	<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
 
    	<a name="comment-<?php comment_ID() ?>"></a>
 
      	<div class="comment-container">
 
			<?php if(get_comment_type() == "comment"){ ?>
 
            <div class="avatar"><?php the_commenter_avatar($args) ?></div>
 
            <?php } ?>
 
            <div class="comment-right">
 
                <div class="comment-head">
 
                    <span class="name fl"><?php the_commenter_link() ?></span>
 
                    <?php if(get_comment_type() == "comment"){ ?>
 
                        <span class="reply fr"><?php comment_reply_link(array_merge( $args, array('reply_text' => 'Atsakyti' ,'depth' => $depth, 'max_depth' => $args['max_depth']))) ?></span><!-- /.reply -->
                        <span class="date fr"><a href="<?php echo get_comment_link(); ?>" title="<?php _e('Direct link to this comment', 'woothemes'); ?>"><?php echo get_comment_date(get_option('date_format')) ?> <?php _e('at', 'woothemes'); ?> <?php echo get_comment_time(); ?></a></span>
                    <?php }?>
                        <span class="edit fr"><?php edit_comment_link('Edit &nbsp;', '', ''); ?></span>
 
                </div><!-- /.comment-head -->
 
                <div class="comment-entry"  id="comment-<?php comment_ID(); ?>">
 
                    <?php comment_text() ?>
 
                    <?php if ($comment->comment_approved == '0') { ?>
                        <p class='unapproved'><?php _e('Your comment is awaiting moderation.', 'woothemes'); ?></p>
                    <?php } ?>
 
 
                </div><!-- /comment-entry -->
 
            </div><!-- /comment-right -->
 
		</div><!-- /.comment-container -->
 
 
<?php
}

There is a called function comment_reply_link() – as you can see in the array there is item ‘reply_text’ with a value. So change that value by your needs and thats it :)

You also might not see that item, because it is optional, in my case I had to add it myself too. If you want to see all the posible parameters, you can go to wordpress core files – directory includes/comment-template.php and look at the function

get_comment_reply_link()

there is an array $defaults and there are all posible variables, in case you don’t like default values.

Also there is one another, more simple thing, but I will still tell it – you might want to change the text of cancel comment reply link.
So if you want to do that – open comments.php in your theme directory and find a function

cancel_comment_reply_link

and its parameter is string (optional). So if you put some string, it will replace the default text. (It probably is a chance that there is no such function in your theme, but I am not sure, because I myself still haven’t worked too much with wordpress).

How to make “Home” link visible on all pages except the front page in wordpress

Once I needed to do this, but could not find how to do it without editing a code. So by some similar examples I made this code.

Place this in your themes directory, functions.php:

add_filter('wp_get_nav_menu_items', 'removeHome');
function removeHome($menu) 
{
	$homeUrl = get_bloginfo('url') . '/';
	if (is_home()) {	
		foreach ($menu as $key => &$m) {
 
			if ($m->url == $homeUrl) {
				unset($menu[$key]);
				break;
			}
		}
	}
	return $menu;
}

Here we assume that home link has ‘/’ at the end. But if you want that it would not care if its ‘/’ at the end or not, I think it will not hard to modify it. As you can see this script loops through menu items and unsets the array element which has the url as it is our home url.

Nothing fancy in the script itself, but for a some the biggest problem could be to find what to modify, how to get that menu array, what filter to use. It took time for me at least.

Hello!

Welcome, this is my new and first blog. I hope I will add some posts soon.