Not only does the language natively support functions for doing this, newer features of ES6 give us more powerful ways to build client-side scripts. And if you’re using jQuery with WordPress, then you have the same library of functions for, ahem, querying the DOM that we’ve had for years.
Other than, of course, we’re doing so on the server.
Continue reading “How to Manipulate the DOM Using PHP Manipulating the DOM on the client-side isn’t always the best option. We can do so using built-in features of PHP.“
Recently, I was working on a project that was communicating with a third-party API that, like many APIs, sends back a lot of data part of which includes a URL for the product associated with the API.
The thing about the API that was being returned was not the proper destination URL. Instead, it was a URL that ultimately redirected to another URL.
So imagine hitting, say, acme.site-info.com and having it direct to acme.com. We do this manually all the time, but I needed to get the actual – or the final – destination of the direct from the URL.
And finding the destination of a redirect with PHP is easy; however, this does assume there’s only a single redirect.
Nonetheless, here’s how to do it.
Continue reading “Finding the Destination of a Redirect with PHP This solution for finding the destination of a redirect with PHP is easy but it assumes a single redirect.“
If you’re used to working with models (in any foundation or framework, but specifically WordPress), then there’s a chance that you may need to serialize an instance of the model at some point.
Sure, writing the class to a database using PHP’s built-in functions is easy enough; however, introducing a bit of flexibility especially as it relates to making it available on other platforms is important.
For example, let’s say you’re building an application on WordPress that’s going to have some type of unique piece of information represented in a model. The model will then be accessible via a mobile application through the REST API.
Arguably, one of the easiest ways to get this done is to use JSON. It’s a format that works across various languages and platforms, can be easily serialized and de-serialized by said platforms, and sent across the wire as needed.
And it’s incredibly easy to implement this in PHP. You just need to make sure your class implements the JsonSerializable interface.
From the documentation, the interface does the following:
Objects implementing JsonSerializable can customize their JSON representation when encoded with json_encode().
The only method a class needs to provide is jsonSerialize, and though it’s likely you will want to serialize all of the properties of an object (as well as its state whenever its called), you can customize the implementation however you’d like.
Continue reading “WordPress Class Serialization with PHP Using the JsonSerializable Interface, adding WordPress class serialization is really easy.“
When you’re working with a collection in PHP, most notably, arrays in PHP, there are two ways in which you primarily see the information manipulated:
- through a variety of the
array functions that PHP provides.
For what it’s worth, I think the
array functions provide greater readability but they have been shown to be slower (especially with larger data – with smaller data, it’s naturally going to be negligible).
I often work with
for loops and related functions to achieve the same thing but I thought it might be worth look at an example from the previous post and how I used the
array functions to achieve the same things as a
Ultimately, this is is a comparison post but I think it’s good to see how the same code can be written in different ways.
Continue reading “Writing Loops in PHP: Two Ways; Same Thing This is is a comparison post but I think it’s good to see how the same code can be written in different ways.“
As many of us continue to move forward with PHP7+, we can continue to take advantage of a lot of new features that the language offers.
In the meantime, though, there are still features of PHP and related software that we can use t help streamline our development. The least of which (and that which I’ve written and spoken about a bit) is namespaces.
Here’s the thing, though: I like to have my plugin’s files and directories structured so that they are organized to mirror that of the namespace conventions they follow. And this can be done for taxonomies, meta boxes, domain objects, database-related functionality and so on.
In this post, though, I want to talk about a way of organizing WordPress settings screens from both the logical – that is, their file system location – and the virtual – that is, their namespaces – organizational structures.
Continue reading “Organizing WordPress Settings Screens with Namespaces I want to talk about a way of organizing WordPress settings screens from both the logic and the virtual organizational structures.“