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 !

PHP Solution For Persistent Timeout Error

PHP Solution For Persistent Timeout Error

Perhaps you came across a message such as “Maximum execution time of 180 seconds exceeded” and went googling for a solution. The solution would probably be one of the following

By editing .htaccess

php_value max_execution_time -1

By editing php.ini 

max_execution_time=-1

By editing your PHP code

set_time_limit(-1); 
// OR
ini_set ('max_execution_time', -1);

But even after trying all these.. you are still getting the same error.

Here’s some possible reasons and solutions why.

    1. Your script is getting timed-out by Apache/LiteSpeed/Other Similar Web Server TechnologyIn your httpd.conf (in server config or vhost config), edit the TimeOut variable. The default should be 300.
      TimeOut 600
      
    2. Your using PHP-FastCGI or PHP-FPMThe error you are seeing is from apache’s fastcgi proxy who killed connection to php-fpm pool because your script did not outputted anything for 30 seconds.You can change idle-timeout in your apache config to extend it (cannot be 0):
      FastCgiExternalServer /usr/lib/cgi-bin/php7-fcgi -socket /run/php/php7.0-fpm.sock -idle-timeout 1800 -pass-header Authorization
    3. Apache or PHP isn’t resetting the execution time counter for unknown reasons
      Finally, if you still are not able to get it to work, simply try putting the code

      set_time_limit(-1)

      around different parts of your script/code. This is because each time you call set_time_limit it resets the timer / counter for the script. An example as shown below – I faced a problem with hashing large files and had to consistently make calls to set_time_limit

      $bytesToRead = 1024;
      $fp = fopen($filepath, "r");
      $ctx = hash_init('sha256');
      while (!feof($fp)) {
          hash_update($ctx, fgets($fp,$bytesToRead));
          set_time_limit(-1);  
      }
      $sha256 = hash_final($ctx);  
      fclose($fp);

Conclusion

Hopefully what I’ve shared will be useful for you in solving your issues. Should you need any help, feel free to let me know in the comments below.

Enjoyed the content ? Share it with your friends !