WordPress Find Categories By Name

WordPress Find Categories By Name

The below works for both categories, and other custom taxonomies (e.g genres) you may have.

Find category by an exact match

$cat = get_term_by( 'name', $cat_name, 'category' );

Find category by partial match anywhere

$categories = get_terms( 'category', array( 'name__like' => 'cat_name' ) );

Find category by partial match from the start of the string

$categories = get_terms( 'category', array( 'search' => 'cat_name' ) );
Enjoyed the content ? Share it with your friends !

Javascript Block Source Code Inspection

Javascript Block Source Code Inspection

You may not want users to look at your html, css, or javascript codes. Hence you are here at this page.

You should take not however that, as html/css/javascript is rendered on the client-side, there is no complete way of blocking the user from viewing your code. You can, however, block out the newbies who are attempting this (which would probably account for over 90% of them). Also, know that blocking source code inspection may mean that your site is less user-friendly and may end up irritating legitimate users.

I have prepared a jsfiddle should you wish to test the code. Now, onto the actual code …

Blocking Right Clicks

To block a specific element (and its children) from being right clickable,

// block a specific element (and all its children) from being right-clicked
// use it like so : disable_rightclick("container_id");
function disable_rightclick(element_id)
{
    var elem = document.getElementById(element_id);
    elem.oncontextmenu = function() {return false;};
}

To block the entire page from being right clickable,

document.oncontextmenu = function() {return false;};

 

Blocking Hotkeys

The commonly used hotkeys for source code inspections are

  • Right Click -> Inspect Element (Hotkey: CTRL + SHIFT + I)
  • Right Click -> View Page Source (Hotkey: CTRL + U)

To block the above hotkeys, put the following codes on your page

document.onkeydown = function(e) {
    if(e.keyCode == 123) {
        return false;
    }
    if(e.ctrlKey && e.shiftKey && e.keyCode == 'I'.charCodeAt(0)){
        return false;
    }
    if(e.ctrlKey && e.shiftKey && e.keyCode == 'J'.charCodeAt(0)){
        return false;
    }
    if(e.ctrlKey && e.keyCode == 'U'.charCodeAt(0)){
        return false;
    }

    if(e.ctrlKey && e.shiftKey && e.keyCode == 'C'.charCodeAt(0)){
        return false;
    }      
}

CONCLUSION

Hopefully with these, you are able to block off users will ill-intent. Do remember that you will be sacrificing usability for security and you should determine for yourself if the trade-off is worth it or not.

Enjoyed the content ? Share it with your friends !

C# How to compile ConfuserEx

C# How to compile ConfuserEx

ConfuserEx is an open-source, free protector for .NET applications. The product itself is great, but the code and internals are even greater – it allows us to learn how it works and study them. So for anyone who is interested in the nitty-gritty, technical aspect of how obfuscators work, ConfuserEx is a great source to learn from.

For programmers who are relatively new, chances are that you will come into problems when trying to compile open-source projects with a larger codebase. This is because these projects (such as ConfuserEx) will rely on various modules and dependencies that you may not have.

How To Compile

First, ensure that you have these installed

  1. Visual Studio 2017 (Download Here)
  2. .NET Core 2.1 or later (Download Here)

Second, download the latest ConfuserEx solution from their github

Third, extract it to wherever you want to. After extraction, check to ensure that the dnlib folder is not empty (mine was when I tried). If it is empty, go to ConfuserEx github and download the folder manually (not the entire solution). The folder name should be dnlib @ <snipped hash> .Then, copy the dnlib solution into the empty dnlib folder

Fourth, open up the solution file that is named Confuser2.sln

Fifth, enter CTRL+SHIFT+B to build the solution. Hopefully everything goes well and it compiles, if it doesn’t, let me know in the comments below and I will see how I can help you.

 

 

 

Enjoyed the content ? Share it with your friends !

PHP WordPress Remove Paragraph and Line Break Tags

PHP WordPress Remove Paragraph and Line Break Tags

I find myself needing to remove the paragraph and line break tags that are automatically added into the post or category description quite often. So here is how it can be done

For Categories or Custom Taxonomies

add_filter( 'term_description', 'do_shortcode' ); /*Enable shortcode for term_description (incl. category/other taxonomy)*/
add_filter( 'term_description', 'shortcode_unautop' ); /*Don’t auto-p wrap shortcodes that stand alone*/
add_filter( 'term_description', 'wpautop' , 100); /*Remove line breaks from term_description by pushing priority to the end. Alternatively, you can remove the filter entirely*/
remove_filter('term_description', 'wpautop'); /*Remove line break filter completely*/

For Posts

add_filter( 'the_content', 'do_shortcode' ); /*Enable shortcode for term_description (incl. category/other taxonomy)*/
add_filter( 'the_content', 'shortcode_unautop' ); /*Don’t auto-p wrap shortcodes that stand alone*/
add_filter( 'the_content', 'wpautop' , 100); /*Remove line breaks from term_description by pushing priority to the end. Alternatively, you can remove the filter entirely*/
remove_filter('the_content', 'wpautop'); /*Remove line break filter completely*/

 

Enjoyed the content ? Share it with your friends !

PHP Check If A Resource Pointed By Url Is Valid

PHP Check If A Resource Pointed By Url Is Valid

Sometimes we might have to check if a particular video or image exists and works, and to do so, some people might choose to download the entire resource file using something like file_get_contents which might not be the most effective and efficient way of doing so.

A better way is to use curl and simply check the http status code returned.

/*
* Check if a resource is valid (or exists)
* Works with pretty much anything - jpg, mp4 ...
*
* @url : the url must be a direct link (or hotlink) to the resource.
*/
function is_resource_valid($resource_url)
{
    $resource_exists = false;
     
    $ch = curl_init($resource_url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_exec($ch);
    $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
     
    if($status_code == '200'){
        $resource_exists = true;
    }		
    
    return $resource_exists;
}

 

Enjoyed the content ? Share it with your friends !