How to use PHP to insert content after or before X-th HTML tag

The problem is quite simple…you just want to count a specific tag in a page and insert some content. After or before it’s just a matter of perspective. So let’s say you want an ad, for example, to be inserted, after the fourth H2 tag.

So let’s go and insert an ad code or any other content block after some HTML tag. Here we have a simple PHP function: Continue reading “How to use PHP to insert content after or before X-th HTML tag”

Simple OAuth2 authorization code grant example using PHP and cURL

The authorization code grant methods, should be very familiar if you’ve ever signed into an application using your Facebook or Google account.

The flow is quite simple. The application redirects the user to the authorization server >> the user will then be asked to log in to the authorization server and >> approve access to his data. And if the user approves the application  >> he will be redirected back to the application. Continue reading “Simple OAuth2 authorization code grant example using PHP and cURL”

PHP logical operators true false

The &&  and ||  logical operators seem to have more general recognition and background usage, although the and  and or  form of the same logical operators is taking more and more acceptation for the sake of readability.

So here is a list for the results when evaluating the expressions in PHP. I am sure that this can be applied to a bunch of other languages. (I think it is a little bit superficial to say that it apply to all programming language since I only know some of them). Continue reading “PHP logical operators true false”

WordPress is asking for FTP credentials to update plugins, FINALLY RESOLVED

I was dealing with this problem for a while and did not know how to give WordPress permission to write to his folders when updating plugins or themes.

On the other hand this was forcing me to chmod wp-content/uploads  folder with 777 permissions. I am not a heavy image uploader, so when I was in need of updating I was just chmod to 777 and then, after uploading my stuff, get back to 755.

I am not a Linux lover, geek, guru (or how they call it) so I had no clue about why this happened on my VPS or how to fix that. But as a respectable “self-directed learner” as I am, I had to dig into the situation. Continue reading “WordPress is asking for FTP credentials to update plugins, FINALLY RESOLVED”

Benchmarking on the glob() “dinosaur” and readdir() PHP functions

Well..it’s true. The PHP glob()  function is a big dinosaur, memory eater and speed blow upper (if that can be said). So let’s test the glob()  function against some other alternatives.

I have recently had to detect duplicated/missing files comparing two directories in a set of 80.000 files and I used the PHP glob() function. 80k is a reasonable number for a local machine. But later I had to deal with 800k and that is really a lot if you don’t do things the right way. So I had to try some other alternatives.

I had to do some benchmarking in the area and I used a set of 25.000 images and another folder with 10.000 random duplicates. I think this is a reasonable number to see any differences in the final result.

Continue reading “Benchmarking on the glob() “dinosaur” and readdir() PHP functions”

How to compare two directories for missing files using PHP

Detecting the missing files when comparing two directories can be a tricky job to do. So this is my scenario:

I am trying to pass some image throw a small piece of software for batch processing and I always get timeout due to the big quantity of images. There are around 80.000 images that I am trying to process and the software get stuck to (let say) 10.000 images so I have to start all over again and I have no idea which are the missing files.

So my solution is to take out the images that have been processed from the folder and feed the program with the images that have not been processed.

So to find out this I have to compare the 2 directories for duplicated files. In other words “detect the missing files”

Continue reading “How to compare two directories for missing files using PHP”

Change default 1M max upload filesize limit for WordPress Mutisite

If you are like me playing around with the WordPress Multisite feature and you’ve run into the max allowed upload filesize limit of 1M, you are in the right place to fix it.

There is a not quite intuitive (for me) option in the Super admin dashboard that allows you to change this. Please note that this does not overwrite your apache or php.ini settings if this last one is smaller. Here you can apply the rule of the most restrictive.

So here are the steps to achieve this:

Here are the steps to change the Maximum upload file size within WP admin: Continue reading “Change default 1M max upload filesize limit for WordPress Mutisite”

The easy way to migrate WordPress using search and replace database

So you want to migrate a WordPress database and you came into the problem that when you load the new installation directory you are being redirected to the old domain. You probably tried to manually replace in your PhpMyAdmin in the wp_options  table the two entries that normally exist, but still no success. Maybe your website is loading but there are lots of errors in there.

This is because WordPress and lots of plugins store data and full URI in the database using wp_serialize function. Here is a little example about what I am talking about:

If you just do a search and replace you will break all this fields and data can not be recovered.

Continue reading “The easy way to migrate WordPress using search and replace database”

PHP check if mail() function is enabled on your server

To check if mail function is enabled on your apache server you can try one of the following:

Check your php.ini like this:

You should search for this in the list sendmail_path that has the default  value /usr/sbin/sendmail -t -i

You can also try to manual set it to this value by changing the php.ini file. To do this go to  /etc/php5/apache2/php.ini and uncomment the sendmail_path line like this

Continue reading “PHP check if mail() function is enabled on your server”

Complete case study: watermark detection, remove and place a new watermark- Part II

Like I said in the previous article “Complete case study: watermark replacement – part I” about replacing an old or outdated  watermark, this task is not quite trivial but more likely boring and very time-consuming.

Anyway…let’s continue. I have now separated all images by:

  • logo in the center
  • multiple logos all over.

I will try to process the images that have the logo in the center. In this group I have little bit over 40.000 images. Continue reading “Complete case study: watermark detection, remove and place a new watermark- Part II”