WordPress Archive Page for Custom Post Type

Get archive link for a specific post type: <a href="<?php echo get_post_type_archive_link( ‘books’ ); ?>">Books List</a><a href="<?php echo get_post_type_archive_link( ‘books’ ); ?>">Books List</a> Set archive permalink for a custom post type when registering the cpt: $args = array( … ‘has_archive’ => ‘books-list’, // can be "true" or the permalink string );   register_post_type( ‘books’ , […]

Download a remote file with cURL and PHP

Save a remote file to disk with PHP and cURL: // Remote file to download $url = "http://www.sample-videos.com/audio/mp3/crowd-cheering.mp3";   // This is where the file will be saved $fp = fopen(dirname(__FILE__).’/localfile.tmp’, ‘w+’); // Replace spaces with %20 on the URL string $ch = curl_init(str_replace(" ","%20", $url)); // We allow cURL to run for max 60 […]

Force Download of Remote File with PHP

Force the download of a remote file with PHP: $file_name = ‘crowd-cheering.mp3’; $file_url = ‘http://www.sample-videos.com/audio/mp3/’ . $file_name; header(‘Content-Type: application/octet-stream’); header("Content-Transfer-Encoding: Binary"); header("Content-disposition: attachment; filename=\"".$file_name."\""); readfile($file_url); exit;$file_name = ‘crowd-cheering.mp3’; $file_url = ‘http://www.sample-videos.com/audio/mp3/’ . $file_name; header(‘Content-Type: application/octet-stream’); header("Content-Transfer-Encoding: Binary"); header("Content-disposition: attachment; filename=\"".$file_name."\""); readfile($file_url); exit; Note that ini.allow-url-fopen must be enabled. Alternatively, download the file to disk and […]

Upload a File via POST with cURL and PHP 7

Example of sending or uploading a file via POST with cURL and PHP 7: File content of php-send-file-curl.php: <?php if ( isset($_POST[‘submitfile’]) ) { // Make sure there are no upload errors if ($_FILES[‘upfile’][‘error’] > 0) { die("Error uploading file…"); }   // Prepare the cURL file to upload, including file name and MIME type […]

Youtube embed iframe not working on mobile devices

Some days ago I noticed that Youtube iframe embed code was not working on mobile devices, instead it showed a blank space on mobile. I used the iframe code gathered from the Youtube -> Share -> Embed window. After some testings, I found the correct Youtube embed iframe code that works on iPhone, iPad and […]

Reduce disk I/O with noatime and nodiratime

Add noatime and nodiratime to the mount options in /etc/fstab: UUID=[…] /webserver ext4 defaults,noexec,nodev,nosuid,noatime,nodiratime 0 2UUID=[…] /webserver ext4 defaults,noexec,nodev,nosuid,noatime,nodiratime 0 2 Remount the modified partitions as follows (use the correct mount point): mount -o remount /webservermount -o remount /webserver You may also apply this to / partition.

Array as parameters in a PHP function

Sample PHP function that uses an array to handle arguments: function testFunction($args) { // $args contains the arguments of the function print_r($args); // Validate and assign params to variables $name = $args[‘name’] ? $args[‘name’] : "None"; $file = $args[‘file’] ? $args[‘file’] : "None"; $title = $args[‘title’] ? $args[‘title’] : "None"; }function testFunction($args) { // $args […]

Change Bootstrap Table Border Color

Change default border color of table-bordered class: .table-bordered { border: 1px solid #ecf0f1 !important; } .table-bordered > thead > tr > th, .table-bordered > tbody > tr > th, .table-bordered > tfoot > tr > th, .table-bordered > thead > tr > td, .table-bordered > tbody > tr > td, .table-bordered > tfoot > tr […]

Restore MySQL datadir on a new server

You can restore a previously copied\moved MySQL datadir on a new server: 1) Stop the MySQL service service mysql stopservice mysql stop 2) Rename the actual MySQL datadir to something different: mv /var/lib/mysql /var/lib/mysql_originalmv /var/lib/mysql /var/lib/mysql_original 3) Copy the MySQL datadir to restore in the MySQL datadir path: cp -R /backups/mysql /var/libcp -R /backups/mysql /var/lib […]

Backup MySQL datadir without Mysqldump

The fastest way to backup MySQL datadir is: 1) Stop the MySQL service service mysql stopservice mysql stop 2) Copy /var/lib/mysql to another location, i.e: cp -R /var/lib/mysql /root/mysqldatadircp -R /var/lib/mysql /root/mysqldatadir 3) Start the MySQL service service mysql startservice mysql start 4) Compress the new MySQL datadir with tar: tar czf /root/mysqldatadir.tar.gz /root/mysqldatadirtar czf […]