Categories
Code Snippets PHP

How to get current directory, filename and code line number in PHP

How to Get the Current Directory, Filename, and Code Line Number in PHP

In PHP, there are several built-in functions that can be used to obtain information about the current script’s directory, filename, and code line number.

Getting the Current Directory

To get the absolute path of the directory containing the current script, you can use the dirname(__FILE__) function. For example:

$current_directory = dirname(__FILE__);

This will set the $current_directory variable to the absolute path of the directory containing the current script.

Getting the Current Filename

To get the name of the current script file, you can use the basename(__FILE__) function. For example:

$current_filename = basename(__FILE__);

This will set the $current_filename variable to the name of the current script file.

Getting the Current Code Line Number

To get the current code line number, you can use the __LINE__ magic constant. For example:

$current_line_number = __LINE__;

This will set the $current_line_number variable to the current code line number.

By using these built-in functions and magic constants, you can easily obtain information about the current script’s directory, filename, and code line number in PHP.

Categories
Code Snippets General PHP Wordpress

[] operator not supported for strings – PHP

I had a problem with Revolution Slider on a WordPress installation when upgrading to PHP 7.2+. It would generate the error – PHP Fatal error: [] operator not supported for strings in C:[path]\wp-content\plugins\revslider\includes\framework\base-admin.class.php:71

The problem was from the plugin attempting to use the short array push syntax on a string:

$box = array();
$box['title'] = $title;
$box['location'] = $location;
$box['content'] = $content;
$box['draw_function'] = $customDrawFunction;
self::$arrMetaBoxes[] = $box;

The solution was to modify the last line as such:

self::$arrMetaBoxes = $box;

No explosions, so all is well!

Why was the operator not supported?

A discussion on Stack Overflow gave me a nudge in the right direction. The suggestion was that PHP 7 has problems with empty-index array push syntax.

These are ok and work properly in PHP 7+.

$undeclared_variable_name[] = 'value'; // creates an array and adds one entry

$emptyArray = []; // creates an array
$emptyArray[] = 'value'; // pushes in an entry

An attempt to use empty index push on any variable declared as a string, number, object, etc, doesn’t works however. ie

$declaredAsString = '';
$declaredAsString[] = 'value';

$declaredAsNumber = 1;
$declaredAsNumber[] = 'value';

$declaredAsObject = new stdclass();
$declaredAsObject[] = 'value';

Theses examples all fail with a PHP error, which in my case was “[] operator not supported for strings”

For more PHP related posts, check here

Categories
Code Snippets General

Display Tiff files on the web

Do you want to display a TIFF image on a webpage? You can learn how here. TIFF files are not made for the web and are not well suited for the web. They used to work in many browsers but as of this writing, support is waning: http://en.wikipedia.org/wiki/Comparison_of_web_browsers#Image_format_support. TIFF files are very large and contain layer information and are meant to preserve quality so they do not compress well. If you display TIFF files you will hurt your page performance.

However, sometimes situations arise where a client requires that their site display TIFF files, so you have to. But how?

How to Display TIFF Files

Enter UTIF.js which was created for the Photopea online photo editor, therefore this library processes your TIFF files and allows you to display them in HTML.

Display TIFF Files on a Website
This is a TIFF file

You can use TIFF images directly inside the <img> element and then, you  just have to to call UTIF.replaceIMG() once at some point. Eg:

<body onload=”UTIF.replaceIMG()”>

<img src=”image.tif” /> <img src=”dog.tif” /> …

Needs fixin’

Categories
Code Snippets PHP Wordpress

WordPress Custom Sortable Columns in Post/Page/CPT Listings

Categories
Code Snippets PHP Wordpress

Bulk Migrate Users to New Blog on WordPress Multisite

Recently I had to move a large number of users from one blog to another on a Multisite install. These users had been mistakenly added to the wrong blog and most of them also had been assigned the wrong role. There were plugin options to do this one by one, but not to bulk migrate users. There were too many users to do it manually.
I needed to get a list of users in the source blog, filter out the users that I didn’t want to modify, move the ones I did, then delete the users that had been moved from the source blog.

Code to Bulk Migrate Users:

$targetblog = 3; // The id of my target blog
 $role = 'agent'; // Custom role
 
 // First I retrieved all of the users in the source blog
 $blogusers = get_users( 'blog_id=1' ); 
 
 // Loop through the Array of WP_User objects.
 foreach ( $blogusers as $user ) {

 // I wanted to only migrate users that weren't already members of the target blog
 // In this case there were only subscribers, agents, and administrators in the database. 
 // Each user had exactly one role. I wanted to move subscribers and agents, 
 // so I used the following check:
 if (! is_user_member_of_blog( $user->ID, $targetblog ) && $user->roles[0] != 'administrator'){
 
 // The following line is only there to give me a bit of a log of events, and to verify that 
 // the correct users were been moved, prior to doing the actual migration operation
 echo '<hr><span>Migrating: ' . $user->ID. ' - ' . esc_html( $user->user_email ) . ' - ' .$user->roles[0] .'</span><br>';
 
 if ( add_user_to_blog( $targetblog, $user->ID, $role ) ) {
 echo 'Added user '.$user->ID.' as '.$role;
 // remove the user from the source blog, and asign their posts to user ID 2
 if ( remove_user_from_blog($user->ID, 1, 2) ) {
 echo 'Removed from source blog';
 } else {
 echo 'Failed to remove user ';
 }
 } else {
 echo 'Failed to add user '.$user->ID.' as '.$role;
 }
 }
 
 }

Some of the important WordPress functions needed to bulk migrate users are:
is_user_member_of_blog
add_user_to_blog
remove_user_from_blog

…among others!