Chrome Extension – Get Current Or Active Tab Id And URL

Chrome Extension – Get Current Or Active Tab Id And URL

Here’s a quick code snippet on how you can get your current tab details when developing a chrome plugin or extension.

To get current tab id,

var tabId = chrome.tabs.getSelected(null, function(tab) {
    var tabId = tab.id;
    
    return tabId;
});

To get current tab url,

var tabUrl = chrome.tabs.getSelected(null, function(tab) {
    var tabUrl = tab.url;
    
    return tabUrl;
});

That’s all. Let me know if you need any assistance in the comments.

Enjoyed the content ? Share it with your friends !

PHP Check If A Linux Command Is Valid

PHP Check If A Linux Command Is Valid

Sometimes, you might end up using the linux terminal via PHP, and it is good practise for you to perform checks before executing commands. Here’s how you can check if a command is even valid for that linux terminal, or OS.

In linux, the command would be command -v <command>. In this scenario, for example, command -v ifconfig. If the ifconfig command exists, then it would return the path of the associated command like so /usr/sbin/ifconfig. If the command doesn’t exist, then it would simply not return anything.

Here’s a simple PHP wrapper function to perform the task mentioned above.

function is_command_valid($command)
{
    $check_command_string = "command -v {$command}"; // e.g command -v youtube-dl 
    exec($check_command_string, $output, $return_var);
    if( !empty($output) ) {
        return true;
    }
    return false;
}

To use it, simply call the function like so, is_command_valid('ifconfig');

That’s all.

Enjoyed the content ? Share it with your friends !

PHP WordPress How To Display All Of Your Post Tags

PHP WordPress How To Display All Of Your Post Tags

Here is how you can display all of your post tags very easily. Add the code below to your functions.php and just place the shortcode wherever you want the tags to be shown – a post, a page, a widget, or anywhere else.

function display_all_post_tags($attributes)
{
    $tags = get_tags('post_tag'); //taxonomy=post_tag
    
    $string =<<<CSS
<style>
.tagbox { 
background-color:#eee;
border: 1px solid #ccc;
margin:0px 10px 10px 0px;
line-height: 200%;
padding:2px 0 2px 2px;
 
}
.taglink  { 
padding:2px;
}
 
.tagbox a, .tagbox a:visited, .tagbox a:active { 
text-decoration:none;
}
 
.tagcount { 
background-color:black;
color:white;
position: relative;
padding:2px;
}	
</style>
CSS;
    
    foreach ($tags as $tag) { 
        //$string .= '<span class="tagbox"><a class="taglink" href="'. get_tag_link($tag->term_id) .'">'. $tag->name . '</a><span class="tagcount">'. $tag->count .'</span></span>' . "\n";
        $string .= '<span class="tagbox"><a class="taglink" href="'. get_tag_link($tag->term_id) .'">'. $tag->name . '</a></span>' . "\n";
    } 
    return $string;
    
}
add_shortcode('display_all_post_tags', 'display_all_post_tags');

The code includes some simple styling, which you can modify to suit your own WordPress theme. On top of that, there is a commented line that is like so

//$string .= '<span class="tagbox"><a class="taglink" href="'. get_tag_link($tag->term_id) .'">'. $tag->name . '</a><span class="tagcount">'. $tag->count .'</span></span>' . "\n";

Currently, the code doesn’t display the number of post the tag is linked with. If you wish to display the post count, simply uncomment the above line of code in the snippet given above, and comment out the existing

$string .= '<span class="tagbox"><a class="taglink" href="'. get_tag_link($tag->term_id) .'">'. $tag->name . '</a></span>' . "\n";

If you need any help, let me know in the comments below.

Enjoyed the content ? Share it with your friends !

Redirect To Another Host With Query String Htaccess

Redirect To Another Host With Query String Htaccess

If you wish to redirect your current domain, along with its query strings, to another host or domain, you can do so with the following .htaccess code. Just replace www.newdomain.com with whatever domain you wish to redirect to

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*) http://www.newdomain.com%{REQUEST_URI} [R=302,NC]

 

Enjoyed the content ? Share it with your friends !