Development
-
Do you like WordPress admin buttons?
Do you like WordPress admin buttons? Yeah, me too. And I noticed that Woocommerce has similar purple buttons. I stopped and I thought for a moment – why shouldn’t I create some other colors? Yeah, that was a good idea. My implementation needs some improvements, but I like it. You may see results over that text (yeah, I know, you know that already, it was easy Captain Obvious). Please note this is a static image file. You may also find source code on github – as SCSS source file and compiled CSS file. To use it, please add two classes to HTML element: .button and .some-button-name, like: class=”button button-darkgreen” or…
-
Add woocommerce category programmatically
As I promised in this article Assign woocommerce product categories programmatically, I will explain how to create woocommerce category programmatically. It is very easy, just use this code: [crayon-66dc8e1165ebf644370428/] You should get something like this: [crayon-66dc8e1165ec9618186455/] However, sometimes you may get WP_Error. Sometimes, category may already exists. You must check the result, if it isn’t WP_Error object. So, if you need to add category or get existing category ID, use this: [crayon-66dc8e1165ecc248246531/] This code uses PHP7 construction (?? – null coalesce operator), you may substitute it with isset when using earlier PHP version (or much better – upgrade your PHP or your hosting operator!) Remember to wrote this code in…
-
Assign woocommerce product categories programmatically
How to assign categories for your Woocommerce products programmatically? Well, it isn’t that hard. First of all, you must know the ID of your product. If you insert your product programmatically, it is easy, you got it. If not, you can see ID for many ways, for example in your edit screen (something like http://example.com/wp-admin/post.php?post=123&action=edit – 123 is your product ID). Let’s assume you’ve got your product ID in the $product_id variable. Now, you must decide if you want to assign product to existing category or create category by yourself? Well, if you already got category, you need your term ID. How to get it? Go to product categories list…
-
Deleting non empty directory
I assume that you are aware of the rmdir function. This is very useful function, however it has its own limitations. The biggest one is that you are not able to delete non empty folders. We need to build our own function. First, we will use recursion and second – this actually won’t be a function. We will write a method instead. Of course you may translate it into function, it is very easy (just remove $this->). [crayon-66dc8e1170b43414017408/] Please remember – this is a method, part of the class.
-
Different value and label of the input submit
Once upon a time (today), I stuck into this problem: I needed GET form with a few submit inputs. However, I needed numeric GET values (1-5) but I wanted to keep it simple for users – and I needed different labels than values… After some thinking, I figured it out. I can’t use input submit, I must use button! And here we are: [crayon-66dc8e1170fd3257260433/] A piece of cake.
-
Showing all errors, warnings, notices…
Why would you like to show all errors, warnings, notices, etc? Well, this is the first and easiest way to debug your PHP script. Of course there is a bunch of scripts and debug tools out there, but we do not need sledgehammer to crack a nut, don’t we? So, first thing is to turn this on on your local or staging server. Second thing, but even more important!, is to turn it off on your production server! You don’t want your users to see what is wrong this way. OK, let’s go to code: [crayon-66dc8e1171369678580087/] And that’s all! Of course you may need it more complicated way. Something like:…
-
Add theme support – basics
add_theme_support is very important functions for developing themes. Thanks to this function, you may add many cool features to your theme. And forget about title tag in your templates (since WordPress 4.1). Important notice: add_theme_support must be called before “init” hook is fired, because it is too late for some features. Use “after_setup_theme” hook or just put functions calls straight in your functions.php file. Example 1: functions.php [crayon-66dc8e117164b914759705/] Example 2: after_setup_theme hook [crayon-66dc8e1171650063969132/]
-
WordPress tables part 1
Some of you knows, that I write the book about WordPress programming. It is very slow process and I’m not even sure if I finish it this year. Of course I will try. While this process, I needed WordPress tables (I named it after mathematical tables, very popular before cheap-calculator arrived). So, I will try to give you a sample of it. WordPress actions muplugins_loaded registered_taxonomy (for “category”) registered_taxonomy (for “post_tag”) registered_taxonomy (for “nav_menu”) registered_taxonomy (for “link_category”) registered_taxonomy (for “post_format”) registered_post_type (for “post”) registered_post_type (for “page”) registered_post_type (for “attachment”) registered_post_type (for “revision”) registered_post_type (for “nav_menu_item”) plugins_loaded sanitize_comment_cookies setup_theme unload_textdomain (for “default”) load_textdomain (for “default”) after_setup_theme auth_cookie_malformed auth_cookie_valid set_current_user init registered_post_type…
-
Post class and Body class with WordPress
Classes in WordPress are important and commonly forgotten (in poor themes) elements. There are two main roles for classes, body class and post class. Of course we may get different post class set for each post. Body class body_class() is a shortcut for get_body_class() function. The easiest method to use it, is: [crayon-66dc8e117181a480570784/] Of course we may add our custom class, to do that, just put it as a string or an array. You may do that like this: [crayon-66dc8e117181e076571529/] or like this: [crayon-66dc8e1171820543315474/] The result will be absolutely the same. Of course you may use get_body_class() function, but you need to do something more. Like this: [crayon-66dc8e1171821354013824/] or like…
-
How to hide toolbar (admin bar)?
Well, I like toolbar (note, that we call it toolbar, since WordPress 3.3 as far as I remember, we called it admin bar before that) and I do not need to hide it, but I know many people wants to. So, here is very simple code to do that thing: [crayon-66dc8e1171b63188168961/] Please note, that this line will hide toolbar only on front-end! If you want to hide it on both frontend and in the admin panel (for god’s sake, why?) do that: [crayon-66dc8e1171b69540938468/] As easy as is.