Development
-
Using native WordPress loading icons (spinners) in your plugins
When we create our plugin, sometimes the loading icon (or the spinner icon) can be very useful and handy. It can be used to signal the saving of settings, processing of some data or loading data by Ajax. There are many fields to use, everything is limited by your invention and your needs. I have seen many times programmers create their own “spinners” or use ready-made ones instead of using WordPress ones. Inquiry – Why use those from WordPress? For one reason – to create a consistent interface. And, in addition, to reduce the size of the plugin being created. There are many possibilities, it is worth using them. WordPress…
-
Did you forget to signal async completion?
Gulp: did you forget to signal async completion? Did you forget to signal async completion is a very common error for Gulp 4 users. Before Gulp 4, when we used Gulp 3.9.1 etc, everything works fine. We updated to version 4 and now what? Well, Gulp changed a lot and we need to update our gulp files. Let’s see one: [crayon-675b3886d1d36888688790/] This is one task from one of my projects. It runs smoothly with Gulp 3.9.1. But when I upgraded to Gulp 4, I got this did you forget to signal async completion error while running gulpfile.js. So, what to do? We must adjust our code a little bit, like…
-
Country codes as PHP array
EN: I’m working on some client’s site and found myself in need of country names and codes written as PHP array. As finding the right list is relatively easy (maybe except polish names…) it was a bit harder to find it as PHP code. So I took Wikipedia list (it has all informations I need) and parse it to PHP array. Here is result, you may copy it for your project. PL: Pracując nad projektem dla klienta, zdałem sobie sprawę, że będę potrzebował listy państw wraz z ich kodami. Nie znalazłem takiej listy (szczególnie z polskimi nazwami), więc wziąłem listę z Wikipedii i przerobiłem na kod. Dodatkowo ten kod sobie…
-
Showing all errors and warnings
Sometimes, when you write PHP code, you may need to see every single error, warning or even notice. And this is a very good practice in your development environment. If you can write your code and after all tests there are not a single notice, you may think your code is ready. Of course it doesn’t mean it is good, it’s different thing. But suppressing (by handling, not by @!) all errors, warnings and notices are a very good first step. How to make PHP show all errors and warnings? It’s very easy: [crayon-675b3886d6b6e673418512/] And that’s all. Oh no, one more thing. Do not enable this on production server! Every…
-
Programmatically create and login user into WordPress
Is there a way to programmatically login user into WordPress? Well, of course yes. This is, in fact, very easy. WordPress does it every time you login! Even more, we may login without using any password. WordPress basically checks password hash and if it is correct, then log user in. We may just skip password hash comparing and jump straight into login. I wrote some small plugin recently to join foreign Application with WordPress installation. User has to login into application and WP login layer has to be invisible. Here is how I did it: [crayon-675b3886d6ee2340975733/] You may also want to know if user is already in our database. Very…
-
Finally here… required PHP version
I talked about it many times, I thought about it even more. However I was stupid enough not to talk about it to WordPress team. Well, somebody did. On March 2013 as far as I remember. And finally it is here… We can mark miminal required PHP version in plugins! And maybe in themes. It wasn’t very clear, but they mentioned themes As a next step, the WordPress core team is going look into showing users a notice that they cannot install a certain plugin or theme because their install does not meet the required criteria. I won’t test it, because I don’t have any themes in WordPress repository. However,…
-
How to style widget description in admin menu?
Is it ever possible to style widget description in admin menu? Well, the short answer should be no, however longer answer should be yes, of course. You must keep in mind, that this method is a bit tricky. If you may avoid that, please do. If you have to style your widget description using html, well… this article is just for you. I found myself dumbfound when I realized I need to style widget description but… it’s impossible using normal WordPress actions, filter or hooks. WordPress is using wp_sidebar_description function to show widget description in admin panel and it is located in \wp-includes\widgets.php. When you take a look into source,…
-
WordPress: How to localize your JavaScript code?
Update (on 17th July 2017) – see the bottom of the page. In WordPress, while writing theme or plugin, you should always keep in mind localization. Maybe your theme or plugin become so popular and someone would like to translate it into another language? It’s easy for me to think about localization, because I write my themes and plugins using english language and I usually add polish translation, because I’m Pole. So it isn’t very hard to achieve. Localization in WordPress is very easy. Let’s see one example from my plugin, Google Analytics Head. Plugins requires some information in the file header, like plugin name, description, author etc. There is…
-
Date and Time – PHP way – part 3
Today third and final part. Today we will talk mainly about DatePeriod class. It is very powerful tool, we can use it to iterate dates. How about iterating all Fridays? We like Fridays, because weekend starts, so… it is always good to know it’s Friday 🙂 Let’s begin… [crayon-675b3886d7980217985521/] And results: [crayon-675b3886d7985747907507/] I think this is self explanatory code. Please remember three things. One – I used next friday as an interval. If you use last friday you’ll get your iterator iterating downwards, so you may end in the year of -9999 🙂 Two – $item is DateTime object, you may do whatever you want as it’s normal object Three…
-
Date and Time – PHP way – part 2
Where were we? Ok., I got it 🙂 Now we gonna talk about time intervals. You know that, sometimes we need to calculate one month and a few days for example. What to do? Let’s say we start something yesterday, on 29th June 2017 and this event will continue for one month and eleven days. Don’t ask me why. So, how to calculate this period of time? We start with the code you already know: [crayon-675b3886d7bcf480271245/] The result is very easy to predict. Then, let’s clone $start_date object as $end_date and add date period to $end_date object like this: [crayon-675b3886d7bd5575975677/] Results are: [crayon-675b3886d7bd8710193526/] So, we know that one month and…