preg_grep – find keys in an array that match a pattern

Sometimes there is a need to find a key/s of an array that matches a particular pattern. array_search searches the array for a given value and returns the corresponding key but it matches the entire word. Here's a scenario. You have a list of tags and you want to provide a user to seach a particular tag which closely matches to their search. So here are some of the tags in an array [crayon-59e6ebf53a7cb218343902/] If the user enters Wordpress we want to show Wordpress Plugins as well as Wordpress Tweaks. So this is how it can be done using preg_grep [crayon-59e6ebf53a7d6980245257/] where $key is the user input i.e. Wordpress in this case In our case $filtered_tags will be an array and will return 0=>Wordpress Plugins 1=>Wordpress Tweaks  ...
Read More

Create wordpress plugin for custom PHP code

Often there is requirement to create your own PHP application and generally most people tend to install a plugin which allows to run PHP code snippets as it is much easier to do so. Although this approach may seem ok it may not be ideal. Lot of times people use this approach because they do not know how to create plugin for the same in Wordpress. Actually it is much easier to create wordpress plugin than you think. I understand it may take a bit longer to create a plugin than just writing a PHP code snippet. However the difference is not that big. Creating your own Wordpress plugin for your requirement works out better in the long run It allows you to deactivate as soon as you feel you do not need the code to run on your site You are in total control of the code. If the PHP plugin which allows you to run your PHP code snippets stops providing support or is no longer...
Read More