Category

Programming

How to override the comments form in a child theme

By | Custom Wordpress Theme Development, PHP, Programming, Theming, Wordpress | No Comments

This is a quick post on how to provide your own comments template in your child theme ( overriding comments.php in your parent theme). You can just as easily use the same code to override comments.php if you aren’t using a child theme.

Todo this you need to you need to use the filter ‘comments_template’ ( see the wordpress codex on this filter here)

 

A handy thing you might sometimes want todo is to have a custom comments template, for various custom post types. If you did want todo this you could just add in an if to encase the returned path and filename. e.g.

 

 

Hope someone finds these snippets for how to override comments.php useful.

 

 

Regex reviser

By | Pattern Matching, Programming, Regular Expressions | No Comments

Every now and then I need to use regex ( and I always forget/confuse what things like * + etc mean ). So this is just a little reviser with a few examples to help me as a reference. Hope you find it useful also. I’ve only covered the basics here as thats usually all I need.

special characters

+ match 1 or more of the previous character

* match 0 or more of the previous character

? previous character is optional

. match any character (except new line)

^ match starting with (also used as the NOT operator with classes eg [^0-9] , this means not a number )

$ match ending with

classes

[a-z] match any lower case char

[A-Z] match any upper case char

[0-9] match any number

examples

 

recently I was working on a site todo with actors and needed to find all the actor profile pages in the database (they had links like this) :

So I wrote a pattern to find them in text:

/ahatalent.localhost\/actor\/[a-z]+/                    match one or more a-z chars after /actor/ , but this broke due to the – alison after alison (as is non a-z char)

/ahatalent.localhost\/actor\/.+/                            I changed to use the wildcard to match 1 or more of any character after the /actor/

I could also have written:

/ahatalent.localhost\/actor\/(.+)/                   this the () just means evaluate this first ( same as in php or any most programming languages)

Example 2 (match a string that has 4 or 5 characters exactly)

Lets say I want to find all the aria-hiddens and eg aria-hidden=”true” or aria-hidden=”false” in the following bit of html:

My pattern could be this:

/aria-hidden=”.{4,5}”/

This means find this string: aria-hidden=” followed by 4 or 5 of any character. So would match both:

  • aria-hidden=”true”
  • aria-hidden=”false”

Example 3

Match strings between a given length.

Lets say I want to find strings that are between 5 and 10 chars in length. Maybe I want to find numbers between 5 and 10 chars in length in some text.

e.g. 12345, 54321, 1234567890 any string like that

my pattern could be:

/[0-9]{5,10}/

 

 

 

 

How to get a users Roles and how to list all WordPress roles in PHP

By | PHP, Programming, Wordpress | One Comment

This code is handy if you want to find out a users role/s. You could expand it and check if a user has a given role or similar.

This code is based on examples from:

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

and

https://wordpress.stackexchange.com/questions/1665/getting-a-list-of-currently-available-roles-on-a-wordpress-site

If you did want to check if the user is in a certain group you could do something like this:

 

beginners guide to theming Woocommerce

How to make a WordPress Theme, Woocommerce Compatible from scratch

By | E-commerce, Ecommerce, PHP, Programming, Theming, Woocommerce, Wordpress | 6 Comments

In this post I’ll going to explain how I took a WordPress Bootstrap Theme and made it Woocommerce Compatible. I couldn’t find many good tutorials on this so I’m writing this one, so that it might help someone else.

The Process to make a theme Woocommerce Compatible

I’m going to take the very nice DevDmBootstrap3 theme and make it Woocommerce Compatible.

  1. I created a child theme of DevDmBootstrap3
  2. I added the required stuff needed to that child theme

I’ll skip over 1 (there are plenty tutorials of how to do that out there), and move right onto 2.

Complete code below (can be put in functions.php)

Sidebar Code for Woocommerce (wp-content/themes/THEME/sidebar.php )

The DevDmBootstrap3 theme didn’t have a sidebar.php so I added one to my child theme with the following:

Explanation of Code

Remove the Woocommerce default wrappers (this drops wraps the Woocommerce content in the theme, so everything looks nice), see remove_action lines.

Next, we need to make our own wrappers so Woocommerce can nicely be wrapped into the theme and all looks good. See functions gbb_theme_wrapper_start() and gbb_theme_wrapper_end(), in these functions I wrap the content in the theme (todo this on different themes this will vary slightly).

Roughly speaking the contents of the 2 wrapper functions will be output between the header.php and footer.php files of the theme you are using.  As you can see below I’ve added content to drop in relevant stuff from the DevDmBootstrap3 theme such as the menu e.g. ‘topnav’ .

 

Essentially what I’ve done is created a bootstrap row with 2 columns: column 1 is two thirds width and column 2 is one third width.

Column 1 (col-md-8)

This contains the main Woocommerce content.

Column 2 (col-md-4)

This contains the Woocommerce sidebar.

After that I declare Woocommerce support in the theme, and lastly create a sidebar for the theme (that I can use as my Woocommerce sidebar). The sidebar can be seen in the themes sidebar.php template file (which I created for this theme as it didn’t have one).

And thats about it in nutshell as they say, hope you find this useful.

 

 

 

 

References:

Woocommerce third party theming

Theming Jigoshop , this is the best resource I could find (Jigoshop is a fork of Woocommerce or vice versa)

 

Update 27 June 2016

To Download the latest version of the Woocommerce Compatible theme click here

In this version there is no sidebar on woocommerce pages (except on cart / checkout, this can be switched on/off via devdm-theme-options is needed).

To have a sidebar in the shop / product page etc this can be setup via the wrapper functions see file: make_theme_woocommerce_compatible.php (as in the tutorial above).

 

TODO at a later date I will do a part 2 to this tutorial to  create the rollover cart contents script (that will be put in the header to quickly preview whats in the cart).

 

 

 

Woocommerce – Add an Important Admin Note to the Order Report for Order pickers to view

By | Ecommerce, PHP, Plugins, Programming, Woocommerce, Wordpress | 3 Comments

In this article I’ll show you how to add an Important Admin Note to the Order Report, very useful if there’s some information you want to add for Order pickers to view. For instance maybe the item is fragile you can highlight this to the order picker.

 

Screen Shot 2016-01-14 at 15.21.07

This plugin allows you to enter an Admin note on the order and then displays this note on order report screen.

Screen Shot 2016-01-14 at 15.23.27

If you just want to get the plugin please click here to download the plugin. The rest of this article will run over how to create a plugin like this, so if you have an interest int he programming side of wordpress, this is for you.

Essentially to write this plugin we need todo 3 things:

  1. display the important Order Note input field on Woocommerce edit order screen
  2. save it when needed (i.e. when you save the order)
  3. display the note (in red in the graphic above) on the Woocommerce Order Report

So lets dive in:

Display the important Order Note input field on Woocommerce edit order screen

To display the order note field we need to do something like this (below). Print out the html for the text input field and add some explanatory note for the admin user.

 

Save it when needed (i.e. when you save the order)

In the code below I’m saving the Import Order Note, and before doing so checking that its valid.

 

finally

Display the note (in red in the graphic above) on the Woocommerce Order Report

In this function I use the relevant hook to drop my Import Order Note into the Order Report in the correct place.

 

I hope you found that interesting, if you need any help with Programming WordPress or Woocommerce please get in touch.

Wordpress create admin reports using WP_List_Table

WordPress create admin reports using WP_List_Table

By | PHP, Plugins, Programming, Wordpress | No Comments

In this post I’m going to discuss how to use WP_List_Table to create reports/tables/lists that are similar in style to the standard WordPress reports for Posts, Pages etc.

To achieve this I’ll use the WP_List_Table core WordPress class, this contains the functionality to create said tables/reports.

This example will look at the links table and display the data from it. This code used here is adapted from an example plugin on WordPress.org by Matt Van Andel

Important information to mention about creating admin reports using WP_List_Table

Its important to mention that this class is marked private by WordPress and they could change it in the future, without warning  (I’d be surprised if they did, given alot of peoples code relies on this class, but you never know). To be extra safe you can make a copy of the class in your own code and include this.

Explanation of the Code

The complete source code for the how to create admin reports using WP_List_Table plugin, is below. I’ve stripped out alot of the original comments. Please download the original plugin from WordPress.org to see the original version with comments (my version is adapted to run a SQL query and output the results as a report, the original doesn’t use SQL instead a pre-populated array is used for simplicity).

I’ll discuss in brief what the plugin does, first include the links report (this is hidden by default now in wp), I’m going to use this functionality to load wp_links with data that I can show on my report (for testing purposes):

Next the WP_List_Table class is included, this isn’t available by default to plugins. Then a class is created that inherits from WP_List_Table (this class will override the necessary methods of its parent class to give us the functionality we require).

Following this we have the column_default and get_columns methods. Column default tells class what to print in the report for each column. get_columns tells the class what our columns are.

The next part is the prepare_items method, this is where the magic happens. We query the database and setup the required information for our pagination and so on. After this the final 2 functions deal with putting a menu item on the left of the admin dashboard and the actual report display and we’re all good.

I hope you’ve found this interesting. Creating admin reports using WP_List_Table, is very useful for creating reports that are in the style of wordpress admin.

The complete code for how to create admin reports using WP_List_Table