Development
-
Date and Time – PHP way – part 1
Usually, when we speak about date and time, we think about date, time or gmdate (or many other names) functions and that is all right, we’ve got them and it works. But some of you may not be aware about DateTime class, built in PHP (since version 5.2 as far as I remember). First, the constructor. It can get two parameters – time and timezone. You may omit those two parameters, then time will be set to ‘now’ and timezone will be set to null. What does it mean? It will give you current time in your default timezone (default for the server of course). You may also get current…
-
Add meta boxes on Front Page only without template
I am rewriting one of my old pages into WordPress theme and I needed some meta boxes. But for front page only. I don’t need them to be show on any other page. It isn’t very hard to do, you just need to check template meta. But I don’t want to use template. I got front-page.php file to view front page and that’s it, I don’t need specialized template. So, what to do? It’s pretty easy. First, we need to hook into meta-box action. It’s simple and well known, but… Here’s the code: [crayon-67dd997d404cc368102800/] Next step is also easy as pie. We need to compare current object ID with page…
-
Always remember about variable type
Yes, I use the correct word. Always remember about variable type. It is not always important because PHP uses type casting, but it may lead you to hard-to-find mistakes. For example, I need to get some variable value. There are only three allowed states: 0, basic, advanced. As you may see, 0 is easily recognizable as integer and two others are strings. If variable (it is post_meta to be exact) is not set, it should use default value – 0. So, it is pretty easy: [crayon-67dd997d4085f027157313/] I do not need to filter it more, because I translate this variable once more later. So, let’s jump to this ‘later’: [crayon-67dd997d40864764936424/] Yes,…
-
Using jQuery UI CSS in admin screen
I work on new plugin and I need jQuery UI in admin screen. Well, it’s a common situation. Besides the javascript, I also need jQuery UI stylesheet. How to include proper CSS into your WordPress? It’s not that hard. First, we need to include jQuery and jQuery UI. As an example, we will also use jQuery UI Tabs. Normally we may use Google APIs, but while we are in WordPress environment, we should use jQuery provided. So, let’s write some class: [crayon-67dd997d40d5e774623906/] So, what we’ve got here? This code will include jQuery, jQuery UI, jQuery UI Tabs (because those are our dependencies) and plugin.js – our main javascript file located…
-
Using gulp to make your WordPress site runs faster
First of all, what is gulp? It’s open software, a task runner build on node.js. Maybe it isn’t very descriptive, so let’s jump into an example. When I build a theme or a plugin for WordPress, I use many CSS, less, scss, javascript files. For WordPress theme it may be: /src/less/basic.less /src/less/main.less /src/css/default-wp.css /src/css/theme-a.css /src/css/theme-b.css /src/css/responsive.css Now, I need those files in my theme, so I use wp_enqueue_scripts hook and then… No, I do not use wp_enqueue_style for each file (including .less compilation somewhere). So, what do I do? I use one single file, let’s call it my-theme.min.css. Just one file, one HTTP call (it may be a bit outdated…
-
How to modify footer in admin screen
Sometimes you may want to add your own footer text in admin screen. I use it often to add information about my theme. Of course it is visible only in admin screen, so you do not need to worry about WordPress regulation about asking user to view any copyright notice for common users. How to do that? It’s very easy, we need to use admin_footer_text filter. [crayon-67dd997d41136073500656/] So, this is the simplest solution. We’ve got more here. I think we should leave original WordPress copyright, because it is just fair. To do that, just preserve original $msg variable, like this: [crayon-67dd997d4113b781072065/] You may notice, that the footer string is in…
-
How to add your custom css or js on admin screen?
How to add your custom CSS or JS on admin screen? Well, it’s pretty easy. We need to hook into admin_enqueue_scripts action and this is the best and only solution. So, let’s write some code: [crayon-67dd997d4153a326135462/] Please note the $hook variable. Thanks to its value you may enqueue your scripts/styles depending on active admin screen. It is important, because you should not enqueue your scripts or styles where it isn’t necessary. Second thing, first conditional is_admin. It’s pretty obvious, but I have to mention it. Do not even hook into admin_enqueue when you are not on admin screen.
-
Get woocommerce random products
Sometimes people asks me how to get N random products from Woocommerce plugin. Well, I heard it isn’t obvious or easy. And still I don’t know why. It is more than easy! We just need to use get_posts function. Simplest function to get N random products (default value is three as you may see in the source code) would look like that: [crayon-67dd997d419b6734939627/]
-
WordPress Toolbar a.k.a. Admin Bar
First things first. Why WordPress Toolbar a.k.a. Admin Bar? Well, a lot of people knows Toolbar as Admin Bar. Still. To be honest, it was Admin Bar. It was Admin Bar in WordPress 3.2 (released July 4, 2011) and previous. Even more, for backward compatibility, WordPress class for Toolbar is named WP_Admin_Bar. However, since WordPress 3.3 (released December 12, 2011) it is Toolbar and please remember it. To be honest, I have to remember it, too. All right, to the point. WordPress Toolbar. Nice feature. Very useful. And great. And even more – it is customizable! And we will learn a few basic tricks, how to deal with it. It…
-
WordPress admin buttons once again
I just wrote small nice plugin to add custom CSS code to certain post/page. So I thought I may use it to show you my admin-like button styles (I mentioned it a few days ago – WordPress admin buttons). I’ve added static image to that post but here you are with brand new preview live! Primary Primary Darkgreen Darkgreen Forestgreen Forestgreen Green Green Lime Lime Limegreen Limegreen Seagreen Seagreen Mediumseagreen Mediumseagreen Springgreen Springgreen Mediumspringgreen Mediumspringgreen Darksalmon Darksalmon Mediumaquamarine Mediumaquamarine Coral Coral Orangered Orangered Turquoise Turquoise Lightseagreen Lightseagreen Mediumturquoise Mediumturquoise Cyan Aqua Cyan Aqua Darkcyan Darkcyan Teal Teal Darkturquoise Darkturquoise Deepskyblue Deepskyblue Sienna Sienna Steelblue Steelblue Dodgerblue Dodgerblue Cornflowerblue Cornflowerblue Royalblue…