What's new in PHP 8 - Part 1(New features)

As mentioned in previous post What’s new in PHP 8 – Part 1(New features) : this is a major update and thus there will be breaking changes. The best thing to do is take a look at the full list of breaking changes over at the UPGRADING document.

Many of these breaking changes have been deprecated in previous 7.* versions though, so if you’ve been staying up-to-date over the years, it shouldn’t be all that hard to upgrade to PHP 8.

Consistent type errors rfc

User-defined functions in PHP will already throw TypeError, but internal functions did not, they rather emitted warnings and returned null. As of PHP 8 the behaviour of internal functions have been made consistent.

Reclassified engine warnings rfc

Lots of errors that previously only triggered warnings or notices, have been converted to proper errors. The following warnings were changed.

  • Undefined variable: Error exception instead of notice
  • Undefined array index: warning instead of notice
  • Division by zero: DivisionByZeroError exception instead of warning
  • Attempt to increment/decrement property ‘%s’ of non-object: Error exception instead of warning
  • Attempt to modify property ‘%s’ of non-object: Error exception instead of warning
  • Attempt to assign property ‘%s’ of non-object: Error exception instead of warning
  • Creating default object from empty value: Error exception instead of warning
  • Trying to get property ‘%s’ of non-object: warning instead of notice
  • Undefined property: %s::$%s: warning instead of notice
  • Cannot add element to the array as the next element is already occupied: Error exception instead of warning
  • Cannot unset offset in a non-array variable: Error exception instead of warning
  • Cannot use a scalar value as an array: Error exception instead of warning
  • Only arrays and Traversables can be unpacked: TypeError exception instead of warning
  • Invalid argument supplied for foreach(): TypeError exception instead of warning
  • Illegal offset type: TypeError exception instead of warning
  • Illegal offset type in isset or empty: TypeError exception instead of warning
  • Illegal offset type in unset: TypeError exception instead of warning
  • Array to string conversion: warning instead of notice
  • Resource ID#%d used as offset, casting to integer (%d): warning instead of notice
  • String offset cast occurred: warning instead of notice
  • Uninitialized string offset: %d: warning instead of notice
  • Cannot assign an empty string to a string offset: Error exception instead of warning
  • Supplied resource is not a valid stream resource: TypeError exception instead of warning

The @ operator no longer silences fatal errors

It’s possible that this change might reveal errors that again were hidden before PHP 8. Make sure to set display_errors=Off on your production servers!

Default error reporting level

It’s now E_ALL instead of everything but E_NOTICE and E_DEPRECATED. This means that many errors might pop up which were previously silently ignored, though probably already existent before PHP 8.

Default PDO error mode rfc

From the RFC: The current default error mode for PDO is silent. This means that when an SQL error occurs, no errors or warnings may be emitted and no exceptions thrown unless the developer implements their own explicit error handling.

This RFC changes the default error will change to PDO::ERRMODE_EXCEPTION in PHP 8.

Concatenation precedence rfc

While already deprecated in PHP 7.4, this change is now taken into effect. If you’d write something like this:

echo "sum: " . $a + $b;

PHP would previously interpret it like this:

echo ("sum: " . $a) + $b;

PHP 8 will make it so that it’s interpreted like this:

echo "sum: " . ($a + $b);

Stricter type checks for arithmetic and bitwise operators rfc

Before PHP 8, it was possible to apply arithmetic or bitwise operators on arrays, resources or objects. This isn’t possible anymore, and will throw a TypeError:

[] % [42];
$object + 4;

Namespaced names being a single token rfc

PHP used to interpret each part of a namespace (separated by a backslash \) as a sequence of tokens. This RFC changed that behaviour, meaning reserved names can now be used in namespaces.

Saner numeric strings rfc

PHP’s type system tries to do a lot of smart things when it encounters numbers in strings. This RFC makes that behaviour more consistent and clear.

Saner string to number comparisons rfc

This RFC fixes the very strange case in PHP where 0 == "foo" results in true. There are some other edge cases like that one, and this RFC fixes them.

Reflection changes

A few reflection methods have been deprecated:

  • ReflectionFunction::isDisabled()
  • ReflectionParameter::getClass()
  • ReflectionParameter::isCallable()

You should now use ReflectionType to get information about a parameter’s type:

$reflectionParameter->getType()->allowsNull();

If the type is a single type, ReflectionParameter::getType() returns an instance of ReflectionNamedType, which you can get its name from and whether it’s built-in:

$reflectionParameter->getType()->getName();
$reflectionParameter->getType()->isBuiltin();

If the type is a union type however, you’ll get an instance of ReflectionUnionType, which can give you an array of ReflectionNamedType like so:

$reflectionParameter->getType()->getTypes();

Checking whether a type is a union or not can be done with an instanceof check:

if ($reflectionParameter->getType() instanceof ReflectionNamedType) { 
    // It's a single type
}

if ($reflectionParameter->getType() instanceof ReflectionUnionType) {
    // It's a union type
}

Next up, three method signatures of reflection classes have been changed:

ReflectionClass::newInstance($args);
ReflectionFunction::invoke($args);
ReflectionMethod::invoke($object, $args);

Have now become:

ReflectionClass::newInstance(...$args);
ReflectionFunction::invoke(...$args);
ReflectionMethod::invoke($object, ...$args);

The upgrading guide specifies that if you extend these classes, and still want to support both PHP 7 and PHP 8, the following signatures are allowed:

ReflectionClass::newInstance($arg = null, ...$args);
ReflectionFunction::invoke($arg = null, ...$args);
ReflectionMethod::invoke($object, $arg = null, ...$args);

Stable sorting rfc

Before PHP 8, sorting algorithms were unstable. This means that the order of equal elements wasn’t guaranteed. PHP 8 changes the behaviour of all sorting functions to stable sorting.

Fatal error for incompatible method signatures rfc

From the RFC: Inheritance errors due to incompatible method signatures currently either throw a fatal error or a warning depending on the cause of the error and the inheritance hierarchy.

Other deprecations and changes

During the PHP 7.* development, several deprecations were added that are now finalised in PHP 8.

What's new in PHP 8 - Part 1(New features)

PHP 8 is here! It was released on November 26, 2020. You can download it here. It’s a new major version, which means that it will introduce some breaking changes, as well as lots of new features and performance improvements.

Besides breaking changes, PHP 8 also brings a nice set of new features such as the JIT compiler, union types, attributes, and more. Let’s start with what’s new in php 8, it’s quite a list!

New features Explained:

Union types rfc

Given the dynamically typed nature of PHP, there are lots of cases where union types can be useful. Union types are a collection of two or more types which indicate that either one of those can be used.

public function foo(Foo|Bar $input): int|float;

Note that void can never be part of a union type, since it indicates “no return value at all”. Furthermore, nullable unions can be written using |null, or by using the existing ? notation:

public function foo(Foo|null $foo): void;

public function bar(?Bar $bar): void;

JIT (Just in Time compiler) rfc

The JIT — just in time — compiler promises significant performance improvements, albeit not always within the context of web requests. The JIT compiler is sort of a middle ground between compilation and interpretation. It will compile and cache some sections of code at runtime so that the compiled version can be used instead of the interpreted version. This could lead to huge performance gains for PHP, but with some caveats. Generally, JIT compilers mostly benefit CPU-intensive applications, such as 3D rendering or large mathematical computations. If you’re using PHP for web applications, you may not see a substantial performance boost by enabling the JIT compiler.

The nullsafe operator rfc

If you’re familiar with the null coalescing operator you’re already familiar with its shortcomings: it doesn’t work on method calls. Instead you need intermediate checks, or rely on optional helpers provided by some frameworks:

$startDate = $booking->getStartDate();

$dateAsString = $startDate ? $startDate->asDateTimeString() : null;

With the addition of the nullsafe operator, we can now have null coalescing-like behaviour on methods!

$dateAsString = $booking->getStartDate()?->asDateTimeString();

Named arguments rfc

Named arguments allow you to pass in values to a function, by specifying the value name, so that you don’t have to take their order into consideration, and you can also skip optional parameters!

function foo(string $a, string $b, ?string $c = null, ?string $d = null) 
{ /* … */ }

foo(
    b: 'value b', 
    a: 'value a', 
    d: 'value d',
);

Attributes rfc

Attributes, commonly known as annotations in other languages, offers a way to add meta data to classes, without having to parse docblocks. As for a quick look, here’s an example of what attributes look like, from the RFC:

use App\Attributes\ExampleAttribute;

#[ExampleAttribute]
class Foo
{
    #[ExampleAttribute]
    public const FOO = 'foo';
 
    #[ExampleAttribute]
    public $x;
 
    #[ExampleAttribute]
    public function foo(#[ExampleAttribute] $bar) { }
}
#[Attribute]
class ExampleAttribute
{
    public $value;
 
    public function __construct($value)
    {
        $this->value = $value;
    }
}

Note that this base Attribute used to be called PhpAttribute in the original RFC, but was changed with another RFC afterwards.

Match expression rfc

You could call it the big brother of the switch expression: match can return values, doesn’t require break statements, can combine conditions, uses strict type comparisons and doesn’t do any type coercion.

It looks like this:

$result = match($input) {
    0 => "hello",
    '1', '2', '3' => "world",
}; 

Constructor property promotion rfc

This RFC adds syntactic sugar to create value objects or data transfer objects. Instead of specifying class properties and a constructor for them, PHP can now combine them into one.

Instead of doing this:

class Money 
{
    public Currency $currency;
 
    public int $amount;
 
    public function __construct(
        Currency $currency,
        int $amount,
    ) {
        $this->currency = $currency;
        $this->amount = $amount;
    }
}

You can now do this:

class Money 
{
    public function __construct(
        public Currency $currency,
        public int $amount,
    ) {}
}

New static return type rfc

While it was already possible to return selfstatic wasn’t a valid return type until PHP 8. Given PHP’s dynamically typed nature, it’s a feature that will be useful to many developers.

class Foo
{
    public function test(): static
    {
        return new static();
    }
}

New mixed type rfc

Some might call it a necessary evil: the mixed type causes many to have mixed feelings. There’s a very good argument to make for it though: a missing type can mean lots of things in PHP:

  • A function returns nothing or null
  • We’re expecting one of several types
  • We’re expecting a type that can’t be type hinted in PHP

Because of the reasons above, it’s a good thing the mixed type is added. mixed itself means one of these types:

  • array
  • bool
  • callable
  • int
  • float
  • null
  • object
  • resource
  • string

Note that mixed can also be used as a parameter or property type, not just as a return type.

Also note that since mixed already includes null, it’s not allowed to make it nullable. The following will trigger an error:

// Fatal error: Mixed types cannot be nullable, null is already part of the mixed type.
function bar(): ?mixed {}

Throw expression rfc

This RFC changes throw from being a statement to being an expression, which makes it possible to throw exception in many new places:

$triggerError = fn () => throw new MyError();

$foo = $bar['offset'] ?? throw new OffsetDoesNotExist('offset');

Inheritance with private methods rfc

Previously, PHP used to apply the same inheritance checks on public, protected and private methods. In other words: private methods should follow the same method signature rules as protected and public methods. This doesn’t make sense, since private methods won’t be accessible by child classes.

This RFC changed that behaviour, so that these inheritance checks are not performed on private methods anymore. Furthermore, the use of final private function also didn’t make sense, so doing so will now trigger a warning:

Warning: Private methods cannot be final as they are never overridden by other classes

Weak maps rfc

Built upon the weakrefs RFC that was added in PHP 7.4, a WeakMap implementation is added in PHP 8. WeakMap holds references to objects, which don’t prevent those objects from being garbage collected.
Take the example of ORMs, they often implement caches which hold references to entity classes to improve the performance of relations between entities. These entity objects can not be garbage collected, as long as this cache has a reference to them, even if the cache is the only thing referencing them.

If this caching layer uses weak references and maps instead, PHP will garbage collect these objects when nothing else references them anymore. Especially in the case of ORMs, which can manage several hundreds, if not thousands of entities within a request; weak maps can offer a better, more resource friendly way of dealing with these objects.
Here’s what weak maps look like, an example from the RFC:

class Foo 
{
    private WeakMap $cache;
 
    public function getSomethingWithCaching(object $obj): object
    {
        return $this->cache[$obj]
           ??= $this->computeSomethingExpensive($obj);
    }
}

Allowing ::class on objects rfc

A small, yet useful, new feature: it’s now possible to use ::class on objects, instead of having to use get_class() on them. It works the same way as get_class().

$foo = new Foo();

var_dump($foo::class);

Non-capturing catches rfc

Whenever you wanted to catch an exception before PHP 8, you had to store it in a variable, regardless whether you used that variable or not. With non-capturing catches, you can omit the variable, so instead of this:

try {
    // Something goes wrong
} catch (MySpecialException $exception) {
    Log::error("Something went wrong");
}

You can now do this:

try {
    // Something goes wrong
} catch (MySpecialException) {
    Log::error("Something went wrong");
}

Note that it’s required to always specify the type, you’re not allowed to have an empty catch. If you want to catch all exceptions and errors, you can use Throwable as the catching type.

Trailing comma in parameter lists rfc

Already possible when calling a function, trailing comma support was still lacking in parameter lists. It’s now allowed in PHP 8, meaning you can do the following:

public function(
    string $parameterA,
    int $parameterB,
    Foo $objectfoo,
) {
    // …
}

As a sidenote: trailing commas are also supported in the use list of closures, this was an oversight and now added via a separate RFC.

Create DateTime objects from interface

You can already create a DateTime object from a DateTimeImmutable object using DateTime::createFromImmutable($immutableDateTime), but the other way around was tricky. By adding DateTime::createFromInterface() and DatetimeImmutable::createFromInterface() there’s now a generalised way to convert DateTime and DateTimeImmutable objects to each other.

DateTime::createFromInterface(DateTimeInterface $other);

DateTimeImmutable::createFromInterface(DateTimeInterface $other);

New Stringable interface rfc

The Stringable interface can be used to type hint anything that implements __toString(). Whenever a class implements __toString(), it automatically implements the interface behind the scenes and there’s no need to manually implement it.

class Foo
{
    public function __toString(): string
    {
        return 'foo';
    }
}

function bar(string|Stringable $stringable) { /* … */ }

bar(new Foo());
bar('abc');

New str_contains() function rfc

Some might say it’s long overdue, but we finally don’t have to rely on strpos() anymore to know whether a string contains another string.

Instead of doing this:

if (strpos('string with lots of words', 'words') !== false) { /* … */ }

You can now do this

if (str_contains('string with lots of words', 'words')) { /* … */ }

New str_starts_with() and str_ends_with() functions rfc

Two other ones long overdue, these two functions are now added in the core.

str_starts_with('haystack', 'hay'); // true
str_ends_with('haystack', 'stack'); // true

New fdiv() function pr

The new fdiv() function does something similar as the fmod() and intdiv() functions, which allows for division by 0. Instead of errors you’ll get INF-INF or NAN, depending on the case.

New get_debug_type() function rfc

get_debug_type() returns the type of a variable. Sounds like something gettype() would do? get_debug_type() returns more useful output for arrays, strings, anonymous classes and objects.

For example, calling gettype() on a class \Foo\Bar would return object. Using get_debug_type() will return the class name.

A full list of differences between get_debug_type() and gettype() can be found in the RFC.


New get_resource_id() function pr

Resources are special variables in PHP, referring to external resources. One example is a MySQL connection, another one a file handle.

Each one of those resources gets assigned an ID, though previously the only way to know that id was to cast the resource to int:

$resourceId = (int) $resource;

PHP 8 adds the get_resource_id() functions, making this operation more obvious and type-safe:

$resourceId = get_resource_id($resource);

Abstract methods in traits improvements rfc

Traits can specify abstract methods which must be implemented by the classes using them. There’s a caveat though: before PHP 8 the signature of these method implementations weren’t validated. The following was valid:

trait Test {
    abstract public function test(int $input): int;
}

class UsesTrait
{
    use Test;

    public function test($input)
    {
        return $input;
    }
}

PHP 8 will perform proper method signature validation when using a trait and implementing its abstract methods. This means you’ll need to write this instead:

class UsesTrait
{
    use Test;

    public function test(int $input): int
    {
        return $input;
    }
}

Object implementation of token_get_all() rfc

The token_get_all() function returns an array of values. This RFC adds a PhpToken class with a PhpToken::tokenize() method. This implementation works with objects instead of plain values. It consumes less memory and is easier to read.

Variable syntax tweaks rfc

From the RFC: “the Uniform Variable Syntax RFC resolved a number of inconsistencies in PHP’s variable syntax. This RFC intends to address a small handful of cases that were overlooked.”

Type annotations for internal functions externals

Lots of people pitched in to add proper type annotations to all internal functions. This was a long standing issue, and finally solvable with all the changes made to PHP in previous versions. This means that internal functions and methods will have complete type information in reflection.

ext-json always available rfc

Previously it was possible to compile PHP without the JSON extension enabled, this is not possible anymore. Since JSON is so widely used, it’s best developers can always rely on it being there, instead of having to ensure the extension exist first.

Breaking changes

As mentioned before: this is a major update and thus there will be breaking changes. The best thing to do is take a look at the full list of breaking changes over at the UPGRADING document. We will discuss about Breaking changes in next part.

Python is the most preferred programming language

Programming languages play a vital role in a programmer’s life to help build a great career, especially when the software world is full of thousands of programming languages. As a matter of fact, more than 50 programming languages have been added in this year’s Code Gladiators event, an annual coding competition by TechGig, to give programmers the ability to work in their preferred programming language.

Here is the key takeaway –

– Python is clearly the programming language that rules the hearts of more than 50% of software programmers.

– The charm of Java is imperishable even when a lot of new programming languages are emerging every day. Java is the second most-preferred programming language among coders.

– C and C++ have proved that old is always gold. 12% of the poll respondents have voted for these.

– JavaScript was on quite a hype in recent year but still, it has to go a long way to top the preferred language list.

– Ruby, PHP, and C# are no more able to lure a large number of programmers.

– Even when backed by Apple, Swift has failed drastically to win the hearts of programmers.

To get a clear picture of the poll responses, please refer to the image below.

wp-category

Categories are a very important feature of any WordPress website. They are very helpful to maintain or browse a website. They tell you what’s related to what and they give your visitors a good way to browse your site. But sometimes they are not serving your URL structures. Then what should you do?

Don’t worry guys we have are here to help. Today we are going to show how to fix this. Meanwhile you may also interested in 20 Reasons Why You Should Use WordPress For Your Website

So, there are different ways to achieve the same and we are going discuss them one by one.  Just remember that if you have a fresh WordPress installation then set up permalinks as you want. But if you are changing permalinks structure on a live website, do properly, redirect old URLs to new ones to avoid loses.

Remove Category from WordPress URLs with a Dot

  1. Go to Settings -> Permalinks.
  2. Select Custom Structure and put /%category%/%postname%/ there.
  3. Assign a dot to the Category base field and save the change.

The below picture will help you to understand.

Please remember that providing dot to Category base in settings is a must. As leaving it blank will use the default value. Visit a post after saving settings and check the URL, it won’t have the category base now. While this trick currently works, there’s no assurance that it will work in the future as well.

Remove Category From Your URLs with .htaccess

RewriteRule ^category/(.+)$ http://www.yourwebsite.com/$1 [R=301,L]

You can also add this code to your .htaccess file through FTP rather than using a dot. Add the code before the closing </IfModule> tag in the file and it will remove the category slug from WordPress permalink.

What if I am using Yoast SEO Plugin

The current version of SEO by Yoast plugin has discontinued the option of category removal from URLs. The last version had the option in the Advanced menu of plugin settings. The websites, who are using Yoast for a long time, still have their URLs without the category base.

If you’ve installed Yoast plugin on your fresh WordPress website, check the post URLs as well. While we’re not sure, the feature might be still there and working silently. Else there are other plugins as well to remove category slug.

Removing Category from Slug using a Plugin.

The plugin ‘Remove Category URL’ is much popular and has additional advantages too. You can try the plugin if your website is older than 6 months. It will redirect the old category permalinks to the new ones, which is better for SEO. Also, you don’t need to configure anything or modify a file. Get the WordPress official plugin URL here.

Although you can always write a function for the functionality it makes the site slower. At last, all easy methods to remove the category slug from URLs are there. Do let us know in the comment section which one do you prefer.

 

It’s the end of 2016, which means that we’ve now finished the “20 Years of Java” celebrations. Actually, although the announcement about Java (and the HotJava browser, which did not enjoy quite the same success) was made publicly on May 23rd, 1995, the first official release of the JDK was on January 23rd, 1996. You could justifiably claim that Java is only now turning twenty. There have been numerous retrospectives on Java, but I thought what would be interesting to look at is the success of Java in its twentieth year.

It’s difficult to accurately measure the popularity of programming languages, but one of the generally accepted surveys is the TIOBE index. This tracks language references through a variety of search engines as well as Google Blogs, Wikipedia and even YouTube. (In writing this entry I learnt something new, which is that TIOBE is actually an acronym for “The Importance Of Being Ernest,” the play by Oscar Wilde. Not sure what that has to do with programming languages, but there you go.).

Looking back at the results over the last fifteen years Java has consistently been either number one or two. Java has jostled for position with C, which was consistently the most popular before Java was launched (how things change: back in 1986 Lisp was number two and Ada was number three). TIOBE have also just announced that Java is their “Programming Language of the Year” for 2015. A great achievement, especially as it was also given the award ten years ago.

As another measure of popularity, Oracle frequently likes to quote the statistic that there are nine million Java developers in the world. If you want a chuckle check out this JAXenter article, which gives some details of how they got to this number. Regardless of the details I think we can all agree there are literally millions of Java developers around the world.

The question to ask is why does Java continue to be so popular? I think there are a number of reasons for this; let’s just highlight four:

1. Practicality
James Gosling has described Java as a “blue collar” programming language. It was designed to allow developers to get their job done with the minimum of fuss, whilst still enabling developers to pick up someone else’s (or even their own) code at a later date and understand what it’s supposed to do. Sure, you can write unreadable code in Java, just as you can in any language, but with good coding conventions it is more readable than many other languages.

2. Backwards compatibility
Sun and subsequently Oracle have made huge efforts to ensure that code written for one version of Java will continue to run unchanged on newer versions. Although this hasn’t always been the case (assertions in Java SE 1.4, enumerations in Java SE 5) and it has sometimes led to implementations that could have been better without compatibility (generics) it is a very compelling feature for developers. There’s nothing worse than taking code that works and having to change it to make it work on a newer version of the platform. That’s just wasted time.

3. Scalability/Performance/Reliability
With over twenty years and thousands of man-years of development, Java is a rock-solid platform that performs on a level that can match or even exceed that of native code (thanks to some of the optimisations made by the JVM using dynamic rather than static code analysis). When it comes to scalability, just look at some of the large enterprises using Java: Twitter (who moved off Ruby-on-Rails to the JVM because RoR wouldn’t scale), Spotify, Facebook, Salesforce, eBay and, of course, Oracle. Hadoop, Cassandra and Spark, the basis of most big data projects, are either written in Java or Scala and run on the JVM. If you want scalability and performance, Java and the JVM is an obvious choice.

4. Freshness
To me this is the big one. Looking at the TIOBE graph there is a significant upswing in Java popularity since October 2014, which is shortly after the launch of JDK 8. JDK 8 was a big change for developers using Java because of the introduction of Lambda expressions and the streams API. Suddenly Java developers could do things in a more functional way without having to learn a whole new language like Scala. These features also make it potentially much simpler to take advantage of multi-core/multi-processor machines without having to write lots of complex and potentially error prone multi-threaded code. With project Jigsaw scheduled for delivery in JDK 9 we’ll see modularity make big enterprise applications much easier to build, deploy and maintain. There are already plans for new language features, like value types, in JDK 10.

I look forward to seeing Java being awarded Programming Language of the Year in another ten years’ time.

WordPress Location-Based Search

Create a WordPress Location-Based Search:

Creating a location-based search feature for a WordPress website involves integrating geolocation functionality and customizing your search system to filter results based on user-provided or detected location data. Here’s a general guide to help you achieve this:

1. Using a plugin:

1. Choose a Geolocation Plugin: Start by choosing a suitable WordPress plugin that offers geolocation functionality. Some popular options include “WP Store Locator,” “GeoDirectory,” and “Locatoraid.”

2. Install and Activate the Plugin: Search for your chosen geolocation plugin in the WordPress plugin repository, install it, and activate it.

3. Configure the Plugin: Most geolocation plugins will have settings to configure how your location-based search works. You’ll need to set parameters such as map styles, location data input methods (manual entry or automatic detection), radius distance, and other relevant options.

4. Add Locations: If your website involves displaying specific locations (e.g., stores, events, properties), you’ll need to add these locations using the plugin’s interface. Typically, you’ll provide the location name, address, and potentially additional information like hours of operation or contact details.

5. Integrate Geolocation with Search: Depending on the plugin you’re using, it might have built-in shortcodes or widgets that you can use to display a search form with location-based options. If not, you might need to customize your theme’s templates or use a page builder to create a custom search page.

6. Customize the Search Form: Create a search form that includes a location input field along with other search criteria you want to offer. The location input could be a text field where users can type in their location or use a map-based picker if your plugin supports it.

7. Filter Search Results: Modify your website’s search functionality to consider the location parameter when querying your database for results. You’ll need to integrate the plugin’s geolocation functionality into your search queries. This might involve using hooks, filters, or custom code depending on your site’s setup and the chosen plugin.

8. Display Results: Once you’ve gathered location-based search results from your database, display them on your search results page. Depending on your design, you might want to show the results as a list, grid, or on a map.

9. Implement Maps (if needed): If your plugin supports it, you can integrate interactive maps into your search results page. This can help users visualize the location of search results more effectively.

10. Test Thoroughly: Testing is crucial to ensure that the location-based search functionality works as intended. Test it on various devices, browsers, and with different search scenarios to catch any potential issues.

Remember that this is a general guide, and the exact steps might vary based on the plugin you choose and your website’s specific requirements. Make sure to refer to the documentation of the geolocation plugin you’re using for more detailed instructions.

2. Custom Code

I’ve done this a few times in the past but yesterday I decided to write these notes for a colleague to show how to construct a location based search for a new WordPress site he is developing. Essentially to create a location search, in this case for Property sales, we need to override the standard WordPress search with our own custom query. The following is split into sections. It’s written mostly in note form but it should be easy enough to follow. If you have any questions by all means comment and I’ll answer as best as I can.

Data

Create a new custom post type called ‘property’ which will serve as the location for the data keeping it away from the other content on the site.

Custom fields

I use Advanced Custom Fields for all of my projects these days but by all means use your own plugin or method. As long as the data is stored in wp_postmeta then this tutorial is still valid. Create something similar to the following fields in your system. These aren’t really referenced specifically later on but are a good start for search fields for this sort of site.

  • Beds (select, fixed number of options)
  • Baths (select, fixed number of options)
  • Price (number field)
  • Address (separate fields unless you want to split them manually)
  • Type (select, predefined, multiselect?)
  • Status (select, predefined… under offer, sold, available?)

Custom data

On saving a new/exsiting property post type item you need to geocode the address and save the resulting latitude and longitude into the wp_postmeta table. Use the following page to help you with this:

https://codex.wordpress.org/Plugin_API/Action_Reference/save_post

Use the ‘book’ example looking for the ‘property’ post type almost verbatim. It will show you how to trigger a PHP function on the saving of a new or existing item of content using the correct post type. You then need to Geocode (convert to latitude and longitude) the address string from the POST array and save in postmeta per above. The following page will help you with that.

https://www.andrew-kirkpatrick.com/2011/10/google-geocoding-api-with-php/

If you aren’t familiar with how to save to postmeta then the following will help:

update_post_meta($post_id, 'latitude', $latitude);

Build the search form sidebar

This can be done either in raw PHP in sidebar.php (or somewhere in the theme) or as a widget (latter method preferred for sidebars as makes it portable). If widgets then the following will help significantly as the code sample is more or less exactly what you need:

https://codex.wordpress.org/Widgets_API

Copy the code from the section titled ‘Example’. This will register a widget on the system which you can drag into one or more sidebars in Appearance > Widgets

Setting up a sidebar is here:

https://codex.wordpress.org/Function_Reference/register_sidebar

If you are using the Bones theme (my preference) then it has several set up out of the box. A sidebar has a name which you use to put the contents onto the site. The code is simply:

dynamic_sidebar($sidebar_name);

It will echo by default and outputs nothing if no widgets so nice and easy really

Build the search form

You can do in HTML/CSS as normal hard coding the values of the search or pulling from $_GET which could be where you are advised to send the data through (means you can directly link via URL to a search result rather than POST which is less portable). The form action to use for a search in WP is /?s= which will activate the search template. ‘s’ is the querystring parameter for a search term. You can perform a search without it.. A handsome chappie wrote a code sample you can use:

https://www.sean-barton.co.uk/2012/05/wordpress-search-keyword

Perform the search

search.php in your theme powers the search results as you might know. The query itself can be done anywhere so use this for layout purposes. There should be ‘the loop’ in this file as normal which is designed for search results. Before ‘if (have_posts()): the_post();’ add a PHP function call or your own to something you have defined in functions.php or simply dump your search code into search.php.. there is no ‘wrong’ way to do it.

The trigger for the search is a WP function ‘query_posts()’. It will override the array of data that ‘has_posts’ has to work with and you can use the native output loop/functions to display the results.

Constructing a custom search would be a case of building an array and passing to query_posts. You can get most of the search done this way and then split into a bit of SQL perhaps for the location based side of things. I shall go into that later.

The resource you need to map the majority of the fields would be:

https://codex.wordpress.org/Class_Reference/WP_Query

WP Query is the class which WP uses at the back of query_posts but using the latter function sets up globals which make WordPress behave differently in ‘the loop’. For searching on data stored in postmeta you would need to use ‘meta_query’, taxonomies use ‘tax_query’ and general stuff like search you just pass as attributes to the array. Examples as follows which you can break down and use if you like:

function cf_custom_search_query() {
	global $wp_query; //not sure we need this
	
	$arr = array();
	
	$author = cf_get('author'); //cf_get is a wrapper on if (isset(something)) return something; else return false;
	$genre = cf_get('genre');
	$price_from = cf_get('price-from');
	$price_to = cf_get('price-to');
	
	$paged = get_query_var('paged'); //wp function. copy verbatim
	
	$arr['post_type'] = 'property'; //post type slub
	$arr['post_status'] = 'publish'; //published only as opposed to draft or private
	$arr['paged'] = $paged; //sets the page number to show.. just leave this as is
	
	$condition = array();
	
	foreach ($_REQUEST as $key=>$value) {
		if (substr($key, 0, 5) == 'cond-') {
			$condition[] = substr($key, 5); //for multi select checkboxes
		}
	}
	
	if ($val = cf_get('s')) {
		$arr['s'] = $val; //s is the actual search term
	}
	
	$meta_query = array();
	
	if ($price_from || $price_to || $author) { //meta query is for items in post_meta. we can only pass a single array although the array can have multiple conditions
		if ($price_from && $price_to) {
			$meta_query[] = array(
						'key'     => '_price',
						'value'   => array($price_from, $price_to),
						'type' => 'DECIMAL',
						'compare' => 'BETWEEN'
					);
		} else if ($price_from) {
			$meta_query[] = array(
						'key'     => '_price',
						'value'   => $price_from,
						'type' => 'DECIMAL',
						'compare' => '>='
					);
		} else if ($price_to) {
			$meta_query[] = array(
						'key'     => '_price',
						'value'   => $price_to,
						'type' => 'DECIMAL',
						'compare' => '<='
					);
		}

		if ($author) {
			$meta_query[] = array(
						'key'     => 'author',
						'value'   => $author,
						'type' => 'CHAR',
						'compare' => 'LIKE'
					);
		}
		
		if ($meta_query) {
			$arr['meta_query'] = $meta_query;
		}
	}
	
	if ($genre && $genre != '-') { //this is a custom taxonomy. so we pass the category slug (or an array of slugs) from a category.
		$arr['tax_query'][] = array(
			'taxonomy' => 'product_cat',
			'field'    => 'slug',
			'terms'    => $genre,
		);
	}
	
	if ($condition) { //as above. another custom taxonomy
		$arr['tax_query'][] = array(
			'taxonomy' => 'condition',
			'field'    => 'slug',
			'terms'    => $condition,
		);
	}
	
	if (!$sort = cf_get('sort_order')) {
		$sort = cf_session('sort_order', 'price_low');
	}
	
	if ($sort) {
		
		if ($sort == 'price_low' || $sort == 'price_high') {
			$arr['orderby'] = 'meta_value_num';
			$arr['meta_key'] = '_price';
			$arr['order'] = 'ASC';
			
			if ($sort == 'price_high') {
				$arr['order'] = 'DESC';
			}
		} else {
			$arr['orderby'] = 'title';
			$arr['order'] = 'DESC';
			
			if ($sort == 'abc') {
				$arr['order'] = 'ASC';
			}
		}
		
		$_SESSION['sort_order'] = $sort; //so it's saved for next time if they go elsewhere and come back
	}
	
	query_posts($arr); //performs the query and sets up the globals
}

Performing a location based search

On search we have ‘s’ or ‘location’ depending on how you built your form. Up to you which field you use but perform the same Geocode on the fly as you did for the save_post step above. This will give you a latitude and longitude to search on. To get into the actual SQL of a search you would want to implement something like the following:

https://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where //to add to WHERE
https://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join //to add to FROM

A handy usage conversation to help with the above:

https://wordpress.stackexchange.com/questions/75381/using-dynamic-conditions-in-posts-where-filter

This is a good resource giving you all of the filters you can use to get access to the SQL of the main query:

https://codex.wordpress.org/Plugin_API/Filter_Reference/posts_clauses

Make sure to wrap your add_filter and remove_filter calls in is_search() which means you only require the LAT/LONG calculation to be done in search based queries.. or just only add/remove the filter in your search function defined in functions.php or search.php and only called on a search.. making sure to remove once used.

if (is_search()) { 
	add_filter('posts_where', 'my_posts_where'); 
	//.. and so on
	//perform search function
	remove_filter('posts_where', 'my_posts_where'); 
	//.. and so on 
}

The remove is just housekeeping, however, if you don’t remove the filter(s) then the next time you run query_posts in that same page load the same filters will be added which might mess with the result unexpectedly. To put this together you need to join in two instances of wp_postmeta or $wpdb->postmeta (in case wp_ is not the table prefix) making sure to alias uniquely:

add_filter('posts_join', 'my_posts_join');

function my_posts_join($sql) {
	$sql .= ' JOIN ' . $wpdb->postmeta . ' my_join1 ON (' . $wpdb->posts . '.ID = my_join1.post_id AND my_join1.meta_key="latitude");
	return $sql; 
}

And in posts_where:

add_filter('posts_where', 'my_posts_where');

function my_posts_where($sql) {
	$sql .= ' AND my_join1.value = "blah" '; //always start AND because it will be appended onto the main query and you can use the aliases defined in the post_join above
	return $sql; 
}

You can use the geocode values for the location entered via $_GET and then write the SQL to return the correct result based of something like this example:

https://developers.google.com/maps/articles/phpsqlsearch_v3

The section titled ‘Finding Locations with MySQL’ gives the query and some useful advice.

Get a brew and put your feet up!

How to Enable Two-Step Verification on WhatsApp

Technology giants such as Google, Apple, Microsoft, and Facebook among others have always advocated enabling two-step verification to add more security. If you still don’t use two-step verification, we recommend enabling the security feature following these simple steps for many popular services. Facebook-owned WhatsApp is the latest in the list of services to add support for two-step verification.

The new two-step verification is optional but your WhatsApp account is probably the one you want to secure first. The company assures that once two-step verification enabled, any attempt to verify phone number on WhatsApp will require the six-digit passcode that is created by the user.

Currently, the two-step verification feature is available only for beta users, we can expect it to be rolled out to all users in coming weeks. On Android, beta app users running version 2.16.341 or above will be able to enable the two-step verification. Windows 10 Mobile beta user, running version 2.16.280 of WhatsApp can enable the security feature.
To set up two-step verification for WhatsApp, you need to take the following steps:

Open WhatsApp (duh!)
Go to Settings
Go to Account
Look for Two-step verification
Tap on Enable
On the next screen, enter six-digit passcode.
Next, re-enter six-digit passcode.
Optionally, add your email address on the next screen.

In the last step – optional, as mentioned – WhatsApp asks for an email address to link with your account. This email address will let WhatsApp to send a link via email to disable two-step verification in case user forgets the six-digit passcode.

Remember that in case you user forget the six-digit passcode, WhatsApp won’t permit re-verifying the account within seven days of last using WhatsApp.

“After these seven days, your number will be permitted to re-verify on WhatsApp without your passcode, but you will lose all pending messages upon re-verifying – they will be deleted. If your number is re-verified on WhatsApp after 30 days of last using WhatsApp, and without your passcode, your account will be deleted and a new one will be created upon successfully re-verifying,” the WhatsApp team explains.

Have you set up two-step verification on your WhatsApp account? Let us know via the comments. For more tutorials, visit our How to section.

SEO graphics

You’ve printed flyers, you’ve sent out mailers, you have blasted your social-media followers, but you’re still not getting the lead flow you need to grow your company into the next billion-dollar brand. Well, there are hundreds of ways to generate leads, so don’t hang your head.

Here are ten ways to quickly generate leads.

1.eBooks

These work great for B2B companies or a business that works in a very technical space, as people love to read and gain expertise about their industry. Make sure you don’t promote your services or products. People don’t want to be sold, they want to be informed. So write it from a neutral perspective and give actionable insights. Share the eBook socially and ask your network to share it for you.

You’ll want to ensure that you have a landing page set up that requires visitors to input their name, email and phone number for a chance to download the eBook.

2. Newsletter

Do you have a newsletter yet? If not, you’re missing out on one of the simplest way to generate more leads. Make sure you put a newsletter sign up in every possible place that makes sense on your website.

With these newsletter, not only do you have a captive audience (people have to opt-in to your newsletter) but email pathways are a great way to avoid getting lost in all the Internet noise. Again, don’t make it all about you. Instead, share with your contacts your insights, recent wins you created for your customers and industry news.

3. Blog

Having a blog is one of the best lead generating tools you can use, as it not only allows a company (or person) complete control of what is said but also an opportunity to have the undivided attention of the reader. Make sure that your blog is optimized to generate leads by having a sign-up section for your newsletter and by using the margins to promote your products and services. And I feel like a broken record, but don’t make the blog all about you! Give real value.

4.Twitter

Twitter is a dream for generating leads. Use it to reach out to influencers in your industry and get into conversations with them. Their followers, who are probably some of your potential customers, will see your interactions and follow you or visit your site. You can also follow trending subjects that pertain to your business and interject your thoughts into the general discussion by using the # symbol. For instance, if you are in the mobile world, you may use #iphone to find and eventually get leads.

5. Networking events

While a lot of action happens in the digital world, the real world still provides a lot of advantages — especially networking events. Even events that didn’t directly pertain to my business have provided me with some of the most valuable contacts and leads. You’ll meet more people, expand your network and gain referrals. Plus, it’s a great way to build your brand. Make sure to bring business cards and don’t be afraid to ask the people you meet for their patronage.

6. Develop an engaging video

Please don’t produce another terrible commercial and post it on YouTube. The world doesn’t need any more of these. What people always want more of is entertainment. So, spend some time on developing a great idea that promotes your business and engages your customers. Once you’ve uploaded the video, you can get the ball rolling on making it go viral by using services like StumbleUpon to drive traffic to your video for pennies per viewer. Video sites like YouTube allow you to put links directly into the video. Use these to link back to specific landing pages on your website.

7. Infographic

Yes, infographics may have been overused a few years back, but people still share them and still stop and read them whenever they can.They’re quite cheap to produce as well. Simply come up with an original idea (again, don’t make it about you), find a quality (but affordable) graphic designer, then share it socially and ask your network to pass it along. You can also submit your infographics to publications that cover your industry.

Always put your logo and website URL on the bottom of the infographic. This way readers will be able to visit your site.

8. Webinars

Webinars are an inexpensive way to get your message to thousands of potential customers. There are many services that allow you to broadcast a webinar quickly and easily. And if you make it a recurring event, you’ll continue to grow your following. So, come up with a great idea that helps your customers and promote it using social media, your network and your newsletter.

At the end of the webinar, feel free to ask the attendees to download an eBook, sign up for your newsletter, or visit your site. This will bring the leads flowing in.

9. Media coverage

Do you know what journalists hate? Being hit up by dozens of PR agencies hour after hour, day after day. Do you know what journalists hate a little less? Having an owner of a company reach out to them and tell their story in a real and authentic manner. In fact, your chances of being covered go up significantly if you do the reach out yourself (as long as your pitch is on point). A few tips regarding reaching out to journalists: Don’t mass email them, do not open with “to whom it may concern” and don’t pester them (one follow up email is sufficient).

By getting your name out in the media, you begin to develop a following, increase your personal brand and come off as an expert in your domain – all that can help you get leads.

10. Strong branding

Customers love authentic companies. They like it when a company has a strong and clear message and that message is consistent across all of their marketing platforms. So, don’t try and be everything to everyone. Customers want the experts and the company that is the best in their industry. Focus on communicating that through your branding and your conversion rates will go up, resulting in more leads.

Google's Toughest Interview Questions

Google’s Toughest Interview Questions: Test Your Problem-Solving Skills

Discover Google’s Toughest Interview Questions: Prepare for challenging brain teasers and problem-solving scenarios that test your skills under pressure. Learn how to tackle quantitative and broad thinking challenges. While the specific questions may vary, we provide a glimpse into past interview questions that have stumped candidates at Google.

Google has a reputation for asking difficult brain-teaser questions that challenge how you act under pressure.

Most of them require you to think quantitatively and broadly, and test the way you tackle problems on the spot.

Google probably switches up its questions over time, but career website Glassdoor provides a glimpse of the types of brain-stumping puzzles Google has asked in the past.

Not all of Google’s tricky questions are necessarily meant to be brain teasers–some of them sound simple, but turn out to be difficult to answer in a concise way. Here are Google’s Toughest Interview Questions

1. What is your favorite Google product, and how would you improve it?–Associate Product Manager, January 2016

2. If you wanted to bring your dog to work but one of your team members was allergic to dogs what would you do?–Associate Account Strategist, December 2014

3. If ads were removed from YouTube, how would you monetize it? – Associate Account Strategist, January 2016

4. What do you know about Google?–Administrative Business Partner Interview, February 2015

5. Design an evacuation plan for the building.–Business Analyst Interview, November 2014

6. Which do you think has more advertising potential in Boston, a flower shop or funeral home?–Account Strategist, October 2015

7. A coin was flipped 1000 times and there were 560 heads. Do you think the coin is biased?–Quantitative Analyst, September 2015

8. What does “being Googley” mean to you?–Product Specialist, December 2015

9. Name a prank you would pull on x manager if you were hired.–Google Applications Support Engineer, June 2014

10. What is your opinion on whether or not individuals should be required to use their official name when opening a gmail or Google + account?–Administrative Assistant Interview, April 2014

11. What would you want to do if you didn’t have to work?–Interaction Designer, September 2014

12. What scares you?–Business Analyst Interview, September 2014

13. How many ways can you think of to find a needle in a haystack?–Business Associate, May 2014

14. Estimate the number of tennis balls that can fit into a plane.–Intern, December 2015

15. If you could be remembered for one sentence, what would it be?–Associate Account Strategist Interview, March 2014

16. If you could only choose one song to play every time you walked into a room for the rest of your life, what would it be?–Associate Account Strategist Interview, March 2014

17. How do you think the digital advertising world will change in the next 3 years?–Creative Director, January 2016

18. What three things would you change at your university/work place if you were CEO today?–Account Strategist Interview, April 2014

19. Describe AdWords to a seven-year-old.–Associate Account Strategist Interview, December 2014

20. You have a grocery delivery service (similar to Amazon Fresh) which delivers food within 24 hours. Estimate how many trucks you need to operate this service.–Product Manager, November 2015

21. How would you explain cloud computing to a 6 year old?–Product Manager, November 2015

22. Tell me what you think about Google charging users $1/month to use GMail.– BOLD Candidate, October 2015

23. How many haircuts do you think happen in America every year?–Business Associate, May 2014

24. List six things that make you nervous.–Android Support Level III, July 2014

25. Tell me something about you that isn’t on your resume.–Associate Account Strategist Interview, March 2014

26. What is the market for driverless cars in the year 2020?–Product Manager, November 2015

27. Model raindrops falling on a sidewalk (sidewalk is 1m and raindrops are 1cm). How could we know when the sidewalk is completely wet?–Software Engineer, January 2016

28. How would I explain the importance of HTML 5 to Larry Page and then to my grandma. — Creative Specialist, January 2016

29. Tell me a joke. — Executive Assistant Interview, March 2014

30. The best question in my opinion was, they asked me at this stage whether you prefer earning or learning. — Software Engineer, January 2016

31. If I gave you 10 million dollars right now, what would you do?–Associate Account Strategist, May 2014

32. Define a service that would allow you to travel to the future.–Interaction Designer, December 2015

33. Would you remove the link to an extremist piece of writing?–Legal Assistant, December 2015

34. How could you solve humankind’s biggest crisis given one billion dollars and a spacecraft?–Database Administrator, December 2015

35. You have a colony on Mars that you want to communicate with. How do you build a system to communicate with them?–Associate Product Manager, November 2014

36. How many cars travel across a bridge each day?–Advertising Interview, September 2014

37. If you had access to a bank’s database, how would you use that information to design an ATM for elderly people?–Associate Product Manager, February 2015

38. How would you improve a shoe factory?–Field Operations Specialist, November 2014

39. Design a mobile social app for a chain of local orthodontist offices.–Product Manager, November 2015

40. What are the number of new book titles published in the US each year?–Product Manager, November 2015

41. How would you solve homelessness in downtown San Francisco?–Product Manager, November 2015

Here are some additional Google interview questions that were asked up until 2021:

  1. Describe a time when you had to persuade someone to see things your way.
  2. How would you explain a complex technical concept to a non-technical audience?
  3. Design a system to track and manage inventory for an e-commerce website.
  4. How would you optimize Google Search for mobile devices?
  5. Solve a coding problem: Implement a function to check if a binary tree is balanced.
  6. How would you design a recommendation system for a streaming platform like Netflix?
  7. Describe a challenging project you worked on and how you overcame obstacles.
  8. How would you improve Google Maps to provide more accurate real-time traffic information?
  9. Solve a coding problem: Find the kth largest element in an unsorted array.
  10. How would you approach reducing the environmental impact of data centers?
  11. Describe a time when you had to make a difficult decision with limited information.
  12. Design a scalable and efficient system for processing and analyzing large volumes of data.
  13. How would you detect and prevent fraudulent activity on an online marketplace?
  14. Solve a coding problem: Reverse a linked list.
  15. How would you optimize the performance of a web application?
  16. Describe a time when you had to collaborate with a difficult team member.
  17. Design a system to detect and mitigate distributed denial-of-service (DDoS) attacks.
  18. How would you improve the user experience of a popular social media platform?
  19. Solve a coding problem: Determine if two strings are anagrams.
  20. Describe a time when you had to quickly learn a new technology or programming language.